两种用c语言解决亲密数问题的方法并比较程序运行时间
来源:互联网 发布:如何重新设置网络连接 编辑:程序博客网 时间:2024/06/05 07:12
Problem K
亲密数(close numbers)
时限:2000ms 内存限制:10000K 总时限:2000ms
描述:
两个整数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<stdio.h>#include<cmath>#include<math.h>#include<time.h>int f(int x ){//方法一: 一般方法 int i = 1; int sum =0 ; for (;i <x ;i++){ if(x%i==0){ sum = sum + i; } } return sum;}int f2(int x ){//方法二:利用根号节省程序运行时间 int i = 2; int sum =1 ; for (;i <=sqrt(x) ;i++){// 范围与第九行写的范围不一样 if(x%i==0){ sum = sum + i; //如果这个数值是因子的话 那个数字除以这个因子也是这个数的因子 if(i != sqrt(x)) sum = sum + x/i; } } return sum;}int main(){ clock_t start = clock(); for(int i = 1 ; i < 10000 ;i++){ int xx = f(i); if( i == f(xx) && i < xx) printf("%d %d\n", i ,xx); } clock_t finish = clock(); printf("花费的时间是: %lf ms\n",(double)(finish - start)/CLOCKS_PER_SEC); // cout<<"Time:"<<<<"ms"<<endl;//计算程序运行时间 start = clock(); for(int i = 1 ; i < 10000 ;i++){ int xx = f2(i); if( i == f2(xx) && i < xx) printf("%d %d\n", i ,xx); } finish = clock(); printf("花费的时间是: %lf ms\n",(double)(finish - start)/CLOCKS_PER_SEC); return 0;}
阅读全文
1 0
- 两种用c语言解决亲密数问题的方法并比较程序运行时间
- C语言---“亲密数”问题
- 亲密数 c语言
- c语言计算程序运行时间的方法
- 计算程序的运行时间(c语言)
- C 语言计算程序运行的时间
- C语言查看程序运行的时间
- c语言计算亲密数
- 【C语言训练】亲密数
- C语言 特殊数问题 回文数 素数 亲密数 水仙花数
- 【HPU】[1014]【C语言训练】亲密数
- 1122: 【C语言训练】亲密数
- UltraEdit编译C语言程序并运行的示例
- C语言中的测量程序运行时间的函数
- 用C语言计算程序的运行时间
- C 语言基础——程序运行时间的计算
- c语言获取程序运行的时间例子
- C语言中如何计算程序运行的时间
- codeforces 245H 区间dp
- JDK9新特性
- VMware永久许可证密钥
- JSONP 跨域读取数据
- jvm系列(四):jvm调优-命令篇
- 两种用c语言解决亲密数问题的方法并比较程序运行时间
- Tensorflow Example Resources
- Linux下用户登录查看常用命令
- 六度空间
- 请大神讲解
- 双链表的基本使用及其应用(实验2.2)
- 解决Pr, AE在渲染时输出出错的问题
- BZOJ 4562: [Haoi2016]食物链 拓扑排序+DP(其实是递推)
- C++中引用(&)的用法和应用实例