题目要求
解题思路
①由于可能出现键盘上所有的数字和字母来进行进制的转换,所以开一个比ASCII码多的整型数组来把把数组对应的字符的ASCII吗下标的内容变成对应的数字
②int的初始化一般只能初始化0,所以要用memset来将内容全部变成-1,值得注意的是memset的格式,
(数组地址,要变的数字,类型的占据×数量)
另外一点,memset每次只能改一个bit,而int类型有4个bit,所以除了0与-1,输入其他的数的时候,int的数据是相对应数的8位二进制×4拼接起来,所以并不会出现想要的数据
③第二个出现的符号应该是0,小心特殊情况
代码
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
int totalQ;
cin >> totalQ;
for(int i = 0 ; i < totalQ; i++)
{
int alpha[260];
memset(alpha,-1,260*sizeof(int));
string to_sort;
cin >> to_sort;
int length = to_sort.length();
int base = 0;
for(int j = 0; j < length; j++)
{
int temp = (int)to_sort[j];
if(alpha[temp] == -1)
{
// cout << base << " base" <<endl;
base++;
// cout << base << " base" <<endl;
// cout << endl;
if(base == 1)//只有一个字符也默认是二进制
{
alpha[temp] = 1;
}
else if(base == 2)//两个字符默认二进制
{
alpha[temp] = 0;
}
else
{
alpha[temp] = base - 1;
}
//cout << alpha[temp] << " "<< temp <<" " << (char)temp <<endl;
}
}
//解决完进制的对应后开始转化数据
long long Dnum = 0;
if(base == 1) base = 2;
// cout << base <<"base";
for(int j = 0; j < length; j++)
{
int temp = (int)to_sort[j];
int each_char = alpha[temp];
Dnum *= base;
Dnum += each_char ;
}
cout << "case #" << i << ":" << endl;
cout << Dnum << endl;
}
// system("pause");
return 0;
}