zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    洛谷—— P3353 在你窗外闪耀的星星
    洛谷—— P1238 走迷宫
    洛谷—— P1262 间谍网络
    9.8——模拟赛
    洛谷—— P1189 SEARCH
    算法
    May 22nd 2017 Week 21st Monday
    May 21st 2017 Week 21st Sunday
    May 20th 2017 Week 20th Saturday
    May 19th 2017 Week 20th Friday
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4696500.html
Copyright © 2011-2022 走看看