UVA 11728 - Alternate Task (數論)
阿新 • • 發佈:2018-01-21
sans 20px blog courier splay mission mic sub pos
Uva 11728 - Alternate Task
題目鏈接
題意:給定一個因子和。求出相應是哪個數字
思路:數字不可能大於因子和,對於每一個數字去算出因子和,然後記錄下來就可以
代碼:
#include <stdio.h>
#include <string.h>
const int N = 1005;
int n, ans[N];
void init() {
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= 1000; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
sum += j;
}
if (sum <= 1000) ans[sum] = i;
}
}
int main() {
int cas = 0;
init();
while (~scanf("%d", &n) && n) {
printf("Case %d: %d\n", ++cas, ans[n]);
}
return 0;
}
UVA 11728 - Alternate Task (數論)