BFS 求助
查看原帖
BFS 求助
1101266
Fe_CuSO4__FeSO4_Cu楼主2024/12/15 09:46
#include <bits/stdc++.h>
using namespace std;
#define N 1020
#define ll long long
struct node
{
	string a;
	int cnt;
};
int b[N];
int n;
bool chu(node a)
{
	int temp=0;
	for(int i=0,j=a.a.size();i<a.a.size();i++,j--)
	{
		b[j]=int(a.a[i]-'0');
	}
	for(int i=a.cnt;i>0;i--)
	{
		temp=temp*10+b[i];
		b[i]=temp/n;
		temp%=n;
	}
	return !temp;
}

void bfs()
{
	queue<node>q;
	node h,t;
	h.a="1";
	h.cnt=1;
	q.push(h);
	while(!q.empty())
	{
		h=q.front();
		q.pop();
		if(chu(h))
		{
			
			bool found=false;
		    for(int j=h.cnt;j>0;j--)	
		    {
		        if(!found&&b[j]!=0)
		        {
		            found=true;
		            cout<<b[j];
		        }
		        else if(found)
		        {
		            cout<<b[j];
		        }
		    }
		    cout<<" "<<h.a;
		    exit(0);
		}
		else
		{
			t.a=h.a+"0",t.cnt=h.cnt+1;
			q.push(t);
			t.a=h.a+"1",t.cnt=h.cnt+1;
			q.push(t);
		}
	}
}
int main()
{
    cin>>n;
    bfs();
	return 0;
}
2024/12/15 09:46
加载中...