回复 18楼 巧若拙
^_^ 我改了一下,又快了20倍,一百万以内只需要0.124秒,一千万以内时间长了点,要4.312秒程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main( void ) { //unsigned n; //printf( "%s", "请输入一个正整数N,以查找相亲数对,数对中的每个数值均不大于N:" ); //if( scanf("%u",&n) != 1 ) // return 1; unsigned n = 1000000; clock_t t0 = clock(); unsigned* p = (unsigned*)calloc( n, sizeof(unsigned) ); if( !p ) return 2; for( size_t i=2; i*2<=n; ++i ) for( size_t j=i*2; j<=n; j+=i ) p[j-1] += i; for( size_t i=1; i<=n; ++i ) if( p[i-1]>=i && p[i-1]<n && i==p[p[i-1]]+1) printf( "%u\t%u\n", i, p[i-1]+1 ); clock_t t1 = clock(); printf( "--- %.3lf ---\n", (t1-t0+0.0)/CLOCKS_PER_SEC ); free( p ); return 0; }