博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
阅读量:7223 次
发布时间:2019-06-29

本文共 1198 字,大约阅读时间需要 3 分钟。

Regionals 2000 >> Europe - Mid-Central

Regionals 2000 >> Europe - Northwestern
Regionals 2000 >> Europe - Southwestern

问题链接:。

题意简述:寻找大于输入数并且最接近的Smith Numbers。

问题分析

Smith Numbers数不是素数,其各位数字之和等于其各个因子的各位数字之和。。

程序说明

该问题与《》是同一个问题,只是输入形式不同

函数digitsum()用于计算数的各位数字之和。

函数fact_digitsum用于计算数的各个因子的数字之和,但是若为素数则返回-1。函数中对因子的数量进行了计数,若为1则数为素数。

相关链接

AC的C++语言程序如下

/* UVALive2203 UVa10042 Smith Numbers */#include 
using namespace std;const int BASE10 = 10;inline int digitsum(int n){ int sum = 0; while(n) { sum += n % BASE10; n /= BASE10; } return sum;}int fact_digitsum(int n){ int digitsum2 = 0, count = 0; for(int i=2; i*i<=n; i++) { while(n % i == 0) { count++; digitsum2 += digitsum(i); n /= i; } } if(n > 1 && count) digitsum2 += digitsum(n); if(count == 0) digitsum2 = -1; return digitsum2;}int main(){ int t, n; cin >> t; while(t--) { cin >> n; for(n++; ; n++) { if(digitsum(n) == fact_digitsum(n)) { cout << n << endl; break; } } } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563758.html

你可能感兴趣的文章
Ubuntu上安装TensorFlow(python2.7版)
查看>>
Fast-Track之Microsoft SQL 注入篇
查看>>
MySQL year函数
查看>>
Hadoop开发者入门专刊(pdf)
查看>>
linux文件查找利器 非find莫属
查看>>
Unity3D插件大全
查看>>
c++中打乱数组的顺序输出
查看>>
极速理解设计模式系列:8.策略模式(Strategy Pattern)
查看>>
Hyper-V Server --SMB3.0
查看>>
IT草根的江湖之路之五:鉴于现实,屈服!
查看>>
编译报错 /usr/bin/ld: cannot find -lc 解决
查看>>
系统自带的系统工具
查看>>
浅谈专心只学一门C#的优缺点[邀月补充:一精胜于十专]
查看>>
UWA资源检测与分析支持Unity 5.3!
查看>>
IOT
查看>>
记录一次raid故障后的恢复和回迁数据全过程
查看>>
单臂路由的配置
查看>>
Operations Manager 2007 R2系列之邮件通知
查看>>
被动DNS
查看>>
需求管理之客户需求何时休?
查看>>