例题 7-1 除法(Division,Uva 725)

题目

UVa 725原题题目

AC代码

#include <iostream>
#include <cstring>

using namespace std;

int Judge(int a, int b) {
	int temp[10];
	memset(temp, 0, sizeof(temp));
	int mm = 0;
	while(1) {
		temp[mm] = a % 10;
		a /= 10;
		mm++;
		if (a == 0) break;
	}
	while(1) {
		temp[mm] = b % 10;
		b /= 10;
		mm++;
		if (b == 0) break;
	}
	for (int i = 0; i < 10; i++) {
		for (int  j = i+1; j < 10; j++) {
			if (temp[i] == temp[j]) return 0;
		}
	}
	return 1;	//ok 
}

int main()  {
	int n;
	int number = 0;
	while (cin >> n && n) {
		if (number > 0) cout << endl;
		number++;
		bool is_empty = true;
		for (int i = 1e4; i < 1e5; i++) {
			//若不能整除,直接继续遍历 
			if (i % n != 0) continue;
			int div = i / n;
			//分子最小只能四位数 
			if (div < 1000) continue;
			if (Judge(i,div)) {
				printf("%05d / %05d = %d\n", i,div,n);
				is_empty = false;
			} 
		} 
		if (is_empty) printf("There are no solutions for %d.\n", n);
	}
	return 0;
}

说两句

采用逐一枚举的傻方法,暴力解题。一直PE了半小时,ac不了,原来是Output中的空行问题。空行要在第二组测试实例N之后开始打,因为存在只有一组测试实例的情况,此时,第一组输出结束,打印了空行,这个空行就是多余的了,也就导致了Presentation Error。

作者刘汝佳的代码

世界上最大的TongXing交友网站传送门

我从他的源码中学到了 sprintf()、%0nd、sort(),开心。还有,同样是暴力解题法,他的算法比我的帅多了。还有还有,C++的轮子sort()真的好强!!!
用心去看别人的源码,真的能学到很多!!!:)

END
———————
再稍稍小坐片刻
也可以哟
——直到日落
———————

发布了41 篇原创文章 · 获赞 2 · 访问量 1643

猜你喜欢

转载自blog.csdn.net/qq_44296342/article/details/104993021
今日推荐