• uva 725 Division(暴力枚举) 解题心得


    原题:

    Description

    Download as PDF
     

    Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9once each, such that the first number divided by the second is equal to an integer N, where $2
le N le 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
    

     分析:虽然是采用暴力的方法,但是也要有技巧的暴力, 并不是无脑暴力,只要暴力前5个数的可能,就能根据n的值推断出后5个数

      ,再进行判断数字有没有重复就可以了,最后还要注意输出的格式,空位用‘0’填充

    我的代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int main() {
        int n, kase = 0;
        char buf[99];
        while (scanf("%d", &n) == 1 && n) {
            int cnt = 0;
            if (kase++) printf("
    ");
            for (int fghij = 1234;; fghij++) {
                int abcde = fghij * n;
                sprintf(buf, "%05d%05d", abcde, fghij);
                if (strlen(buf) > 10) break;
                sort(buf, buf + 10);
                bool ok = true;
                for (int i = 0; i < 10; i++)
                if (buf[i] != '0' + i) ok = false;
                if (ok) {
                    cnt++;
                    printf("%05d / %05d = %d
    ", abcde, fghij, n);
                }
            }
            if (!cnt) printf("There are no solutions for %d.
    ", n);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    MVC模式的学生信息增删改查
    常用排序算法
    2803 爱丽丝·玛格特罗依德
    3118 高精度练习之除法
    中秋练习题
    poj2011
    P1558 色板游戏
    P1830 轰炸III
    P1656 炸铁路
    1067 机器翻译
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4696500.html
Copyright © 2020-2023  润新知