Alternate Task UVA11728 暴力枚举



F

Alternate Task

Input: Standard Input

Output: Standard Output

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 number and the others have to tell the sum of its factors. The first one to say it correctly wins. After a while they got bored and wanted to try out a different game. Hassan then suggested about trying the reverse. That is, given a positive number S, they have to find a number whose factors add up to S. Realizing that this task is tougher than the original task, Hasan came to you for help.  Luckily Hasan owns a portable programmable device and you have decided to burn a program to this device. Given the value of S as input to the program, 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 followed by a value of 0.

 

Output

 

For each case of input, there will be one line of output. It will be a positive integer whose sum of factors is equal to S. If there is more than one such integer, output the largest. If no such number exists, output -1. Adhere to the format shown in sample output.

Sample Input                             Output for Sample Input

1
102
1000
0

 

Case 1: 1
Case 2: 101
Case 3: -1

 


Problem Setter: Shamim Hafiz, Special Thanks: Sohel Hafiz

 

/*
 * Author:  
 * Created Time:  2013/10/31 20:09:42
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d
", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 30000;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

int main() 
{
    //freopen("in.txt","r",stdin);
    int n;
    int kcase = 1;
    while(cin>>n && n)
    {
        int ans = -1;
        repd(i,1000,1)
        {
            int sum = 0;
            for(int j = 1;j*j<=i;++j)
            {
                if(i%j == 0)
                {
                    if(i/j != j)
                    {
                        sum += j;
                        sum += i/j;
                    }else
                        sum+=j;
                }
            }
            if(sum == n)
            {
                ans = i;
                break;
            }
        }
        printf("Case %d: ",kcase++);
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3400031.html