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 ...
随机推荐
- 《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤
简介 <Asp.Net Core3 + Vue3入坑教程> 此教程仅适合新手入门或者前后端分离尝试者.可以根据图文一步一步进操作编码也可以选择直接查看源码.每一篇文章都有对应的源码 教程后 ...
- STM32学习笔记——序言
写AVR已经两年了.如果初中时候玩Arduino也算的话,就是6年. 两年以来,我用AVR单片机完成了两个大项目: AVR单片机教程,一时兴起写的,效果不好: MEDS,参赛用的课题,半完成,比赛都结 ...
- 关于Java中for,while,if,方法的练习
练习 计算0到100之间的奇数和偶数和 package com.kangkang.forDemo;public class demo01 { public static void main(S ...
- Blackduck的Hub安装教程
1 产品介绍 Black Duck 是最早进行开源代码检测工具开发的公司,其产品包括Protex 和HUB,Protex 强调检测的精度和准确性,而HUB 强调检测的速度和易用性. 1.1 Prote ...
- 剑指 Offer 14- II. 剪绳子 II + 贪心 + 数论 + 快速幂
剑指 Offer 14- II. 剪绳子 II 题目链接 因为有取模的操作,动态规划中max不能用了,我们观察:正整数从1开始,但是1不能拆分成两个正整数之和,所以不能当输入. 2只能拆成 1+1,所 ...
- Bullet碰撞检测
DBVT 在bullet 引擎中是很基础且重要的一个数据结构,本质上是一个可以动态更新的AABB树. 碰撞响应的分析 约束分类:可积约束,不可积约束 ,摩擦力(见[1]第四章) 整个bullet在动力 ...
- [GXYCTF2019]Ping Ping Ping 1
进入界面 根据提示进行ping信号 看到网页的内容就想到经典的Linux命令执行,使用命令执行的管道符 " | "尝试列出文件 FLAG应该在Flag.php里面 构造play ...
- Java实现解压缩文件和文件夹
一 前言 项目开发中,总会遇到解压缩文件的时候.比如,用户下载多个文件时,服务端可以将多个文件压缩成一个文件(例如xx.zip或xx.rar).用户上传资料时,允许上传压缩文件,服务端进行解压读取每一 ...
- 用 Numba 加速 Python 代码
原文出自微信公众号:Python那些事 一.介绍 pip install numba Numba 是 python 的即时(Just-in-time)编译器,即当你调用 python 函数时,你的全部 ...
- 2.pandas常用读取
一.文本读写 名称 接收 代表(含义) 默认 filepath string 文件路径 无 sep string 分割符 ',' header Int/sequence 某行做列名 infer自动寻找 ...