输入正整数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] … );
buffer:
char型指针,指向将要写入的字符串的缓冲区。
format:格式化字符串。
[argument]..
.:可选参数,可以是任何类型的数据。
%nd 表示输出的整型宽度至少为n位,右对齐,%5d即宽度至少为5位,位数大于5则输出实际位数
%0nd 表示输出的整型宽度至少为n位,不足n位用0填充