第一题:

输入十六进制数,即0xA 或者0xa,输入对应的十进制数 10。

function main(x) {
    var map = {
        A: 10,
        B: 11,
        C: 12,
        D: 13,
        E: 14,
        F: 15,
        a: 10,
        b: 11,
        c: 12,
        d: 13,
        e: 14,
        f: 15,
        0: 0,
        1: 1,
        2: 2,
        3: 3,
        4: 4,
        5: 5,
        6: 6,
        7: 7,
        8: 8,
        9: 9
    }
    var sum = 0,
        n = 1;
    for (var i = x.length - 1; i > 1; i--) {
        sum += map[x[i]] * n;
        n *= 16;
    }
    return sum;
}
test = prompt('', '');
main(test);

第二题:

大数求余:输入一个字符串,表示两个大数,中间以空格隔开;输出两个数的余数。

// 大数的四则运算
#include<iostream>
#include<string>
using namespace std;

class BIGINTEGEROPERATIONS
{
private:
	static int COMPARE(string number1, string number2)
	{
		int j;

		int length1 = number1.size();
		int length2 = number2.size();

		if (number1.size() == 0) number1 = "0";
		if (number2.size() == 0) number2 = "0";

		j = 0;
		for (int i = 0; i < length1; ++i)
		{
			if (number1[i] == '0') ++j;
			else break;
		}
		number1 = number1.substr(j);

		j = 0;
		for (int i = 0; i < length2; ++i)
		{
			if (number2[i] == '0') ++j;
			else break;
		}
		number2 = number2.substr(j);

		length1 = number1.size();
		length2 = number2.size();

		if (length1 > length2)
		{
			return 1;
		}
		else if (length1 == length2)
		{
			if (number1.compare(number2) > 0)
			{
				return 1;
			}
			else if (number1.compare(number2) == 0)
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}
		else
		{
			return -1;
		}

		return 0;
	}

public:
	static string PLUS(string number1, string number2)
	{
		int i;
		int length1 = number1.size();
		int length2 = number2.size();

		string result = "";

		reverse(number1.begin(), number1.end());
		reverse(number2.begin(), number2.end());

		for (i = 0; i < length1 && i < length2; i++)
		{
			char c = (char)(number1[i] + number2[i] - 48);
			result = result + c;
		}

		while (i < length1)
		{
			result = result + number1[i];
			++i;
		}

		while (i < length2)
		{
			result = result + number2[i];
			++i;
		}

		int carry = 0;
		for (i = 0; i < (int)result.size(); ++i)
		{
			int value = result[i] - 48 + carry;
			result[i] = (char)(value % 10 + 48);
			carry = value / 10;
		}

		if (carry != 0)
		{
			result = result + (char)(carry + 48);
		}

		for (i = result.size() - 1; i >= 0; i--)
		{
			if (result[i] != '0') break;
		}

		result = result.substr(0, i + 1);

		reverse(result.begin(), result.end());
		if (result.length() == 0) result = "0";
		return result;
	}


	static string MINUS(string number1, string number2)
	{
		int i;
		string result = "";

		int length1 = number1.size();
		int length2 = number2.size();

		if (COMPARE(number2, number1) > 0)
		{
			return "-" + MINUS(number2, number1);
		}

		reverse(number1.begin(), number1.end());
		reverse(number2.begin(), number2.end());

		for (i = 0; i < length1 && i < length2; i++)
		{
			char c = number1[i] - number2[i] + 48;
			result = result + c;
		}

		if (i < length1)
		{
			for (; i < length1; i++)
			{
				result = result + number1[i];
			}
		}

		int carry = 0;
		for (i = 0; i < (int)result.length(); i++)
		{
			int value = result[i] - 48 + carry;
			if (value < 0)
			{
				value = value + 10;
				carry = -1;
			}
			else carry = 0;
			result[i] = (char)(value + 48);
		}

		for (i = result.size() - 1; i >= 0; i--)
		{
			if (result[i] != '0')break;
		}

		result = result.substr(0, i + 1);

		reverse(result.begin(), result.end());
		if (result.length() == 0) result = "0";
		return result;
	}


	static string MULTIPLY(string number1, string number2)
	{
		int i, j;
		int *iresult;
		int length1 = number1.size();
		int length2 = number2.size();
		string result = "";

		reverse(number1.begin(), number1.end());
		reverse(number2.begin(), number2.end());

		iresult = (int*)malloc(sizeof(int)* (length1 + length2 + 1));
		memset(iresult, 0, sizeof(int)* (length1 + length2 + 1));

		for (i = 0; i < length1; i++)
		{
			for (j = 0; j < length2; j++)
			{
				iresult[i + j] += ((number1[i] - 48) * (number2[j] - 48));
			}
		}

		int carry = 0;
		for (i = 0; i < length1 + length2; i++)
		{
			int value = iresult[i] + carry;
			iresult[i] = value % 10;
			carry = value / 10;
		}

		for (i = length1 + length2 - 1; i >= 0; i--)
		{
			if (iresult[i] != 0)break;
		}

		for (; i >= 0; i--)
		{
			result = result + (char)(iresult[i] + 48);
		}

		free(iresult);

		if (result == "") result = "0";
		return result;
	}


	// 缺省地,商数向下取整, floatpoint用于指定保留小数点的位数
	static string DIVIDE(string number1, string number2, int floatpoint = 0)
	{
		int i, j, pos;
		string result = "";
		string tempstr = "";
		int length1 = number1.size();
		int length2 = number2.size();

		if ((COMPARE(number2, number1) > 0) && (floatpoint == 0))
		{
			return "0";
		}

		tempstr = number1.substr(0, length2);
		pos = length2 - 1;

		while (pos < length1)
		{
			int quotient = 0;
			while (COMPARE(tempstr, number2) >= 0)
			{
				quotient++;
				tempstr = MINUS(tempstr, number2);
			}

			result = result + (char)(quotient + 48);
			pos++;
			if (pos < length1)
			{
				tempstr += number1[pos];
			}
		}

		if (floatpoint > 0)
		{
			result += '.';
			string stmp = "1";
			int itmp = 0;
			for (int k = 0; k < floatpoint; ++k)
			{
				stmp += '0';
				if (COMPARE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2) < 0)
				{
					result += '0';
					++itmp;
				}
			}

			string temp = DIVIDE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2);
			if (temp[0] != '0') result += temp;
		}

		j = result.size();
		for (i = 0; i < j; i++)
		{
			if (result[i] != '0') break;
		}

		result = result.substr(i, j);

		return result;
	}

	static string MOD(string number1, string number2)
	{
		if (COMPARE(number2, number1) > 0)
		{
			return number1;
		}
		else if (COMPARE(number2, number1) == 0)
		{
			return "0";
		}
		else
		{
			return MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2));
		}
	}
};

int main(int argc, char* argv[])
{
	string str1 = "9999999999999999999999999999999999999999";
	string str2 = "9998999899989998999899989998999899989998";

	cout << BIGINTEGEROPERATIONS::PLUS(str1, str2) << endl;
	cout << "===============" << endl;
	cout << BIGINTEGEROPERATIONS::MINUS(str1, str2) << endl;
	cout << "===============" << endl;
	cout << BIGINTEGEROPERATIONS::MULTIPLY(str1, str2) << endl;
	cout << "===============" << endl;
	cout << BIGINTEGEROPERATIONS::DIVIDE(str1, str2, 4) << endl;
	cout << "===============" << endl;
	cout << BIGINTEGEROPERATIONS::MOD(str1, str2) << endl;

	return 0;
}

第三题:

数字汉语读法转化为阿拉伯数字:输入一个字符串,表示一个阿拉伯数字的汉语读法,每个字符首字母大写, 如JiuQianJiuBaiJiuShiJiuWanJiuQianJiuBaiJiuShiJiu;输出对应的阿拉伯数字,如99999999。

现场用c++写的代码如下

#include<iostream>
#include<string>
#include<sstream>
#include<map>
#include<cctype>

using namespace std;

int main()
{
	string string1,string2;
	int i,flag,x,y,sum;
	map<string,int> pingyin;
	pingyin["Ling"]=0;
	pingyin["Yi"]=1;
	pingyin["Er"]=2;
	pingyin["San"]=3;
	pingyin["Si"]=4;
	pingyin["Wu"]=5;
	pingyin["Liu"]=6;
	pingyin["Qi"]=7;
	pingyin["Ba"]=8;
	pingyin["Jiu"]=9;
	pingyin["Shi"]=10;
	pingyin["Bai"]=100;
	pingyin["Qian"]=1000;
	pingyin["Wan"]=10000;
	
	while(cin>>string1)
	{
		string2 = string1;
		flag = 0;
		y = 0;
		sum = 0;
		for(i = 1; i < string1.size(); i++)
		{
			if(isupper(string1[i]))
			{
				string2 = string1.substr(flag,i-flag);
				x = pingyin[string2];
				if(x<10)
				{
					sum += y;
					y = x;
				}
				else
				{
					y *= x;
				}
				flag = i;
			}
		}
		string2 = string1.substr(flag,string1.size() - flag);
		x = pingyin[string2];
		if(x<10)
		{
			sum += y;
			sum += x;
		}
		else
		{
			y *= x;
			sum += y;
		}
		cout<<sum;
	}

    return 0;
}

回来用JavaScript重写,发现逻辑错了。

更改后的代码如下:

#include<iostream>
#include<string>
#include<sstream>
#include<map>
#include<cctype>

using namespace std;

int main()
{
	string string1, string2;
	int i, flag, x, y, sum;
	map<string, int> pingyin;
	pingyin["Ling"] = 0;
	pingyin["Yi"] = 1;
	pingyin["Er"] = 2;
	pingyin["San"] = 3;
	pingyin["Si"] = 4;
	pingyin["Wu"] = 5;
	pingyin["Liu"] = 6;
	pingyin["Qi"] = 7;
	pingyin["Ba"] = 8;
	pingyin["Jiu"] = 9;
	pingyin["Shi"] = 10;
	pingyin["Bai"] = 100;
	pingyin["Qian"] = 1000;
	pingyin["Wan"] = 10000;

	while (cin >> string1)
	{
		string2 = string1;
		flag = 0;
		y = 0;
		sum = 0;
		for (i = 1; i < string1.size(); i++)
		{
			if (isupper(string1[i]))
			{
				string2 = string1.substr(flag, i - flag);
				x = pingyin[string2];
				if (x<10)
				{
					sum += y;
					y = x;
				}
				else if (x == 10000 )
				{
					sum = (sum + y) * x;
					y = 0;
				}
				else
				{
					y *= x;
				}
				flag = i;
			}
		}
		string2 = string1.substr(flag, string1.size() - flag);
		x = pingyin[string2];
		if (x<10)
		{
			sum += y;
			sum += x;
		}
		else if (x == 10000)
		{
			sum = (sum + y) * x;
		}
		else
		{
			y *= x;
			sum += y;
		}
		cout << sum;
	}

	return 0;
}

JavaScript代码如下:

function main(x) {
    var map = {
        Shi: 10,
        Bai: 100,
        Qian: 1000,
        Wan: 10000,
        Ling: 0,
        Yi: 1,
        Er: 2,
        San: 3,
        Si: 4,
        Wu: 5,
        Liu: 6,
        Qi: 7,
        Ba: 8,
        Jiu: 9
    }
    var sum = 0,
        flag = 0,
        i = 1,
        j = 0,
        n = 1;
    x += "X";
    for (var len = x.length; i < len; i++) {
        if(x[i] < "a"){
            n = map[x.substring(flag,i)];
            if (n < 10) {
                sum += j;
                j = n;
            }
            else if (n == 10000){
                sum = (sum + j) * n;
                j = 0;
            }
            else
            {
                j *= n;
            }
            flag = i;
        }
    }
    sum += j;
    return sum;
}
test = prompt('', '');
main(test);