【DidaOJ】[491]【C Language Training】Amicable Numbers

Problem Description

Loading...

Problem Analysis

It involves computing divisors.

To reduce the number of operations,

an array is used to record the sum of divisors of each number.

Then, since each pair should be printed only once,

and the later number is larger than the earlier one,

the inner loop can start from i,

there is no need to start from 1.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main() {
	int a[3000];
	for(int i=1; i<3000; i++) {
		a[i]=0;
		for(int j=1; j<=i/2; j++) {
			if(i%j==0)
				a[i]+=j;
		}
	}
	for(int i=1; i<3000; i++) {
		for(int j=i+1; j<3000; j++) {
			if(a[i]==j&&a[j]==i)
				printf("(%d,%d)",i,j);
		}
	}
	printf("\n");
	return 0;
}

Problem link: 【DidaOJ】[491]【C Language Training】Amicable Numbers

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...