关于c语言实现的任意进制转换谁能给我解答一下?代码如下
解决时间 2021-10-04 13:09
- 提问者网友:感性作祟
- 2021-10-04 00:01
#include
int a[210],len;
void cal(int p, int q) {
bool change = false;
int i, left;
for (i = left = 0; i left = (a[i] + left * p);
a[i] = left / q;
left%=q;
}
if (change) cal(p, q);
printf("%c",left+(left>=10?55:'0'));
}
int main() {
int p = 0, q = 0, t,i;
char s[1100];
for (scanf("%d", &t); t--; printf("\n")) {
scanf("%s", s);
for (p = i = 0; s[i] != ','; p = p*10 + s[i++]-'0');
for (i++, len = 0; s[i] != ','; i++, len++) a[len] = s[i]-(s[i]>='A'?55:'0');
for (q = 0, i++; s[i] != '\0'; i++) q = q * 10 + s[i] - '0';
cal(p, q);
}
}
最佳答案
- 五星知识达人网友:逃夭
- 2020-09-14 21:12
#include
int a[210],len;
void cal(int p, int q)
{
bool change = false;
int i, left;
for (i = left = 0; i
{
left = (a[i] + left * p);
a[i] = left / q;
left%=q;
}
// 如何商不为0
if (change)cal(p, q);
// 输出余数
printf("%c",left+(left>=10?55:'0'));
}
int main()
{
int p = 0, q = 0, t,i;
char s[1100];
//先输入t,表示循环次数
for (scanf("%d", &t); t--; printf("\n"))
{
scanf("%s", s);
//输入格式为 p,a,q
for (p = i = 0; s[i] != ','; p = p*10 + s[i++]-'0');
//a数组是以十进制保存
for (i++, len = 0; s[i] != ','; i++, len++) a[len] = s[i]-(s[i]>='A'?55:'0');
for (q = 0, i++; s[i] != '\0'; i++) q = q * 10 + s[i] - '0';
cal(p, q);
}
}
我要举报
大家都在看
推荐资讯