例题7-1 除法

输入正整数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;
}


枚举,题上求除法,算法可用乘法反求结果,避免出现小数。fghij从1234开始(01234),直到abcde fghij的总长>10停止枚举,中间用排序,方便 重复 数字的检测


int sprintf( char *buffer, const char *format, [argument] … );

bufferchar型指针,指向将要写入的字符串的缓冲区。
format:格式化字符串。
[argument].. .:可选参数,可以是任何类型的数据。

%nd 表示输出的整型宽度至少为n位,右对齐,%5d即宽度至少为5位,位数大于5则输出实际位数
%0nd 表示输出的整型宽度至少为n位,不足n位用0填充


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值