文章

2

粉丝

131

获赞

0

访问

9.6k

头像
1422进制转换
我要提问
发布于2022年7月13日 22:07
阅读数 4.7k

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{   int m,n;
    char s[105];
    cin>>m>>n;
    cin>>s;
	int ans=0;
	for(int i=0;i<strlen(s);i++)
	{ans=ans*m;
	if(s[i]>='0'&&s[i]<='9')
	ans+=(s[i]-'0');
	else
	ans+=(s[i]-'A'+10);
		
	}
	char t[105];
	int cnt=0;
	while(ans>0)
	{
		int w=ans%n;
		if(w<10) t[cnt++]=w;
		else  t[cnt++]=w-10+'a'; 
		ans=ans/n;
	}
	for(int i=cnt-1;i>=0;i--)
	cout<<t[i];

return 0;	
}

为什么这道题我写的通过率为0呀,想不出来

登录查看完整内容


登录后发布评论

1 条评论
admin SVIP
2022年7月14日 10:48

你判断w小于10的时候赋值有问题,t是char类型数组,你的w却是int类型的,你应该再加一个'0'。

将23行

if(w<10) t[cnt++]=w;

改为

if(w<10) t[cnt++]=w+'0';

 

赞(0)