暴力能过吗?
查看原帖
暴力能过吗?
1101266
Fe_CuSO4__FeSO4_Cu楼主2024/12/14 17:52

纯属暴力,根据枚举的十进制转化为二进制,将二进制视作十进制,在进行除法,2TLE。

#include <bits/stdc++.h>
using namespace std;
#define N 1020
#define ll long long
int a[N],b[N];//a为用于除法的数组,b是用于所求的原数的数组
int main()
{
    int n;
    cin>>n;
    int i=1;
    while(1)
    {
		int cnt=0,o=i;
		while(o!=0)//转化二进制
		{
			a[++cnt]=o%2;
			b[cnt-1]=a[cnt-1];
			o/=2;
		}
		b[cnt]=a[cnt];
		
		int temp=0;
		for(int j=cnt;j>0;j--)//高精除法
		{
			temp=temp*10+a[j];
			a[j]=temp/n;
			temp%=n;
		}
		
		if(temp==0)//余数为0,输出
		{
			bool found=false;
		    for(int j=cnt;j>0;j--)	
		    {
		        if(!found&&a[j]!=0)
		        {
		            found=true;
		            cout<<a[j];
		        }
		        else if(found)
		        {
		            cout<<a[j];
		        }
		    }
		    
		    cout<<" ";
		    
			found=0;
		    for(int j=cnt;j>0;j--)	
		    {
		        cout<<b[j];
		    }
		    
		    return 0;
		}
		i++;
	}
	return 0;
}
2024/12/14 17:52
加载中...