杭电刷题汇总

头文件
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include <stdlib.h>
#include<cstring>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;


单词数

Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input
you are my friend #

Sample Output
4

解题思路:
首先一定要看清题目让求的是什么,第一次看的时候随便看了一眼,一位是要求文章有多少的单词,后来wa了,才发现题目问的是文章有多少个不同的单词,
这个题目用stl中的集合set

 1 #include <set>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6   string str1,str2;
 7   while(getline(cin,str1))
 8   {
 9     if(str1 == "#")
10       break;
11     istringstream stream(str1);
12     set<string>Set;//定义一个集合
13     while(stream>>str2)
14     {
15       Set.insert(str2);
16     }
17     cout<<Set.size()<<endl;
18     } 
19   return 0;
20 }
View Code

附:温姑娘 https://blog.csdn.net/wyxeainn/article/details/53000406

手机短号
Problem Description
大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号。假设所有的短号都是是 6+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678。
现在,如果给你一个11位长的手机号码,你能找出对应的短号吗?

Input
输入数据的第一行是一个N(N <= 200),表示有N个数据,接下来的N行每一行为一个11位的手机号码。

Output
输出应包括N行,每行包括一个对应的短号,输出应与输入的顺序一致。

Sample Input
2
13512345678
13787654321

Sample Output
645678
654321

Source
2006/1/15 ACM程序设计期末考试
tip:字符串的输入有cin>> gets scanf(“%s”) scanf(“%c”);

 1 int main()
 2 {
 3     int n;
 4     cin>>n;
 5     while(n--)
 6     {
 7         char a[15];
 8 //        gets(a);
 9 //        getchar();
10         cin>>a;
11         for(int i=5;a[i]!='\0';i++)
12         {    if(i==5)
13             cout<<6;
14             else
15             cout<<a[i];
16         }
17         cout<<endl;
18     }
19     return 0;
20 }
View Code

空心三角形
Problem Description
把一个字符三角形掏空,就能节省材料成本,减轻重量,但关键是为了追求另一种视觉效果。在设计的过程中,需要给出各种花纹的材料和大小尺寸的三角形样板,通过电脑临时做出来,以便看看效果。

Input
每行包含一个字符和一个整数n(0<n<41),不同的字符表示不同的花纹,整数n表示等腰三角形的高。显然其底边长为2n-1。如果遇到@字符,则表示所做出来的样板三角形已经够了。

Output
每个样板三角形之间应空上一行,三角形的中间为空。显然行末没有多余的空格。

Sample Input
X 2
A 7
@

Sample Output
X
XXX
 

      A

     A A

    A   A

   A     A

  A       A

 A         A

AAAAAAAAAAAAA

 1 int main()
 2 {
 3     char a;
 4     int b,i,j,k=0;
 5     while((a=getchar())!='@')//可换为:while(scanf("%c",&a)&&a!='@')
 6     {
 7         ++k;
 8         getchar();//这一句的作用是读取空格! 
 9         scanf("%d",&b);
10         getchar();//记住这一句不能省,否则会在第二次输入时a直接变成\n(换行符)! 
11         if(k!=1)//这个格式很重要,这是在第一个结果的后面先不输出空行,
12           printf("\n");//只有在判断过第二个结果满足条件后,才输出空行,
13                     //此空行依然在第二个结果之前,满足两个空三角形之间有空行的条件!  
14         for(i=1;i<b;i++)
15         {
16             for(j=1;j<=b+(i-1);j++) 
17              {
18                  if(j==b-(i-1)||j==b+(i-1))
19                     printf("%c",a);
20                  else 
21                     printf(" ");
22              } 
23             printf("\n");
24         }        
25         for(i=0;i<2*b-1;i++)
26           printf("%c",a);
27         printf("\n");
28     }
29     return 0;
30 }
View Code

附:大佬一  https://blog.csdn.net/dxx_111/article/details/47067981

2070
斐波那契数列输入n,求F (n) (0<=n<=50)
1. __int64
2. __int128
程序代码:

 1 typedef long long LL;
 2 inline void write(__int64 x)
 3 {
 4      if(x>9)
 5          write(x/10);
 6      putchar(x%10+'0');
 7 }
 8 
 9 int main()
10 {
11     int n;
12     while(scanf("%d",&n)&&n!=-1){
13         __int64 a = 0;
14         __int64 b = 1;
15         __int64 c;
16                 
17         for(int i=1;i<n;i++)
18         {
19             c=a+b;
20             a=b;
21             b=c;
22         }
23         if(n>0)
24             write(b);
25         else
26             cout<<0;
27         cout<<endl;
28     }
29     return 0;
30 }
View Code

2031.进制转换

Problem Description
输入一个十进制数N,将它转换成R进制数输出。

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

Sample Input
7 2
23 12
-4 3

Sample Output
111
1B
-11

Author
lcy

Source
C语言程序设计练习(五)

 1 void f(int n,int r)
 2 {
 3     int m;
 4     if(n==0)
 5         return;
 6     else
 7     {
 8         f(n/r,r);
 9         m=n%r;
10         if(m<10)
11             printf("%d",m);
12         else
13             printf("%c", 'A'+m-10);
14     }
15     
16 }
17 
18 int main(){
19     int n,r;
20     while(scanf("%d%d",&n,&r)!=EOF)
21     {
22         if(n==0)
23             printf("%d",0);
24         else if(n<0)
25         {
26             printf("-");
27             n=-n;
28         }
29         f(n,r);
30         cout<<endl;
31     }
32     return 0;
33 }
View Code

2057.
16进制

A + B Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42021 Accepted Submission(s): 17120

Problem Description
There must be many A + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !

Input
The input contains several test cases, please process to the end of the file.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.

Output
For each test case,print the sum of A and B in hexadecimal in one line.

Sample Input
+A -A
+1A 12
1A -9
-1A -12
1A -AA

Sample Output
0
2C
11
-2C
-90

Author
linle

Source
校庆杯Warm Up

 1 int main(){
 2     __int64 a,b;
 3     while(scanf("%I64X%I64X",&a,&b)!=EOF)
 4     {
 5         if(a+b<0){
 6             a=-a;
 7             b=-b;
 8             cout<<"-";
 9         }
10         printf("%I64X\n",a+b);
11     }    
12     return 0;
13 }
View Code


2099.
整除的尾数

Problem Description
一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢?

Input
输入数据有若干组,每组数据包含二个整数a,b(0<a<10000, 10<b<100),若遇到0 0则处理结束。

Output
对应每组数据,将满足条件的所有尾数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。

Sample Input
200 40
1992 95
0 0

Sample Output
00 40 80
15

Source
2007省赛集训队练习赛(2)

 1 int main()
 2 {
 3     int a,b,i,n,flag;
 4     while(~scanf("%d%d",&a,&b),a||b)
 5     {
 6         flag=0;
 7         for(i=a*100/b;i<=(a*100+99)/b;i++)
 8             if(b*i>=a*100&&b*i<=a*100+99)
 9             {
10                 n=b*i-a*100;
11                 if(flag)
12                     printf(" ");
13                 flag=1;
14                 if(n<10)
15                     printf("0%d",n);
16                 else
17                     printf("%d",n);
18             }
19             printf("\n");
20     }
21 }
View Code

2097.
Sky数
一个十进制数转化为任意进制的数各位之和

Problem Description
Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊。Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数。但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧。

Input
输入含有一些四位正整数,如果为0,则输入结束。

Output
若n为Sky数,则输出“#n is a Sky Number.”,否则输出“#n is not a Sky Number.”。每个结果占一行。注意:#n表示所读入的n值。

Sample Input
2992
1234
0

Sample Output
2992 is a Sky Number.
1234 is not a Sky Number.

Source
2007省赛集训队练习赛(2) 

 1 int g(int m,int n)
 2 {
 3     int sum=0;
 4     while(m)
 5     {
 6         sum+=m%n;
 7         m=m/n;
 8     }
 9     return sum;
10 }
11 int main()
12 {
13     int n,a,b,c;
14     while(~scanf("%d",&n)&&n)
15     {
16         a=g(n,10);
17         b=g(n,12);
18         c=g(n,16);
19         if(a==b&&b==c)printf("%d is a Sky Number.\n",n);
20         else printf("%d is not a Sky Number.\n",n);
21     }
22     return 0;
23 }
View Code

2089.
不要62
转化和搜索字符串函数:itoa,strstr

Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。

Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。

Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。

Sample Input
1 100
0 0

Sample Output
80

Author
qianneng

Source
迎接新学期——超级Easy版热身赛

 1 int s[1000005];
 2 char str[8],a[2]="4",b[5]="62";
 3 void init(){
 4    for(int i=1;i<=1000000;i++){
 5         itoa(i,str,10);
 6         if(strstr(str,a)!=NULL||strstr(str,b)!=NULL)
 7         s[i]=0;
 8         else
 9         s[i]=1;
10    }
11 }
12 int main(){
13     int n,m;
14      init();
15     while(scanf("%d%d",&m,&n)!=EOF&&(n||m))
16     {
17         int sum=0;
18         for(int i=m;i<=n;i++){
19            sum+=s[i];
20         }
21         printf("%d\n",sum);
22     }
23     return 0;
24 }
View Code

附:大佬二 https://www.cnblogs.com/13224ACMer/p/4790995.html
C(string.h)字符串操作函数总结:
https://blog.csdn.net/qq_33757398/article/details/81212618


2077.
汉诺塔III
递归
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26145 Accepted Submission(s): 12378

Problem Description
约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下、由小到大顺序串着由64个圆盘构成的塔。目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动一个盘,且不允许大盘放在小盘的上面。
现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。
Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?

Input
包含多组数据,每次输入一个N值(1<=N=35)。

Output
对于每组数据,输出移动最小的次数。

Sample Input
1
3
12

Sample Output
2
26
531440

Author
Rabbit

Source
RPG专场练习赛

 1 int a[20]={0,1};
 2  
 3 int main()
 4 {
 5     int i,T;
 6     for(i=2;i<21;i++)
 7     {
 8         a[i]=3*a[i-1]+1; 
 9     }
10     scanf("%d",&T);
11     while(T--)
12     {
13         scanf("%d",&i);
14         printf("%d\n",2*a[i-1]+2);
15     }
16     return 0;
17 }
View Code

附:汉诺塔总结 https://blog.csdn.net/xueerfei008/article/details/9904681

猜你喜欢

转载自www.cnblogs.com/zhigengniao/p/11273060.html
今日推荐