Division UVA - 725

—> Division UVA - 725
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

char ans[11] = "0123456789";
char a[11];

int main()
{
	int n;
	int cnt = 0;
	while(scanf("%d", &n)!=EOF&&n)
	{
		if(cnt)
			printf("\n");
		cnt++;    //输出换行的技巧
		 
		bool flag = false;
		
		for(int fghij=1234;;fghij++)  //fghij从01234开始枚举 
		{
			int abcde = fghij*n;
			if(abcde>98765)  break;
			
			sprintf(a,"%05d%05d",abcde,fghij);  //数字转化成字符串
			
			sort(a,a+10);   //从小到大排序
			if(strcmp(a,ans)==0)   //与0123456789比较
			{
				flag = true;
				printf("%05d / %05d = %d\n", abcde, fghij, n);
			}
		}
		
		if (!flag) 
			printf("There are no solutions for %d.\n", n);	
			
	}

	return 0;
}

即使采用暴力枚举,也是需要认真分析问题的。
01234~98765

  1. “The first digit of one of the numerals is allowed to be zero.” 题意,两个五位数都可以有前导零。
  • 我们分析可知,被除数是不可能前导零的。证明很容易,假设被除数有前导零,说明被除数只是四位数,那么除数必须要五位数,很明显不合题意,因为N>=2;

  • 由此,可以把含有前导零的除数和不含有前导零的除数分开处理。
    但是我程序里的被除数和除数都加了前导零, 即便误加了的,也会判断出错误。

  1. “Separate the output for two different values of N by a blank line.”输入输出很严格,掌握输出换行的技巧
  2. 数字转化成字符串
    sprintf(a, “%05d%05d”, abcde, fghij);
  3. sort也可以对字符数组排序
    sort(a,a+10);
  4. int strcmp( const char *lhs, const char *rhs );
    若字典序中 lhs 先出现于 rhs 则为负值。
    若 lhs 与 rhs 比较相等则为零。
    若字典序中 lhs 后出现于 rhs 则为正值。

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/88098412
今日推荐