有两个用例没过
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
vector<int> v;
int n,b;
while(cin>>n>>b)
{
bool flag=false;
int tmp;
if(n==0)
{
cout<<"Yes"<<endl<<"0";
break;
}
while(n!=0)
{
tmp=n%b;
n=n/b;
v.push_back(tmp);
}
int end=v.size();
//cout<<end<<endl;
int mid=v.size()/2;
// cout<<mid<<endl;
for(int i=0;i<mid-1;i++)
{
if(v[i]!=v[end-1-i])
{
flag=false;
break;
}
flag=true;
}
if(!flag)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
int j;
int cnt=0;
for(j=end-1;j>=0;j--)
{
if(cnt>0)
cout<<" ";
cnt++;
cout<<v[j];
}
v.clear();
}
return 0;
}
参考的一个正确的:
- #include<stdio.h>
- #include<stdlib.h>
- #include<vector>
- using namespace std;
- int N,b;
- vector<int> v;
- int main(){
- scanf("%d%d",&N,&b);
- if(N==0){
- printf("Yes\n0\n");
- }else{
- while(N){
- v.push_back(N%b);
- N=N/b;
- }
- int n=v.size();
- bool res=true;
- for(int i=0;i<n/2;i++)
- if(v[i]!=v[n-1-i]){
- res=false;
- break;
- }
- if(res==false)
- printf("No\n");
- else
- printf("Yes\n");
- for(int i=n-1;i>=0;i--){
- if(i==v.size()-1)
- printf("%d",v[i]);
- else
- printf(" %d",v[i]);
- }
- printf("\n");
- }
- //system("pause");
- return 0;
- }