UVA725 Division 除法【暴力】

题目链接>>>>>>

 

题目大意:
给你一个数n(2 <= n <= 79),将0-9这十个数字分成两组组成两个5位数a, b(可以包含前导0,如02345也算),使得a / b = n;列出所有的可能答案。

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>

using namespace std;

int main()
{
    int n, a[10], first = 1;
    while (scanf("%d", &n) != EOF,n) {
        if (first) {
            first = 0;
        }
        else {
            printf("\n");
        }                                //以上是两组数据之间输出空行的技巧
        int num1 = 1, num2 = 1;
        int side = 98765 / n;           //这里稍微降低了一下复杂度
        int flag1 = 0;
        for (num1 = 1234; num1 <= side; num1++) {
            int flag = 1;
            num2 = num1 * n;
            a[0] = num2 / 10000;
            a[1] = num2 / 1000 % 10;
            a[2] = num2 / 100 % 10;
            a[3] = num2 / 10 % 10;
            a[4] = num2 % 10;
            a[5] = num1 / 10000;
            a[6] = num1 / 1000 % 10;
            a[7] = num1 / 100 % 10;
            a[8] = num1 / 10 % 10;
            a[9] = num1 % 10;
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    if (j != i && a[i] == a[j]) {            //这里判断10位数是否有重复的方法
                        flag = 0;
                    }
                }
            }
            if (flag) {
                for (int i = 0; i < 5; i++)cout << a[i]; cout << " / ";                        //注意这里"/"和"="左右两边都有空格
                for (int i = 5; i < 10; i++)cout << a[i]; cout << " = " << n << endl;
                flag1 = 1;
            }
        }
        if (flag1 == 0) {
            printf("There are no solutions for %d.\n", n);
        }
    }
    return 0;
}

 

2018-04-08

posted @ 2018-04-08 23:25  悠悠呦~  阅读(258)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end