PAT A1019 General Palindromic Number (20)

总结:之前写进制转换一直用string存结果,这次测试用例中居然有大于9的,所以换成用vector<ijnt>存

代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
string result;
vector<int>m;
bool isT(int a,int b)
{
    if (a == 0)return true;
    while (a > 0)
    {
        int k = a%b;
        m.push_back(k);
        a = a / b;
    }
    for (int i = 0,j=m.size()-1; j-i>=1; i++,j--)
    {
        if (m[i] != m[j])return false;
    }
    return true;
}
int main()
{
    int a, b;
    cin >> a >> b;
    if (isT(a, b))cout << "Yes" << endl;
    else cout << "No" << endl;
    if (a != 0){
        for (int i = m.size() - 1; i >= 0; i--)
        {
            cout <<m[i];
            if (i != 0)cout << " ";
        }
    }
    else{
        cout << "0";
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81369088