hdu 1333水题

刚看的时候还以为挺难的,试了一下最水的方法,居然过了。就是从N以后往下试,试到smith数停止就行,而且对于smith数的判断也可以简单处理,循环到sqrt(n)即可,唉,这种简单方法我这次现场赛咋没想到呢……

/*

 * hdu1333/win.cpp

 * Created on: 2012-10-27

 * 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;

int getbitsum(int n) {

    int ret = 0;

    while(n > 0) {

        ret += n % 10;

        n /= 10;

    }

    return ret;

}

bool isSmith(int n) {

    int sum1 = getbitsum(n);

    int sum2 = 0;

    int sqrtn = (int)sqrt(n);

    bool isprime = true;

    for(int i = 2; i <= sqrtn; i++) {

        while(n % i == 0) {

            isprime = false;

            sum2 += getbitsum(i);

            n /= i;

        }

        if(n == 1) {

            break;

        }

    }

    if(n > 1) {

        sum2 += getbitsum(n);

    }

    return sum1 == sum2 && !isprime;

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int n;

    while(scanf("%d", &n) == 1 && n > 0) {

        do{

            n++;

        }while(!isSmith(n));

        printf("%d\n", n);

    }

    return 0;

}

你可能感兴趣的:(HDU)