UVa Problem Solution: 10042 - Smith Numbers
来源:互联网 发布:家庭网络组网方案 编辑:程序博客网 时间:2024/06/03 02:43
Follow the problem's statements and you'll get it.
Code:
- /***************************************************************************
- * Copyright (C) 2008 by Liu Kaipeng *
- * LiuKaipeng at gmail dot com *
- ***************************************************************************/
- /* @JUDGE_ID 00000 10042 C++ "Smith Numbers" */
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- int const max_primes = 31623;
- bool primes[max_primes];
- void gen_primes()
- {
- primes[0] = primes[1] = false;
- for (int i = 2; i < max_primes; ++i)
- primes[i] = true;
- for (int i = 2; i < max_primes; ++i) {
- for (; i < max_primes && !primes[i]; ++i) {}
- for (int j = 2; i*j < max_primes; ++j)
- primes[i*j] = false;
- }
- }
- bool is_smith(int n)
- {
- int s1 = 0;
- for (int m = n; m != 0; m /= 10) s1 += m % 10;
- int s2 = 0;
- int f = 2, mf = sqrt(n) + 1;
- for (; n >= f && f < mf && s2 <= s1; ++f) {
- if (!primes[f]) continue;
- if (n % f == 0) {
- int s = 0;
- for (int m = f; m != 0; m /= 10) s += m % 10;
- for (; n % f == 0; n /= f) s2 += s;
- }
- }
- if (s2 != 0 && n != 1)
- for (int m = n; m != 0; m /= 10) s2 += m % 10;
- return s1 == s2;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
- freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
- #endif
- gen_primes();
- int ncases;
- cin >> ncases;
- for (int n; ncases > 0 && cin >> n; --ncases) {
- int m = n + 1;
- for (; !is_smith(m); ++m) {}
- cout << m << '/n';
- }
- return 0;
- }
- UVa Problem Solution: 10042 - Smith Numbers
- UVa Problem 10042 Smith Numbers (Smith 数)
- uva 10042 smith numbers
- UVa 10042 - Smith Numbers
- UVA 10042 Smith Numbers 数论
- PC/UVa 110706/10042 Smith Numbers
- UVa 10042 && POJ 1142 - Smith Numbers
- UVA 10042 Smith Numbers(数论)
- UVa Problem Solution: 10202 - Pairsumonious Numbers
- UVa Problem Solution: 10006 - Carmichael Numbers
- Smith Numbers
- Smith Numbers
- Smith Numbers
- UVa Problem Solution: 10189 - Minesweeper
- UVa Problem Solution: 10033 - Interpreter
- UVa Problem Solution: 10050 - Hartals
- UVa Problem Solution: 10149 - Yahtzee
- UVa Problem Solution: 10082 - WERTYU
- VB 驱动控制类
- JAVA基础学习篇----对象串行化及Transient关键字的使用
- 二、工厂方法模式
- 第一次写
- 三、抽象工厂模式
- UVa Problem Solution: 10042 - Smith Numbers
- 四、单例模式
- 无线网卡
- A Recipe for OpenID-Enabling Your Site
- 兴建平潭海峡大桥的历程
- 佐丹奴公司电子商务整合案例分析
- IT及软件行业三特点
- Linux中ipv6代码阅读(2)
- C/C++/C#考试面试题目