Bookmark and Share

free counters
Back to the Training Home

Lesson: 26
Problem: 1730
Title: Self Numbers
Difficulty Level: 2.0
Date: FEB 28, 2009
Site: http://acm.tju.edu.cn/toj/

I think you already know the Sieve of Eratosthenes algorithm. This problem can be solved by using same algorithm with few modifications. Just analyze the source code.

Solution:

#include<stdio.h>
#define maxn 10001

int Self[maxn];

int sumOfDigits(int n) {
	int sum = 0;
	while(n) {
		sum += n%10;
		n /= 10;
	}
	return sum;
}

void Find() {
	int i, j, k;
	for(i = 1; i<10001; ) {
		k = i + sumOfDigits(i);
		while(k <= 10000) {
			Self[k] = 1;
			k = k + sumOfDigits(k);
		}
		for(++i; Self[i]; i++);
	}
	for(i = 1; i<=10000; i++) {
		if(Self[i] == 0) printf("%d\n",i);
	}
	
}
int main() {
	Find();
	return 0;
}
Back to the Training Home