}
直接用递归函数把进制转化完存入数组。
我的代码:
#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<iomanip>#include<string.h>using namespace std;int r[1000]; int cnt = 0;void suan(int a, int b){ if (a == 0) return ; suan(a/b, b); r[cnt++] = a%b;}int main(){ int a1, b1, i; while (scanf ("%d %d", &a1, &b1)!=EOF) { cnt = 0; if (a1 == 0) cout << "0" ; else if (a1 < 0) { cout << "-"; a1 = -a1; suan (a1, b1); for (i = 0; i < cnt ; i++) { if (r[i] < 10) cout << r[i]; else cout << (char)('A' + r[i] -10); } } else if (a1 > 0) { suan (a1, b1); for (i = 0; i < cnt; i++) { if (r[i] < 10) cout << r[i]; else cout << (char)('A' + r[i] -10); } } cout << endl; } return 0;}
注意, 要输出一个变量(如a)的ascll码值,如果用PRintf,就写printf(“%d”, &a)
如果是cout, 就写cout<<(int)a;
如果要输出一个ascll值代表的字符,用printf,就写printf(“%c”, &a)
如果是cout, 就写cout<<(char)a;
新闻热点
疑难解答