5.19刷题

Fighting for HDU

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=3&problemid=15

复制代码
 1 //排序sort 
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 int sum1=0,sum2=0;
 6 int a[105],b[105];
 7 bool cmp(int x,int y)
 8 {
 9     return x<y;
10 }
11 int main()
12 {
13     int n;
14     while(cin>>n,n)
15     {
16         sum1=0;
17         sum2=0;
18         fill(a,a+105,0);
19         fill(b,b+105,0);
20         for(int i=0;i<n;i++)
21         {
22             cin>>a[i];
23         }
24         for(int i=0;i<n;i++)
25         {
26             cin>>b[i];
27         }
28         sort(a,a+n,cmp);
29         sort(b,b+n,cmp);
30         for(int i=0;i<n;i++)
31         {
32             if(a[i]>b[i])
33             sum1+=2;
34             else
35             if(a[i]<b[i])
36             sum2+=2;
37             else
38             {
39                 sum1++;
40                 sum2++;
41             }
42         }
43         cout<<sum1<<" "<<"vs"<<" "<<sum2<<endl;
44     }
45     return 0;
46 }
复制代码

rank

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=3&problemid=18 

复制代码
 1 //hdoj好像不支持cin,getline输入字符串型? 
 2 //简单的输入和排序,记录与输入学号相同的成绩,将成绩排序,后输出最高名次 
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 using namespace std;
 7 char jk[100];
 8 char s[100]; 
 9 int a[105];
10 bool cmp(int x,int y)
11 {
12     return x>y;
13 }
14 int main()
15 {
16     while(scanf("%s",jk)!=EOF)//相当于~scanf("%s",jk) 
17     {
18         fill(a,a+105,0);
19         int n;
20         int i=0;
21         while(1)
22         {
23             i++;
24             scanf("%s",s);
25             cin>>a[i];
26             //只记录杰克的成绩就可以了 
27             if(strcmp(jk,s)==0)
28             {
29                 n=a[i];
30             }
31             if(s[0]=='0')
32             break;
33         }
34         sort(a+1,a+i,cmp);
35         for(int j=1;j<=i;j++)
36         {
37             if(a[j]==n)
38             {
39                 cout<<j<<endl;
40                 break;
41             }
42         }
43     }
44     return 0;
45 }
复制代码

Crixalis's Equipment 

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=3&problemid=19 

复制代码
 1 //非常简单的贪心,首先解决进入需要空间大,实际空间小的数据 
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int a;
 8     int b;
 9 };
10 node x[1010];
11 //排序标准:进入空间大,实际空间小的在前 
12 bool cmp(node n,node m) 
13 {
14     return n.b-n.a>m.b-m.a;
15 }
16 int main()
17 {
18     int n;
19     cin>>n;
20     while(n--)
21     {
22         int f=1;
23         int v,m;
24         cin>>v>>m;
25         for(int i=0;i<m;i++)
26         {
27             cin>>x[i].a>>x[i].b;
28         }
29         sort(x,x+m,cmp);
30         for(int i=0;i<m;i++)
31         {
32             if(v>=x[i].b)
33             {
34                 v-=x[i].a;
35             }
36             else
37             {
38                 f=0;
39                 break;
40             }
41         }
42         if(f==1)
43         cout<<"Yes"<<endl;
44         else
45         cout<<"No"<<endl;
46     }
47     return 0;
48  } 
复制代码

最小公倍数

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=1 

复制代码
 1 //swap,交换两个值
 2 //辗转相除,最小公倍数,最大公约数 
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 int a,b;
 7 int zz(int a,int b)
 8 {
 9     int k;
10     while(b!=0)
11     {
12         k=a%b;
13         a=b;
14         b=k;
15     }
16     return a;
17 }
18 int main()
19 {
20     while(cin>>a>>b)
21     {
22         if(a<b)
23         {
24             swap(a,b);
25         }
26         cout<<a*b/zz(a,b)<<endl;
27     }
28     return 0;
29  } 
复制代码

How many prime numbers 

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=2

复制代码
 1 //质数,时间复杂度为根号n 
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 int a[100];
 7 int num=0;
 8 int main()
 9 {
10     int n,m;
11     while(cin>>n)
12     {
13         int num=0;
14         while(n--)
15         {
16             cin>>m;
17             int f=1;
18             for(int j=2;j<=sqrt(m);j++)
19             {
20                 if(m%j==0)
21                 {
22                     f=0;
23                     break;
24                 }
25             }
26             if(f==1)
27             num++;
28         }
29         cout<<num<<endl;
30     }
31     return 0;
32  } 
复制代码

cake

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=4

复制代码
 1 //2个人至少切两刀,n个人至少切n刀,理论上要满足全部的情况就要全部切一遍即:
 2 //全部相加,但可能有重合的切线,也就是最小公约数,减去即可 
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 int zz(int p,int q)
 7 {
 8     int k;
 9     while(q!=0)
10     {
11         k=p%q;
12         p=q;
13         q=k;
14     }
15     return p;
16 }
17 int main()
18 {
19     int p,q;
20     while(cin>>p>>q)
21     {
22         cout<<p+q-zz(p,q)<<endl;
23     }
24     return 0;
25  } 
复制代码

 

posted @   格蕾  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示