UVA 725 Divison暴力入门

Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where 2 ≤ N ≤ 79. That is, abcde fghij = N where each letter represents a different digit. The first digit of one of the numerals is allowed to be zero.

Input

Each line of the input file consists of a valid integer N. An input of zero is to terminate the program。

Output Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and, of course, denominator). Your output should be in the following general form: xxxxx / xxxxx = N xxxxx / xxxxx = N . . In case there are no pairs of numerals satisfying the condition, you must write ‘There are no solutions for N.’. Separate the output for two different values of N by a blank line.

Sample Input

61 62 0

Sample Output

There are no solutions for 61.

79546 / 01283 = 62

94736 / 01528 = 62

具体题目意思就是abcde/fghij=n并且n>=2;abcde,fghiij均可以有前导0,所以被除数abcde不可以有前导0,因为这样的话就不满足题意n>=2,想要知道abcde,只需要枚举fghij就可以。这是对题目题意的一个分析,其次要知道这十个数每个数字只能出现一次,不能有重复现象,这就想到存放在字符串数组里面,再定义一个目标数组,排序后进行比较,看是否相等。可以想到两个函数sprintf,sort函数。另外需要注意输出,输出的空格出现位置。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ans[11]="9876543210";
char s[11];
bool cmp(char a,char b)
{
  return a>b;
}
int main()
{
  int n,pro,m=0;
  bool flag=false;
  while(scanf("%d",&n)!=EOF)
  {
    if(n==0) break;
    if(m>0) printf("\n");
    m++;
    flag=false;
    for(int i=1234;i<=50000;i++)
    {
      pro=n*i;//pro代表被除数
      if(pro>98765)  break;
      if(i<10000)
        sprintf(s,"%d%d%d",0,i,pro);//把整数0,i,pro
        //打印成字符串保存在s中
      else
        sprintf(s,"%d%d",i,pro);
      sort(s,s+10,cmp);
      if(strcmp(s,ans)==0)
      {
        printf("%d / %05d =%d\n",pro,i,n);
        //%05d表示输出的宽度至少为5位,不够用0来补
        //%5d表示宽度至少为5位,位数大于5输出实际值
        flag=true;
      }
    }
    if(!flag)
    {
      printf("There is no solutions for %d.\n",n);
    }
  }
  return 0;
}

就是这种写法。flag的作用是标记.其余就很好理解呀。

猜你喜欢

转载自blog.csdn.net/cjh1459463496/article/details/86595839