题目相关
题目链接
AtCoder Beginner Contest 180 C 题,https://atcoder.jp/contests/abc180/tasks/abc180_c。
Problem Statement
We have N cream puffs.
Find all possible number of people to which we can evenly distribute the cream puffs without cutting them.
Input
Input is given from Standard Input in the following format:
N
Output
Print the numbers of people in ascending order, each in its own line.
Samples1
Sample Input 1
6
Sample Output 1
1
2
3
6
Explaination
For example, we can evenly distribute the cream puffs to two people by giving three to each person.
Samples2
Sample Input 2
720
Sample Output 2
1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
30
36
40
45
48
60
72
80
90
120
144
180
240
360
720
Samples3
Sample Input 3
1000000007
Sample Output 3
1
1000000007
Constraints
- N is an integer.
题解报告
题目翻译
给 N 个泡芙,请写出所有分法,泡芙不能切开。从小到大输出。
题目分析
惭愧,发现 AtCoder Grand Contest 已经肝不动了。还是混混鱼塘比较自在一点。
本题就是求数字 N 的因数。本题的核心是以下这么几个:
1、循环的结束在哪里?如果循环到 N,由于 N 很大,这样肯定是 TLE。可以参考标准质数判断的方法,循环到平方即可;
2、数据保存?可能考点就在这里,我们可以使用 STL 的 set,set 会自动排序。当然也可以保存到 vector,然后再排序。
3、注意变量的数据类型。由于 N 最大为 10^12,所以需要使用 long long。
AC 参考代码
//https://atcoder.jp/contests/abc180/tasks/abc180_c
//C - Cream puff
#include <iostream>
#include <set>
using namespace std;
int main() {
long long n;
cin>>n;
set<long long> ans;
for (long long i=1; i*i<=n; i++) {
if (0==n%i) {
ans.insert(i);
ans.insert(n/i);
}
}
set<long long>::iterator it;
for(it=ans.begin(); it!=ans.end(); it++) {
cout<<*it<<"\n";
}
return 0;
}