輸入正整數n,按從小到大的順序輸出所有形如abcde / fghij = n的表達式,其中a~j恰好爲數字0~9的一個排列(可以有前導0),2≤n≤79
input:
62
output:
79546 / 01293 = 62
94736 / 01528 = 62
書上示例:
#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;
}
int sprintf( char *buffer, const char *format, [argument] … );
%0nd 表示輸出的整型寬度至少爲n位,不足n位用0填充