1019 General Palindromic Number (20 分)
思路
代码一的进制转换用了一个非常繁琐的方法,除法进制转换,所以在第二个代码用了求余进制转换。
回文数,for循环搜索一半判断即可。
代码一
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N,b,n,temp;
scanf("%d%d",&N,&b);
int num = 1;
for(int i =1 ;i<32;i++)
{
num = num * b;
if((N - num)<0)
{
n = i-1;
break;
}
}
int res[32]={0};
temp = N;
for (int i = n;i>=0;i--)
{
num = 1;
for(int j = 0;j<i;j++)
{
num*=b;
}
res[i] =temp / num;
temp -= res[i] * num;
}
int flag = 1;
for (int i = n;i>=n/2;i--)
{
if(res[i]!= res[n-i])
{
flag=0;
break;
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
for (int i = n;i>=0;i--)
{
if(i==0)
cout<<res[i];
else
cout<<res[i]<<' ';
}
}
代码二
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N,b,n,temp;
scanf("%d%d",&N,&b);
int res[32]={0};
int num = 0;
do{
res[num++] = N%b;
N = N/b;
}while(N!=0);
n = num-=1;
int flag = 1;
for (int i = n;i>=n/2;i--)
{
if(res[i]!= res[n-i])
{
flag=0;
break;
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
for (int i = n;i>=0;i--)
{
if(i==0)
cout<<res[i];
else
cout<<res[i]<<' ';
}
}