Little Hasan loves to play number games with his friends.One day they were playing a game where one of them will speak out a positive numberand the others have to tell the sum of its factors. The first one to say itcorrectly wins. After a while they got bored and wanted to try out a differentgame. Hassan then suggested about trying the reverse. That is, given a positivenumber S, they have to find a numberwhose factors add up to S. Realizingthat this task is tougher than the original task, Hasan came to you forhelp. Luckily Hasan owns a portableprogrammable device and you have decided to burn a program to this device.Given the value of S as input to theprogram, it will output a number whose sum of factors equal to S.
Input
Each case of input will consist of a positive integer S<=1000. The last case is followedby a value of 0.
Output
Foreach case of input, there will be one line of output. It will be a positiveinteger whose sum of factors is equal to S.If there is more than one such integer, output the largest. If no such numberexists, output -1. Adhere to theformat shown in sample output.
Sample Input Output for Sample Input
1
| Case 1: 1
|
ProblemSetter: Shamim Hafiz, Special Thanks: Sohel Hafiz
题意:输入一个正整数S,求一个最大的正整数N,使得N的所有因子的和是S
思路:统计1000内的数的因子和,求解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int s, sum[maxn];
void init() {
memset(sum, 0, sizeof(sum));
for (int i = 1; i < maxn; i++)
for (int j = 1; j <= i; j++)
if (i % j == 0)
sum[i] += j;
}
int main() {
int cas = 1;
init();
while (scanf("%d", &s) != EOF && s) {
int flag = 0;
printf("Case %d: ", cas++);
for (int i = 1000; i >= 1; i--) {
if (sum[i] == s) {
printf("%d\n", i);
flag = 1;
break;
}
}
if (!flag)
printf("-1\n");
}
return 0;
}