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的作用是标记.其余就很好理解呀。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值