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

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
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值