UVa 294 - Divisors

基础题,就用基本方法即可,计算约数时算到sqrt(n)即可,要注意(int)sqrt(n)和n的关系。

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
	int T; cin >> T;
	while (T--){
		int L, U; cin >> L >> U;
		int MAX = -1, num;
		for (int i = L; i <= U; ++i){
			int cnt = 0;
			for (int j = 1; j <= sqrt(i); ++j){
				if (i % j == 0){
					++cnt;
					if (i / j != j) ++cnt;
				}
			}
			if (cnt > MAX) MAX = cnt, num = i;
		}
		printf("Between %d and %d, %d has a maximum of %d divisors.\n", L, U, num, MAX);
	}
	return 0;
}


posted @ 2015-03-03 16:06  Popco  阅读(126)  评论(0编辑  收藏  举报