輸入N,求 N = abcde / fghij
而a~j為0~9的數字,互相不重複
每兩個輸出間要空行,而最後以0結束時又不能多空行
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int N,a,b; // a / b
int main(){
scanf("%d",&N);
while(1){
int ok=0;
if(N==0)break;
else {
for(int k=0;k<100000;k++){
int b1 = k/10000,
b2 = k%10000/1000,
b3 = k%10000%1000/100,
b4 = k%10000%1000%100/10,
b5 = k%10000%1000%100%10;
//printf("%d %d %d %d\n",b1,b2,b3,b4);
if(b1!=b2 && b1!=b3 && b1!=b4 && b1!=b5 && b2!=b3 && b2!=b4 && b2!=b5
&& b3!=b4 && b3!=b5 && b4!=b5){
b = k;
//printf("%d ",b);
a = b*N;
if(a/10000 >=10)break;
else {
int a1 = a/10000,
a2 = a%10000/1000,
a3 = a%10000%1000/100,
a4 = a%10000%1000%100/10,
a5 = a%10000%1000%100%10;
if( a1!=a2 && a1!=a3 && a1!=a4 && a1!=a5
&& a1!=b1 && a1!=b2 && a1!=b3 && a1!=b4 && a1!=b5
&& a2!=a3 && a2!=a4 && a2!=a5
&& a2!=b1 && a2!=b2 && a2!=b3 && a2!=b4 && a2!=b5
&& a3!=a4 && a3!=a5
&& a3!=b1 && a3!=b2 && a3!=b3 && a3!=b4 && a3!=b5
&& a4!=a5 && a4!=b1 && a4!=b2 && a4!=b3 && a4!=b4 && a4!=b5
&& a5!=b1 && a5!=b2 && a5!=b3 && a5!=b4 && a5!=b5
)
{
printf("%05d / %05d = %d\n",a,b,N);
ok=1;
}
}
}
}
if(ok==0)printf("There are no solutions for %d.\n",N);
}
scanf("%d",&N);
if(N==0)break; else puts("");
}
return 0;
}
Ruija Liu 的神奇寫法
將運算式轉為字串
按照大小排序後,直接檢查0~9是否有缺數字
即可作為題目限制的判定
// UVa725 Division
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
int n, kase = 0;
char buf[99];
while(scanf("%d", &n) == 1 && n) {
int cnt = 0;
if(kase++) printf("\n");
for(int fghij = 1234; ; fghij++) {
int abcde = fghij * n;
sprintf(buf, "%05d%05d", abcde, fghij);
if(strlen(buf) > 10) break;
sort(buf, buf+10);
bool ok = true;
for(int i = 0; i < 10; i++)
if(buf[i] != '0' + i) ok = false;
if(ok) {
cnt++;
printf("%05d / %05d = %d\n", abcde, fghij, n);
}
}
if(!cnt) printf("There are no solutions for %d.\n", n);
}
return 0;
}
沒有留言:
發佈留言