UVa 294 - Divisors

来源:互联网 发布:淘宝店铺一键装修软件 编辑:程序博客网 时间:2024/05/18 16:58

基础题,就用基本方法即可,计算约数时算到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;}


0 0
原创粉丝点击