UVA - 11728 Alternate Task (唯一分解定理)

来源:互联网 发布:空气监测软件 编辑:程序博客网 时间:2024/05/29 13:25

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 positivenumberS, 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 toS.

 

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 toS.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
102
1000
0

 

Case 1: 1
Case 2: 101
Case 3: -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;}


0 0
原创粉丝点击