zoukankan      html  css  js  c++  java
  • UVa 725 除法

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666

    题意:输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列。

    思路:暴力求解,从1234开始一直到50000检验一遍即可。

     1 #include<iostream>
     2 #include<iomanip>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 int tot;
     7 int n;
     8 int used[10];
     9 int kase = 0;
    10 
    11 
    12 bool judge(int ans, int z)
    13 {
    14     memset(used, 0, sizeof(used));
    15     for (int i = 0; i < 5; i++)
    16     {
    17         used[ans % 10] = 1;
    18         used[z % 10] = 1;
    19         z /= 10;
    20         ans /= 10;
    21     }
    22     int sum = 0;
    23     for (int i = 0; i < 10; i++)
    24         sum += used[i];
    25     if (sum == 10)
    26     {
    27         tot++;
    28         return true;
    29     }
    30     return false;
    31 }
    32 
    33 void print_ans(int ans ,int z)
    34 {
    35     if (z> 98765) return;
    36     if (judge(ans, z))
    37     {
    38         //printf("%05d / %05d = %d
    ", z, ans, n);
    39         cout << setfill('0') << setw(5) << z << " / " << setfill('0') << setw(5) << ans << " = " << n << endl;
    40     }
    41 }
    42 
    43 void solve()
    44 {
    45     for (int i = 1234; i <= 50000; i++)
    46     {
    47         int z = i*n;
    48         print_ans(i, z);
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     while (cin >> n && n)
    55     {    
    56         if(kase++)  cout<<endl;
    57         memset(visited, 0, sizeof(visited));
    58         tot = 0;
    59         solve();
    60         if (tot == 0)
    61         {
    62             cout << "There are no solutions for " << n << "." << endl;
    63         }
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    倍增
    「BZOJ 2152」聪聪可可
    「POJ 1741」Tree
    点分治
    高斯消元
    网络流24题之餐巾计划问题
    网络流24题之骑士共存问题
    网络流24题之方格取数问题
    网络流24题之负载平衡问题
    网络流24题之分配问题
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6287639.html
Copyright © 2011-2022 走看看