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
  • 相关阅读:
    源码搭建zabbix平台
    如何用手机远程控制自己的电脑
    nginx无法启动问题详解
    在<script>中加HTML注释标签<!-- --> 的原因?
    UNICODE,GBK,UTF-8区别
    dart学习一 windows环境安装dart环境
    Git使用方法(精心整理,绝对够用)
    非常好用的jQuery表格排序插件
    Redis NOAUTH Authentication required.
    Docker概念、安装
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4696500.html
Copyright © 2011-2022 走看看