1019. General Palindromic Number (20):留有疑惑

如果将 int型 数组S[ ] 改为 string类型 S 测试点  2 4 5 错误; 如果添加上 判断 N是否为0 测试点2 4 错误; 调了好久 参考了网上的代码 其中未判断N是否为0也完全正确了 百思不得解···先保留着 日后再看, 同时 望各位仁兄指点一下 谢谢.


AC代码:

#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;
#define Max 100000
int index;

void conversion(int N, int D, int S[])
{
    index=0;
    while(N)
    {
        S[index++]=(N%D);
        N/=D;
    }
}

bool isPalindrom(int S[])
{
    for(int i=0; i<index/2; i++)
        if(S[i]!=S[index-1-i])
            return false;
    return true;
}

int main()
{
    int N, D;
    while(cin>>N>>D)
    {
        int S[Max];
        conversion(N, D, S);
        if(isPalindrom(S))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

        for(int i=index-1; i>0; i--)
            cout<<S[i]<<" ";
        cout<<S[0]<<endl;
    }
    return 0;
}

博主原思路代码:

#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;
#define Max 1000

string conversion(int N, int D)
{
    string S;
    while(N)
    {
        S+=(N%D)+'0';
        N/=D;
    }

    reverse(S.begin(),S.end());

    return S;
}

bool isPalindrom(string S)
{
    for(int i=0; i<S.size(); i++)
        if(S[i]!=S[S.size()-1-i])
            return false;
    return true;
}

int main()
{
    int N, D;
    cin>>N>>D;
    //while(cin>>N>>D)
    //{
//        if(N==0)
//        {
//            cout<<"Yes"<<endl;
//            cout<<"0"<<endl;
//            continue;
//        }
        string S=conversion(N,D);

        if(isPalindrom(S))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

        for(int i=0; i<S.size(); i++){
            if(i)
                cout<<" ";
            cout<<S[i];
        }
        cout<<endl;
    //}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值