编程题1(string)参考程序:
#include <iostream>
using namespace std;
long long hex10(string num,int b)
{//int i;long long res=0;for(i=0;i<num.size();i++) if(num[i]>='0'&&num[i]<='9')res=res*b+num[i]-'0';else //如果num[i]>10res=res*b+num[i] -'A'+10;return res;
}
int main()
{int n;cin>>n;int cnt =0; while(cnt<n){string num;int b;cin>>b>>num;cout<<hex10(num,b)<<endl;cnt++;}return 0;
}
编程题一(字符数组)参考程序:
#include <iostream>
#include <cstring>
using namespace std;
int trans_digit(int k, char c)
{if (c <= '9')return (c - '0');return (c - 'A' + 10);
}
long long trans(int k, char str[])
{int l = strlen(str);long long res = 0, pw = 1;for (int i = l - 1; i >= 0; i--){res += pw * trans_digit(k, str[i]);pw *= k;}return res;
}
int main()
{int n = 0;cin >> n;for (int t = 0; t < n; t++) {int k = 0;char str[10];cin >> k >> str;cout << trans(k, str) << endl;}return 0;
}
编程题一(stoi)参考程序:
#include <iostream>
#include <string>
#include <cstdlib> // for stoiint main() {int N;std::cin >> N; // 读取 N// 处理每一个输入的进制数for (int i = 0; i < N; ++i) {int K; // 进制std::string number; // K进制数的字符串表示std::cin >> K >> number; // 读取进制和对应的K进制数// 将K进制数转换成十进制数int decimal_value = std::stoi(number, nullptr, K); // stoi的第三个参数指定进制// 输出转换后的十进制数std::cout << decimal_value << std::endl;}return 0;
}
编程题二(按位操作)参考程序:
#include <iostream>
using namespace std;
void output_digit(int d)
{if (d >= 10)cout << (char)('A' + d - 10);elsecout << (char)('0' + d);
}
void output_code(int s)
{output_digit(s >> 4);output_digit(s & 0x0f);
}
int main()
{long long n = 0;cin >> n;int split[10];int l = 0;if(n==0)cout<<"00"<<endl;else{while (n > 0) {split[l] = (int)(n & 0x7f);n >>= 7;l++;}for (int i = 0; i < l - 1; i++)split[i] |= 0x80;output_code(split[0]);for (int i = 1; i < l; i++) {cout << " ";output_code(split[i]);}cout << endl;}return 0;
}