Magic Number(动态数组)

题目描述
There is a romantic city in faraway places, where individuals always take a number plate, keep searching, keep matching, although no one realizes who is his/her true love. However, the God of this world release an oracle to Alice, which can help her distinguish his/her lover from the others!This is a strange algorithm. Assuming that number S on Alice’s plate, then the man she is waiting follows this rule:whose positive divisors’ sum is equal to S.
Now you are asked to write a program that can find all appropriate figures quickly.

输入格式
The input consists of one or more test cases. Each case starts with an integer S. (1<=S<=10000)

输出格式
For each case, there are two lines of output.
The first line must contain m: the number of different individuals.
The second line must contain m distinct numbers wi-the figure on the i-th person’s plate. You should output number plates in ascending order.
If no number can meet request, print ‘Forever alone’ at the second line.

样例输入
42
5

样例输出
3
20 26 41
0
Forever alone

来源
2014 ACM 黑龙江省赛 热身赛 第三题

#include
#include
using namespace std;
const int MAX=10000;
int a[MAX+10];
vector ans[MAX+10];
int main(){
for(int i=1;i<=MAX;i++)
for(int j=1;j<=MAX;j+=i)
a[j]+=i;
for(int i=1;i<=MAX;i++)
if(a[i]<=MAX)
ans[a[i]].push_back(i);
int n;
while(cin>>n){
int s=ans[n].size();
cout<<s<<endl;
if(s==0)
cout<<“Forever alone”<<endl;
else{
for(int i=0;i<s;i++)
cout<<ans[n][i]-1<<" ";
cout<<endl;
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51794965/article/details/111475398