hdu水题习题集(3)

来源:互联网 发布:苹果锁定2g网络 编辑:程序博客网 时间:2024/05/22 09:50

hdu-2563

统计问题

#include<stdio.h>
int f(int n)
{
   if(n==0)  return 0;
   if(n==1)  return 3;
   if(n==2)  return 7; 
   else return 2*f(n-1)+f(n-2);
}
int main()
{
    int n,m;
    scanf("%d",&n);
    while(n--)
    {
      scanf("%d",&m);
      printf("%d\n",f(m));
    }//while(1);
return 0;                找规律,找到规律就容易解决了
}

 

hdu-2562

奇偶位互换

#include<stdio.h>
#include<string.h>
int main()
{
   int n,m,i,j,k,t;
   char str[100];
   char ch;
   while(~scanf("%d",&n))
   {
     for(int r=1;r<=n;r++)
     {
      memset(str,'\0',sizeof(str));
      scanf("%s",str);
       k=strlen(str);
       for(i=1;i<k;i+=2)
       {
         ch=str[i];
         str[i]=str[i-1];
         str[i-1]=ch;                
       }            
       printf("%s\n",str);
     }        
   }
return 0;                        //            只需一次,就过了  
}

hdu-2561

第二小整数

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,m,i,j;
    int a[100];
    while(~scanf("%d",&n))
    {
     while(n--)
     {
      scanf("%d",&m);
      for(int r=0;r<m;r++)
        scanf("%d",&a[r]);                     
      sort(a,a+m);                                          水题,一次就过。。。。。。。。。。
     // for(int r=0;r<m;r++)
      printf("%d\n",a[1]);
     }
   }//while(1);
return 0;   
}

母牛的故事

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41885    Accepted Submission(s): 20813


Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
 


Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
 


Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
 


Sample Input
2450
 


Sample Output
246
 
找规律题,为什么每次的找规律题我都找不到呢?
#include<stdio.h>
int main()
{
    int a[100],i,n;
    a[1]=1;
    a[2]=2;
    a[3]=3;
    a[4]=4;
    for(i=5;i<=100;i++)
      a[i]=a[i-1]+a[i-3];                            //             只有看到发现的规律,我才会做,这是怎么回事。。。
    while(~scanf("%d",&n),n!=0)
    printf("%d\n",a[n]);
return 0;   
}
   hdu-3788

ZOJ问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2710    Accepted Submission(s): 825


Problem Description
对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。

是否AC的规则如下:
1. zoj能AC;
2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空;
 

Input
输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000;
 

Output
对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。
 

Sample Input
zojozojoozoojoooozoojoooozoojozojooooozojozojoooo
 

Sample Output
AcceptedAcceptedAcceptedAcceptedAcceptedAcceptedWrong AnswerWrong Answer
 
对这道题的要求:
1  z和j只能有一个,z和j和o都必须有
2  不能有特殊字符
3  z前面的o的个数a和z和j之间的个数b和j之后的个数c需要满足c=a*b
#include<stdio.h>
#include<string.h>
int main()
{
    int n,m,i,h,j,k,t;
    int a,b,c;
    char str[2000];
    while(gets(str))
    {
        int flag=0;
        int p1=0,p2=0;
        a=b=c=0;
        t=-1;
        h=-1;
        k=strlen(str);               
        for(i=0;i<k;i++)
        {
          if(str[i]=='z')
          {
            p2++;
            t=i;
          }
          if(str[i]!='z'&&str[i]!='o'&&str[i]!='j')
            flag=1;
        }
        for(i=k-1;i>=0;i--)
          if(str[i]=='j')
          {
            p1++;
            h=i;
          }
        if(t==-1||h==-1)
        flag=1;
        for(i=0;i<t;i++)
        {
          if(str[i]!='o')
          break;
          a++;
        }
        for(i=k-1;i>=h;i--)
        {
          if(str[i]!='o')
          break;
          c++;
        }
        for(i=t+1;i<h;i++)
        {
          if(str[i]!='o')
          break;
          b++;                 
        } 
        if(flag==1)
        {
         printf("Wrong Answer\n");
         continue;
        }
        if(c==a*b&&b>0&&p1==1&&p2==1)
        printf("Accepted\n");
        else
        printf("Wrong Answer\n");
  //      printf("%d %d %d\n",a,b,c);   
    }
return 0;       这道题我过得十分不易。。。。。
}
                   
hdu-1412

{A} + {B}

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11814    Accepted Submission(s): 4933


Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
 

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
 

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
 

Sample Input
1 212 31 211 2
 

Sample Output
1 2 31 2
 
 
注意数组大小就行了。。。
#include<stdio.h>#include<algorithm>using namespace std;int main(){    int a[50000];    int n,m,i,j;    while(~scanf("%d%d",&n,&m))    {      for(i=0;i<n+m;i++)                                   scanf("%d",&a[i]);      sort(a,a+n+m);      printf("%d",a[0]);      for(i=1;i<n+m;i++)      {        if(a[i]==a[i-1]) continue;          printf(" %d",a[i]);                   }      printf("\n");    }return 0;    }
 
 

A C

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3776    Accepted Submission(s): 2413
Problem Description
Are you excited when you see the title "AC" ? If the answer is YES , AC it ;You must learn these two combination formulas in the school . If you have forgotten it , see the picture.Now I will give you n and m , and your task is to calculate the answer .
 
Input
In the first line , there is a integer T indicates the number of test cases.Then T cases follows in the T lines.Each case contains a character 'A' or 'C', two integers represent n and m. (1<=n,m<=10)
 
Output
For each case , if the character is 'A' , calculate A(m,n),and if the character is 'C' , calculate C(m,n).And print the answer in a single line.
 
Sample Input
2A 10 10C 4 2
 

Sample Output
36288006
 
 
#include<stdio.h>int main(){   int n,m,i,j,k,t;   int num;   char s;   int a[13];   a[0]=1;   a[1]=1;   for(i=2;i<13;i++)    a[i]=a[i-1]*i;   scanf("%d",&t);   getchar();   while(t--)   {     scanf("%s%d%d",&s,&n,&m);     if(s=='A')  {    printf("%d\n",a[n]/a[n-m]);  }  if(s=='C')   printf("%d\n",a[n]/a[n-m]/a[m]);   }return 0;}真的感觉我好水啊。。。。。这道题我竟然没有一次通过
 
hdu-2022

海选女主角

#include<stdio.h>#include<math.h>int main(){ int n,m; int a[100][100]; int i,j,max; while(~scanf("%d%d",&n,&m)) {    int hang,lie;    for(i=1;i<=n;i++)     {     for(j=1;j<=m;j++)      scanf("%d",&a[i][j]);  }   max=0;  for(i=1;i<=n;i++)  {    for(j=1;j<=m;j++)      if(abs(a[i][j])>abs(max))         {       max=a[i][j];          hang=i;       lie=j;     }     }   printf("%d %d %d\n",hang,lie,max); }return 0; }
 
 
 
 
hdu-2099

整除的尾数

#include<stdio.h>int main(){    int a,b;    int i,j,k,t;    int n,m;    while(~scanf("%d%d",&a,&b))    {      if(a==0&&b==0)        break;        int sum;        int r=0;                for(i=0;i<=9;i++)      {        for(j=0;j<=9;j++)           {             sum=a*100+i*10+j;                      if(sum%b==0)             {               r++;               if(r!=1)                 printf(" ");               printf("%d%d",i,j);             }           }      }           printf("\n");                }return 0;    }

 

hdu-1716

排列2

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){   int shu1[12];   int shu2[12];   int shu3[100];       int i,j,k,t;   int r=0;   while(~scanf("%d%d%d%d",&shu1[0],&shu1[1],&shu1[2],&shu1[3])){      if(shu1[0]==0&&shu1[1]==0&&shu1[2]==0&&shu1[3]==0)                                                  break;                    if(r!=0)             printf("\n");             r++;      sort(shu1,shu1+4);      t=0;      for(i=1000;i<10000;i++){          shu2[0]=i%10;                                 shu2[1]=i/10%10;          shu2[2]=i/100%10;          shu2[3]=i/1000;          sort(shu2,shu2+4);          if(shu1[0]==shu2[0]&&shu1[1]==shu2[1]&&shu1[2]==shu2[2]&&shu1[3]==shu2[3])           shu3[t++]=i;         }      printf("%d",shu3[0]);       for(i=1;i<=t-1;i++)                        {                                                     if(shu3[i]/1000==shu3[i-1]/1000)                                {                                    printf(" ");                                    printf("%d",shu3[i]);                                }                                else        {         printf("\n");                                printf("%d",shu3[i]);        }                                                  }                            printf("\n");   }return 0;}注意输出格式

hdu-1718

 

Rank

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int cmp(int a,int b){   return a>b;    }int main(){    int a[1200];    char s[1200][11];    while(~scanf("%s",s[0])){       int t=1;       int i,j;            int k;                           while(scanf("%s%d",s[t],&a[t])){          if(s[t][0]=='0')            break;            t++;                                                                  }            for(i=1;i<t;i++){          if(strcmp(s[i],s[0])==0){             k=a[i];                            }         }         //     printf("%d\n",k);         sort(a+1,a+t,cmp);       int rank;       int r=0;       for(i=1;i<t;i++){     //     printf("%d ",a[i]);           if(a[i]==k){              r++;              rank=i;                      }                        }       printf("%d\n",rank-r+1);    }return 0;    }

 
hdu-2097

Sky数

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13593    Accepted Submission(s): 7906
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
299212340
 

Sample Output
2992 is a Sky Number.1234 is not a Sky Number.
 
此题为进制转换,比较简单
#include<stdio.h>#include<string.h>#define MAXN 10000int sky[MAXN];int main(){    int i,j,k;    int h,t,x,y;    int a,b,c;    memset(sky,0,sizeof(sky));    for(i=2992;i<MAXN;i++){       h=t=k=i;       a=b=c=0;       while(k){          a+=k%10;            k/=10;       }                              while(t){          b+=t%12;          t/=12;                }            if(a!=b)          continue;          while(h){         c+=h%16;                  h/=16;       }              if(a==b&&b==c){          sky[i]=1;                      }     }    int n;    while(~scanf("%d",&n),n!=0){        if(sky[n]){           printf("%d is a Sky Number.\n",n);                   }                                    else          printf("%d is not a Sky Number.\n",n);    }return 0;    }
hdu-1701-

ACMer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3758    Accepted Submission(s): 1723
Problem Description
There are at least P% and at most Q% students of HDU are ACMers, now I want to know how many students HDU have at least?
 
Input
The input contains multiple test cases.The first line has one integer,represent the number of test cases.The following N lines each line contains two numbers P and Q(P < Q),which accurate up to 2 decimal places.
 
Output
For each test case, output the minumal number of students in HDU.
 
Sample Input
113.00 14.10
 

Sample Output
15
题目解析:
这道题如果是理解了题意的话,比较简单,但是应该注意的是整型,应该改为浮点型
代码:
#include<stdio.h>
int main()
{
int i,t;
double n,m;
scanf("%d",&t);
while(t--){
scanf("%lf %lf",&n,&m);
for(i=1;;i++){
if((int)(i*n/100)<(int)(i*m/100))
break;
}
printf("%d\n",i);
}
return 0;
}


hdu-1702

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3244    Accepted Submission(s): 1691


Problem Description
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
 

Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
 

Output
For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.
 

Sample Input
44 FIFOIN 1IN 2OUTOUT4 FILOIN 1IN 2OUTOUT5 FIFOIN 1IN 2OUTOUTOUT5 FILOIN 1IN 2OUTIN 3OUT
 

Sample Output
122112None23
题目解析:
这道题目可以考虑使用栈,虽然我对栈才有一点点的了解。。。。。。
代码如下:
#include<stdio.h>
#include<string.h>
int main(){
int N,T;
int i,j,k,t;
int a[10000];
char str1[10],str2[10];
char zifu[4];
scanf("%d",&N);
while(N--){
int top=1,num=1;
scanf("%d %s",&t,str1);
for(int r=1;r<=t;r++){
scanf("%s",zifu);
if(strcmp(zifu,"IN")==0){
 scanf("%d",&k);
 a[top++]=k;
//  printf("%d %d\n",top,a[top-1]);
}
else if(strcmp(zifu,"OUT")==0){
if(strcmp(str1,"FIFO")==0){
if(num<top)
  printf("%d\n",a[num++]);
else
  printf("None\n");
}
if(strcmp(str1,"FILO")==0){
if(top-1>=1)
  printf("%d\n",a[--top]);
else
  printf("None\n");
}
}
}
}
return 0;
}



hdu-1703

PBD

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 783    Accepted Submission(s): 262


Problem Description
PrisonBreak is a popular TV programme in HDU. ACboy likes it very much, and he join a PrisonBreak discussing team called "PBD".Every Tuesday night, a lot of PBDers will contact with each other to discuss the newest plot of PrisonBreak season2. Generally speaking, every PBDer has distinct ideas about the play, so everyone want to know all the others' ideas. For example, when ACboy contract with Sam, ACboy will tell all the ideas he konws to Sam, and Sam will also tell all the ideas he konws to ACboy, and the call costs 5 yuan.
If there are N people in the "PBD" team, what is the minimum cost to let everyone knows all the others' ideas?
 

Input
The input contains multiple test cases.
Each test case contains a number N, means there are N people in the "PBD" team.N = 0 ends the input.(A call cost 5 yuan).
 

Output
for each case, output a integer represent the minimum cost to let everyone knows all the others' ideas.
 

Sample Input
12340
 

Sample Output
051520
Hint
If there are 2 people, for example, named A, B. Then A calls B, then A and B will know each other's ideas, so it only needs one call, so the minimum cost is 1*5 = 5 yuan.
题目解析:
这是一道递推题目,当n>=5时,符合条件f(n)=f(n-1)+2;
代码如下:
#include<stdio.h>
#define MAX 1000000
int num[MAX];
int main(){
int N,i;
num[1]=0;
num[2]=1;
num[3]=3;
num[4]=4;
for(i=5;i<1000000;i++){
num[i]=num[i-1]+2;
}
while(~scanf("%d",&N),N!=0){
printf("%d\n",num[N]*5);
}
return 0;
}

hdu-1032:

The 3n + 1 problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22381    Accepted Submission(s): 8364


Problem Description
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm:


    1.      input n

    2.      print n

    3.      if n = 1 then STOP

    4.           if n is odd then n <- 3n + 1

    5.           else n <- n / 2

    6.      GOTO 2


Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
 

Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.
 

Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
 

Sample Input
1 10100 200201 210900 1000
 

Sample Output
1 10 20100 200 125201 210 89900 1000 174
 

3n+1问题,在这道题目中,需要注意的是,如果输入的两个数不是先小后大的话,要先交换两个数,但是输出的时候还是按输入的来说
代码如下:
#include<stdio.h>
int main(){
int n,m,k;
int num1,num2;
while(~scanf("%d %d",&n,&m)){
int t;
int i,j;
num1=n;
num2=m;
if(num1>num2){
t=num1;
num1=num2;
num2=t;
}
int max=0;
for(i=num1;i<=num2;i++){
int r;
r=i;
int ans=1;
while(r!=1){
if(r&1){
r=3*r+1;
ans++;
}
else{
r/=2;
ans++;
}
}
if(ans>max)
  max=ans;

printf("%d %d %d\n",n,m,max);
}
return 0;


hdu-3787

A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2856    Accepted Submission(s): 1619


Problem Description
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
 

Input
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
 

Output
请计算A+B的结果,并以正常形式输出,每组数据占一行。
 

Sample Input
-234,567,890 123,456,7891,234 2,345,678
 

Sample Output
-111111101
题目分析:这道题目比较简单,一次就过。。。使用字符串就行
代码如下:
#include<stdio.h>
#include<string.h>
int main(){
char str1[10000];
char str2[10000];
int i,j,k,t;
int num1,num2;
while(~scanf("%s %s",str1,str2)){
int a=0;
int b=0;
int sum=0;
num1=strlen(str1);
num2=strlen(str2);
for(i=0;i<num1;i++){
if(str1[i]>='0'&&str1[i]<='9'){
a=a*10+(str1[i]-'0');
}
}
if(str1[0]=='-')
 a*=(-1);
for(i=0;i<num2;i++){
if(str2[i]>='0'&&str2[i]<='9'){
b=b*10+(str2[i]-'0');
}

if(str2[0]=='-')
  b*=(-1);
sum=a+b;
printf("%d\n",sum);
}
return 0;
}

0 0
原创粉丝点击