Smith Numbers(分解质因数)
|
Smith Numbers
Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
4937775= 3*5*5*65837 The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers. As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition. Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775! Input The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.
Output For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.
Sample Input 4937774 Sample Output 4937775 Source |
AC代码:
1 #include<iostream>
2
3 using namespace std;
4
5 int CalDigitsSum(int num)
6 {
7 int sum = 0;
8 while(num)
9 {
10 sum += num % 10;
11 num /= 10;
12 }
13 return sum;
14 }
15
16 int PrimaryCal(int num)
17 {
18 int total = 0;
19 int tempNum = num;
20 for(int i = 2; i * i <= num; i++)
21 {
22 int temp;
23 if(num % i == 0)
24 temp = CalDigitsSum(i);
25 while(num % i == 0)
26 {
27 total += temp;
28 num /= i;
29 }
30 }
31 if(tempNum == num)
32 return -1;
33 if(num != 1)
34 total += CalDigitsSum(num);
35 return total;
36 }
37
38 int main()
39 {
40 int n;
41 while(1)
42 {
43 cin >> n;
44 if(n == 0)
45 break;
46 for(int i = n + 1; ; i++)
47 {
48 if(CalDigitsSum(i) == PrimaryCal(i))
49 {
50 cout << i << endl;
51 break;
52 }
53 }
54 }
55 return 0;
56 }
Smith Numbers(分解质因数)的更多相关文章
- POJ 1142:Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- POJ 1142 Smith Numbers(分治法+质因数分解)
http://poj.org/problem?id=1142 题意: 给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加. 思路:直接暴力. #include <ios ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- Smith Numbers POJ - 1142 (暴力+分治)
题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...
- C语言程序设计100例之(5):分解质因数
例5 分解质因数 题目描述 将一个正整数分解质因数.例如:输入90,输出 90=2*3*3*5. 输入 输入数据包含多行,每行是一个正整数n (1<n <100000) . 输出 对 ...
- java分解质因数
package test; import java.util.Scanner; public class Test19 { /** * 分析:对n进行分解质因数,应先找到一个最小的质数k * 最小 ...
- 程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)
1 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. ...
- 【python】将一个正整数分解质因数
def reduceNum(n): '''题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5''' print '{} = '.format(n), : print 'Pleas ...
- light oj 1236 分解质因数
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H 题意:求满足1<=i<=j<=n ...
随机推荐
- Go | Go 结合 Consul 实现动态反向代理
Go 结合 Consul 实现动态反向代理 代理的核心功能可以用一句话概括:接受客户端的请求,转发到后端服务器,获得应答之后返回给客户端. Table of Contents 反向代理 实现逻辑 Go ...
- abp中多种登陆用户的设计
项目地址:https://gitee.com/bxjg1987/abp 场景 在<学校管理系统>中,学生.家长.教师.教务都可能登陆,做一些属于他们自己的操作.这些用户需要的属性各不相同, ...
- 测试成长记录:python调adb无法获取设备信息bug记录
背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...
- Python爬虫学习笔记(四)
Request: Test1(基本属性:POST): 代码1: import requests # 发送POST请求 data = { } response = requests.post(url, ...
- 从代理模式 到 SpringAOP
前言 Spring AOP 就是通过代理模式来实现切面编程的.代理模式用来为其他对象提供一种代理,以控制对这个对象的访问. 代理对象在客户端和目标对象之间起到中介的作用.通过控制对这个对象的访问,可以 ...
- 鹅厂二面,nginx回忆录
前天二面鹅厂,面试官问出了"nginx你了解吗?"这样宽泛直白的句式,我一时抓不到重点,一时语噻. 下班想了一下,平时潜移默化用到不少nginx的能力,但在面试的时候没有吹成对应的 ...
- C# 应用 - 使用 HttpWebRequest 发起 Http 请求
helper 类封装 调用 1. 引用的库类 \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll Syste ...
- Mysql被黑客攻击了?一定要注意一点,不要关闭mysql服务
因为mysql没有关闭的情况,可以从缓存里面获取到数据,如果关闭了只能从文件里面去获取数据了,会大大加大恢复难度
- key解析
密钥在不同实体之间传递,因此密钥必须可以序列化. 所有密钥三个特性: 算法:密钥使用的算法,如DES和DSA等,通过getAlgorithm()获取算法名 编码形式:密钥的外部编码形式,如X.509, ...
- SEO优化基础知识
一.标点符号的重要性 很多人忽略了标点符号对爬虫的重要性,爬虫并不是对所有标点符号都爬取,下面列举几个对关键字分隔有帮助的符号. 1.1.逗号( , ) ==> 千万千万要使用英文的逗号,而不是 ...