自己的代码有两处过不了 不知道怎么回事
我的代码:
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1001;
typedef long long ll;
ll a, b;
string res;
int main() {
cin >> a >> b;
while (a != 0) {
ll t = a % b;
a = a / b;
res += (t + '0');
}
string ww = res;
if (ww == "") {
cout << "Yes" << endl << "0";
}
reverse(ww.begin(), ww.end());
if (ww == res) cout << "Yes" <<endl;
else cout << "No" <<endl;
for (int i = 0; i < ww.size(); i++){
cout << ww[i];
if (i != ww.size()-1) cout << " ";
}
return 0;
}
正确代码:
#include <cstdio>
using namespace std;
int main() {
int a, b;
scanf("%d %d", &a, &b);
int arr[40], index = 0;
while(a != 0) {
arr[index++] = a % b;
a = a / b;
}
int flag = 0;
for(int i = 0; i < index / 2; i++) {
if(arr[i] != arr[index-i-1]) {
printf("No\n");
flag = 1;
break;
}
}
if(!flag) printf("Yes\n");
for(int i = index - 1; i >= 0; i--) {
printf("%d", arr[i]);
if(i != 0) printf(" ");
}
if(index == 0)
printf("0");
return 0;
}