第三组数据不过
  • 板块P1143 进制转换
  • 楼主Q9_KKK
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/8/30 08:31
  • 上次更新2023/11/4 08:35:10
查看原帖
第三组数据不过
486570
Q9_KKK楼主2021/8/30 08:31

测试第三组数据发现少了一个1,然后printf发现是在最后÷3的时候少了一个1,于是我又测试了10,3,2输出为11显示正常。

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<ctype.h>
#include<cmath>
#include<cstring>
using namespace std;
int main() {
	int n;
	cin >> n;
	//cin.get();
	//string thenum = "\0";
	//		scanf("%x",&thenum);
	string thenum;
	cin >> thenum;
//	cin.get();
	//cout<<thenum[thenum.size()]=='\r'<<endl<<endl;
	//cin.get();
	//cin.get();
	//cout<<thenum;
	//cout<<thenum.size();
	int tennum = 0;
	int len = thenum.size();
	//cout << "thelength:" << thenum.size() << endl;
	//将n位制数转化为10进制
	//int len = strlen(thenum);
	//cout << strlen(thenum) << endl;
	for (int i = 0; i < len; i++) {
		//cout<<thenum[i]<<endl;
		//cout<<"length:"<<len<<n<<endl;
		//cout << isdigit(thenum[i]) << endl;
		if (isdigit(thenum[i])) {
			tennum += (thenum[i] - '0') * pow(n, len - i - 1);
		} else {
			//cout<<thenum[i]-'A'+10<<endl;
			//cout<<n<<endl;

			tennum += (thenum[i] - 'A' + 10) * pow(n, len - i - 1);

		}
		//cout<<tennum<<endl;
	}
	//cout<<"tennum:"<<tennum<<endl;

	//cout << (long long)pow(2, 30) << endl;
	//cout << tennum << endl << endl;
	//cout << tennum << endl << endl;
	int thediv;
	scanf("%x", &thediv);
	//cin.get();
	int thecnt = 0;
	string theout;
	while (tennum) {
		//stiring theout[100];
		int thecll = tennum - tennum / thediv * thediv;
		
		if (thecll < 10) {
			theout[thecnt] = thecll + '0';
		} else theout[thecnt] = thecll - 10 + 'A';
		tennum /= thediv;
		
		thecnt++;
		cout<<thecnt<<":"<<tennum<<endl;
	}
	cout << thecnt << endl;
	//cout<<theout[thecnt]<<endl;
	for (int i = thecnt - 1; i >= 0; i--) {
		cout << theout[i];
	}

	cout << endl;
	return 0;

}

2021/8/30 08:31
加载中...