各位高手速度帮小弟个忙????做个题C++zhongshang

2024-11-22 17:29:20
推荐回答(2个)
回答1:

#include

int n2dec (int n, int m)
{
int result =0;
int q =1;
int a;
while(m>0)
{
a= m%10;
result +=q*a;
m/=10;
q*=n;
}
return result;
}

void main()
{
int m,n;
scanf("%d",&n);
scanf("%d",&m);
printf("%d\n",n2dec (n,m) );
}

回答2:

刚才看了道题深有感触,又修改了下,感觉简洁多了,程序如下:
#include "iostream.h"
#include "math.h"
int n2dec (int n, int m)
{
int remain,result,count;
result = count = 0;
while(m > 0)
{
remain = m % 10;
m = m / 10;
result += remain * (pow(n,count++));
}
return result;
}
void main()
{
int m,n;
cout<<"请输入进制n:";
cin>>n;
cout<<"请输入进制为n的数m:";
cin>>m;
cout<<"转换后的十进制数是: ";
cout<}