输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0-9的一个排列(可以有前导0,)
2<=n<=79
样例输入:
63
样例输出:
79546 / 01283 = 62
94736 / 01528 = 62
思路:枚举fghij,算出abcde,判断每一个都不相等
//用sprintf
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; int main() { int fghij,abcde,n,i,j,cz=0; char str[11]; while(scanf("%d",&n)!=EOF&&n) { if(cz++) printf("\n"); int ok=0; for(fghij=1234;;fghij++) { abcde=fghij*n; sprintf(str,"%05d%05d",abcde,fghij); if(strlen(str)>10) break; sort(str,str+10); for(i=1;i<10;i++) if(str[i-1]==str[i]) break; if(i==10) {printf("%05d / %05d = %d\n",abcde,fghij,n);ok=1;} } if(ok==0) printf("There are no solutions for %d.\n",n); } return 0; }
//直接一个一个枚举
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int main() { int sum; int a,b,c,d,e,n,i,sum1,t=0; int x[10]; while(scanf("%d",&n)!=EOF&&n) { int ok=0; if(t++)printf("\n"); for(a=0;a<=9;a++) for(b=0;b<=9;b++) for(c=0;c<=9;c++) for(d=0;d<=9;d++) for(e=0;e<=9;e++) { if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e) { sum1=a*10000+b*1000+c*100+d*10+e; sum=sum1*n; x[0]=a;x[1]=b;x[2]=c;x[3]=d;x[4]=e; int i=9,k=1; int s=sum; while(sum) { x[i]=sum%10; i--; sum/=10; k++; } if(sum==0&&k==6) { sort(x,x+10); for(i=1;i<10;i++) if(x[i-1]==x[i]) break; if(i==10) {printf("%05d / %05d = %d\n", s, sum1, n); ok=1;} } } } if(ok==0) printf("There are no solutions for %d.\n", n); } return 0; }
时间: 2024-10-12 16:22:15