求大佬们查错pj t1
  • 板块灌水区
  • 楼主Chancylaser
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/11/7 22:27
  • 上次更新2023/11/5 08:33:28
查看原帖
求大佬们查错pj t1
241817
Chancylaser楼主2020/11/7 22:27

这是在计蒜客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分过不了?

2020/11/7 22:27
加载中...