hdu2574 Hdu Girls' Day (分解质因数)
Hdu Girls' Day
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1629 Accepted Submission(s): 490
Problem Description
Hdu
Girls' Day is a traditional activity in Hdu. Girls in Hdu participate
in the activity and show their talent and skill. The girls who win in
the activity will become the Hdu's vivid ambassadors(形象大使). There are
many students in Hdu concern the activity. Now it's the finally
competition to determine who will be the Hdu's vivid ambassadors. The
students vote for the girl they prefer. The girl who has the most number
of votes will be the first. You as a student representing Hdu Acm team
has a chance to vote. Every girl who participates in the activity has an
unique No. and name. Because you very like prime number, you will vote
for the girl whose No. has the maximum number of unique prime factors.
For example if the girl's No. is 12, and another girl's No. is 210, then you will choose the girl with No. 210. Because 210 = 2 *3 * 5*7 , 12 = 2*2*3. 210 have 4 unique prime factors but 12 just have 2. If there are many results, you will choose the one whose name has minimum lexicographic order.
For example if the girl's No. is 12, and another girl's No. is 210, then you will choose the girl with No. 210. Because 210 = 2 *3 * 5*7 , 12 = 2*2*3. 210 have 4 unique prime factors but 12 just have 2. If there are many results, you will choose the one whose name has minimum lexicographic order.
Input
The
first line contain an integer T (1 <= T <= 100).Then T cases
followed. Each case begins with an integer n (1 <= n <= 1000)
which is the number of girls.And then followed n lines ,each line
contain a string and an integer No.(1 <= No. <= 2^31 - 1). The
string is the girl's name and No. is the girl's No.The string's length
will not longer than 20.
Output
For each case,output the girl's name who you will vote.
Sample Input
2
3
Kate 56
Lily 45
Amanda 8
4
Sara 55
Ella 42
Cristina 210
Cozzi 2
Sample Output
Kate
Cristina
Source
感慨:啊啊啊,不知道合数可以被分解成质因数相乘,还有快排返回值。WA了好久啊!
收获:1.正整数可以分为1,合数,质数 。其中每个合数都可以写成几个质数相乘的形式。
2.strcmp函数:设这两个字符串为str1,str2,
若str1==str2,则返回零;
若str1>str2,则返回正数;
若str1<str2,则返回负数。
3.求解质因数两种算法
第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的;(http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115399.html)第二个:一次求出1~n的所有数的质因子,适用于题目中给的n个数比较多的,但是n不是很大的。(http://www.cnblogs.com/jiangjing/archive/2013/06/01/3112035.html)
#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> using namespace std; const int INF=0x3f3f3f3f; const double eps=1e-10; const double PI=acos(-1.0); #define maxn 500 int a[14] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; struct Node { int num; int cnt; char s[100]; }node[1005]; int cmp(Node a, Node b) { if(a.cnt == b.cnt) { if(strcmp(a.s,b.s)<0) return true; else return false; } return a.cnt > b.cnt; } int main() { int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%s%d", node[i].s, &node[i].num); if(node[i].num == 0) { node[i].cnt = 0; continue; } node[i].cnt = 0; for(int j = 0; a[j]<= node[i].num; j++) { if(j >= 10) break; if(node[i].num%a[j] == 0) { node[i].cnt++; while(node[i].num%a[j]==0) node[i].num = node[i].num/a[j]; } } if(node[i].num != 1) node[i].cnt++; } sort(node, node + n, cmp); printf("%s\n", node[0].s); } return 0; }