这是在计蒜客ac的:
#include <bits/stdc++.h>
using namespace std;
int a[100], cnt;
int main(){
//freopen("power.in","r",stdin);
//freopen("power.out","w",stdout);
int n;
cin >> n;
if (n % 2 == 1){
cout << -1 << endl;
return 0;
}
int res = 1;
while (n != 0){
a[++cnt] = n % 2 * res;
n /= 2;
res *= 2;
}
for (int i = cnt; i >= 2; i--){
if (a[i] != 0){
cout << a[i] << ' ';
}
}
return 0;
}
这是0分的
#include<iostream>
using namespace std;
int a,b=0;
char ans[100005];
int i=1;
int q(int a,int y)
{
if(y==1)
{
return a;
}
if(y%2==0)
{
int t=q(a,y/2);
t=t*t;
return t;
}
if(y%2!=0)
{
int t=q(a,y/2);
t=t*t;
t=t*a;
return t;
}
}
int main()
{
//freopen("power.in","r",stdin);
//freopen("power.out","w",stdout);
cin>>a;
if(a%2!=0)
{
cout<<"-1";
return 0;
}
while(a)
{
if(a%2==1)
{
a=a/2;
ans[i]='1';
i++;
}
if(a%2==0)
{
a=a/2;
ans[i]='0';
i++;
}
}
for(int j=i;j>=1;j--)
{
if(ans[j]=='1')
{
cout<<q(2,j-1)<<" ";
}
}
return 0;
}
测试了多组大,小数据 都是一样的,但为什么第2分过不了?