zoukankan      html  css  js  c++  java
  • hdu 1271 模拟

    这题还是不错了,刚看到这题的时候感觉是不会,但是后来用笔算出了几个数的结果之后,发现这种手算的方法可以用程序模拟出来,也就是首先穷举A的第K位被抽掉,那么就可以把A分成三部分,K位之前的部分a,第K位b和第K位之后的c,于是c只有两种情况,进位或者不进位,而b也只有两种情况,进位或不进位,判断一下就可以了。

    /*
     * hdu1271/win.cpp
     * Created on: 2012-10-24
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    set<int> ans;
    void work(int N, int k) {
        int a, b, c, aa, bb, cc;
        int t = (int)pow(10.0, k);
        aa = N / t;
        t /= 10;
        bb = (N / t) % 10;
        cc = N % t;
        c = cc / 2;
        for(b = 0; b <= 9; b++) {
            a = (10 * aa + bb - b) / 11;
            if(a * t * 10 + b * t + c + a * t + c == N) {
                if(a == 0 && b == 0) {
                    continue;
                }
                ans.insert(a * t * 10 + b * t + c);
            }
        }
        c = (cc + t) / 2;
        for(b = 0; b <= 9; b++) {
            a = (10 * aa + bb - b - 1) / 11;
            if(a * t * 10 + b * t + c + a * t + c == N) {
                if(a == 0 && b == 0) {
                    continue;
                }
                ans.insert(a * t * 10 + b * t + c);
            }
        }
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.in", "r", stdin);
    #endif
        int N;
        char str[200];
        while(scanf("%d", &N) == 1 && N > 0) {
            ans.clear();
            sprintf(str, "%d", N);
            int len = strlen(str);
            for(int k = 1; k <= len; k++) {
                work(N, k);
            }
            if(ans.size() <= 0) {
                puts("No solution.");
            }else {
                set<int>::iterator it = ans.begin();
                printf("%d", *it);
                for(it++; it != ans.end(); it++) {
                    printf(" %d", *it);
                }
                putchar('\n');
            }
        }
        return 0;
    }
  • 相关阅读:
    (Java实现) 装载问题
    (Java实现) 子集和问题
    (Java实现) 子集和问题
    (Java实现) 整数区间
    (Java实现) 车厢重组
    (Java实现) 车厢重组
    (Java实现) 车厢重组
    (Java实现) 车厢重组
    delphi 程序窗体及控件自适应分辨率(通过ComponentCount遍历改变字体大小以及上下左右)
    后台开发:核心技术与应用实践(边写代码边读书才是最好的学习方式)
  • 原文地址:https://www.cnblogs.com/moonbay/p/2739976.html
Copyright © 2011-2022 走看看