zoukankan      html  css  js  c++  java
  • 7.1 简单枚举 (UVa 725, 11059, 10976)

    在进行枚举前,对问题进行一定分析会使枚举的次数少很多。

    题目:

    UVa 725

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

    思路:枚举第二个数字,算出第一个数字,判断是否有重复的数字。如果算出的第一个数字已经是六位数了,则可以停止枚举了。

    代码:

    /* Division (UVa 725) */
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    bool isOk(long long num);            //每一个数字都不同则返回 true 
    
    int vis[130];
    
    int main(){
        
        int N;
        int flag = false;
        while(cin>>N && N!=0){
            
            if(flag)
                cout << endl;
            
            bool exists = false; 
            
            for(int i=1234; i<=98765; i++){
                
                if(isOk(i)){
                    
                    long long num = N * i;
                    
                    if(num > 99999){        //第一个数肯定是五位数 
                        break;
                    }
                    
                    if(num < 10000){
                        continue;
                    }
                    
                    if(isOk(num*100000 + i)){
                        cout << num << " / ";
                        if(i < 10000)
                            cout << "0";
                        cout << i << " = " << N << endl;
                        exists = true; 
                    }    
                }
            }
            
            if(!exists){
                cout << "There are no solutions for " << N << "." << endl;
            }
            
            flag = true;
        }
        
    }
    
    bool isOk(long long num){
        
        char s[30];
        int vis[30];
        memset(vis, 0, sizeof(vis));
        sprintf(s, "%lld", num);
        
        for(int i=0; i<strlen(s); i++){
            if(vis[s[i] - '0'] == 1)            //如果这个数字之前出现过 
                return false;
            vis[s[i] - '0'] = 1;                //标记出现过 
        }
        
        return true;
    }

    UVa 11059

    思路:枚举出子序列的起点和终点,时间为O(N^2)

    代码:

    /* Maximum Product (UVa 11059) */
    #include <iostream>
    using namespace std;
    
    int main(){
        
        //freopen("input.txt", "r", stdin);
        
        int n;
        long long num[20];
        int T = 0; 
        while(cin >> n){
            
            for(int i=0; i<n; i++){
                cin >> num[i];
            }    
            
            long long maximum = 0;
            
            for(int i=0; i<n; i++){
                long long now = num[i];
                if(now > maximum)        //有可能一个数字是最大的 
                    maximum = now;
                for(int j=i+1; j<n; j++){
                    now *= num[j];
                    if(now > maximum)
                        maximum = now;
                }
            }
                
            cout << "Case #" << ++T << ": " << "The maximum product is " << maximum << "." << endl << endl;
            
        }
        
        return 0;
        
    }

    UVa 10976

    思路:y从 k+1 枚举到 2K ,算出 x 若为正数,则将其保存在一个临时队列中,最后一起输出。

    代码:

    /* Fractions Again (UVa 10976) */
    #include <iostream>
    #include <sstream>
    #include <queue>
    using namespace std;
    
    int main(){
        
        int k;
        while(cin >> k){
            
            int x, y;
            int cnt = 0;
            queue<string> q;
            
            for(y=k+1; y<=2*k; y++){            // y 从 k+1 枚举到 2k 
                if((-(k*y)) % (k-y) == 0){        // 如果可以整除,则 x 为整数 
                    x = (-(k*y)) / (k-y);
                    stringstream ss;
                    ss <<  "1/" << k << " = " << "1/" << x << " + " << "1/" << y;
                    string s;
                    getline(ss, s);
                    q.push(s);                //输出暂存于队列中 
                    cnt++;
                }
            }
            
            cout << cnt << endl;
            for(int i=0; i<cnt; i++){
                cout << q.front() << endl;
                q.pop();
            }
        }
        
        return 0;
        
    }
  • 相关阅读:
    以太坊公开拍卖智能合约案例
    部署智能合约
    ERC20 Token
    查看crontab运行状态
    php把excel数值格式转成日期格式
    PHP识别中文编码并自动转换为UTF-8
    python中文件内容替换
    mysqldump备份指定的数据
    linux设置服务器时间
    git常用命令
  • 原文地址:https://www.cnblogs.com/lighter-blog/p/6527715.html
Copyright © 2011-2022 走看看