1019 General Palindromic Number (20分)测试点2 4

1019 General Palindromic Number (20分)测试点2 4_第1张图片
1019 General Palindromic Number (20分)测试点2 4_第2张图片
刚开始用的string来判断是否是回文数,结果有两个测试点过不去,之后发现是有的数可能大于10,这时候无法表示,所以进行了修改。
刚开始的写法:

#include
#include
#include
#include
using namespace std;
string getNum(int num, int radix)
{
     
	int r = 0;
	string res;
	while(num != 0)
	{
     
		r = num % radix;
		res += (r+'0');
		num /= radix;
	}
	return res;
}
int main()
{
     
	int n, b;
	scanf("%d%d", &n, &b);
    if(n == 0)
    {
     
        printf("Yes\n0");
        return 0;
    }
	string res = getNum(n, b);
	string ser = res;
	reverse(ser.begin(), ser.end());
	if(res == ser)
	cout << "Yes" << endl;
	else
	cout << "No" << endl;
	
	for(int i = 0; i < ser.size(); i++)
	printf("%c%c", ser[i], i < ser.size()-1?' ':'\n');
}

后来的写法:

#include
#include
using namespace std;
int n, b;
int main()
{
     
	scanf("%d%d", &n, &b);
	vector<int> num;
	int flag = 0;
	while(n != 0)
	{
     
		int r = n % b;
		num.push_back(r);
		n /= b;
	}
	for(int i = 0; i < num.size()/2; i++)
	{
     
		if(num[i] != num[num.size()-1-i])
		{
     
			flag = 1;
			break;
		}
	}
	if(flag == 1)
	printf("No\n");
	else
	printf("Yes\n");
	for(int i = num.size()-1; i >= 0; i--)
	printf("%d%c", num[i], i > 0 ? ' ':'\n');
}

你可能感兴趣的:(pat)