C++——列举亲密数
来源:互联网 发布:js async false 编辑:程序博客网 时间:2024/06/18 17:22
描述:
两个整数a和b,如果a的不包含自身的因子之和等于b,并且b的不包含自身的因子和等于a,且a不等于b,则称a,b为一对亲密数。
找出满足a<=10000且b<=10000的全部亲密数对。
A pair of close numbers(a and b) is defined as follows: the sum of a’s factors equals b and the sum of b’s factors equals a,but a does not equals b.Find all close numbers which are not greater than 10000.
输入:
本题无输入。
None
输出:
升序输出所有满足条件的数对,每对数字一行,小数字在前,大数字在后,用空格分隔。注意:本题要求程序效率要高,直接写成二重循环肯定超时。
Output all pair of close numbers in ascending order,and each pair occupies one line with the smaller one in front and the pair is separated by a space.
答案如下:
#include<iostream>
using namespace std;
int factors(int); //自定义一个求因子和的函数
int main(){ //函数主体
int sum;
for(int a=2;a<10000;a++)
{
sum=factors(a); //引入自定义函数
if((a==factors(sum))&&a<sum) // 判断a的因子和求得的因子和是否等于a,且a小于它的因子和
{
cout<<a<<" "<<sum<<endl; //输出a,空格和a的因子
}
}
return 0;
}
int factors(int b) //自定义求因子和的函数
{
int s=b/2;
int sum=0;
for(int i=1;i<=s;i++)
{
if(b%i==0) sum+=i;
}
return sum; //返回sum
}
- C++——列举亲密数
- 助教:c/c++语言——亲密数
- 亲密数 c语言
- c语言计算亲密数
- C语言---“亲密数”问题
- 【C语言训练】亲密数
- 数学问题——找“亲密数”
- 3000以内亲密数(C++)
- 【HPU】[1014]【C语言训练】亲密数
- 1122: 【C语言训练】亲密数
- 第三十九讲项目1.3——亲密数
- 亲密数
- 亲密数
- 亲密数
- 亲密数
- 亲密数
- 亲密数
- 亲密数
- 构建工具-Maven和Gradle对比
- C++文件操作:打开文件和写入文件
- 简单介绍虚拟继承
- Hadoop介绍(二) HDFS
- get与post的区别?
- C++——列举亲密数
- P4253牛的染色体(二分答案)
- Java--IO 常用流的操作
- raspberrypi 3代B 配置摄像头
- 内联函数inline
- C#与sql的连接
- 20171011WindowsPrj08_03IAT Hook
- 文章标题
- LeetCode刷题(7)