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;
}
posted @ 2012-10-25 19:17  moonbay  阅读(159)  评论(0编辑  收藏  举报