【LDU】新生训练营杭电100题(一)

A - ASCII码排序 

http://acm.hdu.edu.cn/showproblem.php?pid=2000

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z

#include<stdio.h>
int main()
{
      char a,b,c;
      while(~scanf("%c%c%c",&a,&b,&c))
      {
            getchar();
            int temp;
            if(a>b)
            {
                 temp=a;
                 a=b;
                 b=temp;
            }
            if(a>c)
            {
                 temp=a;
                 a=c;
                 c=temp;
            }
            if(b>c)
            {
                 temp=b;
                 b=c;
                 c=temp;
            }
            printf("%c %c %c\n",a,b,c);

      }
      return 0;


}


B - 计算两点间的距离 

http://acm.hdu.edu.cn/showproblem.php?pid=2001

输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
Output
对于每组输入数据,输出一行,结果保留两位小数。
Sample Input
0 0 0 1
0 1 1 0
Sample Output
1.00
1.41

#include<stdio.h>
#include<math.h>
int main()
{
      double x1,x2,y1,y2;
      while(~scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2))
      {
            printf("%.2lf\n",sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));

      }
      return 0;
}

C - 计算球体积 

http://acm.hdu.edu.cn/showproblem.php?pid=2002

根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input
1
1.5
Sample Output
4.189
14.137


        
  
Hint
#define PI 3.1415927
#include<stdio.h>
#define PI 3.1415927
int main()
{
      double r;
      while(~scanf("%lf",&r))
      {
          printf("%.3lf\n",(4.0/3.0)*PI*r*r*r);
      }
      return 0;
}

D - 求绝对值

http://acm.hdu.edu.cn/showproblem.php?pid=2003

Problem Description
求实数的绝对值。
 

Input
输入数据有多组,每组占一行,每行包含一个实数。
 

Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
 

Sample Input
   
   
123 -234.00
 

Sample Output
   
   
123.00 234.00
 

#include<stdio.h>
#include<math.h>
int main()
{
     double x;
     while(~scanf("%lf",&x))
     {
          printf("%.2lf\n",fabs(x));
     }
     return 0;

}

E - 成绩转换

http://acm.hdu.edu.cn/showproblem.php?pid=2004

Problem Description
输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;
 

Input
输入数据有多组,每组占一行,由一个整数组成。
 

Output
对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”。
 

Sample Input
   
   
56 67 100 123
 

Sample Output
   
   
E D A Score is error!
#include<stdio.h>
int main()
{
     int x;
     while(~scanf("%d",&x))
     {
           if(x<0||x>100)
           {
               printf("Score is error!\n");
           }
           else
           {
               switch(x/10)
               {
                   case 1:
                   case 2:
                   case 3:
                   case 4:
                   case 5:printf("E\n");break;
                   case 6:printf("D\n");break;
                   case 7:printf("C\n");break;
                   case 8:printf("B\n");break;
                   case 9:
                   case 10:printf("A\n");break;
               }
           }
     }
     return 0;

}

F - 第几天? 

http://acm.hdu.edu.cn/showproblem.php?pid=2005

Problem Description
给定一个日期,输出这个日期是该年的第几天。
 

Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 

Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 

Sample Input
   
   
1985/1/20 2006/3/12
 

Sample Output
   
   
20 71

#include<stdio.h>
int judge(int year,int month,int day)
{
     int a[13]={0,0,31,59,90,120,151,181,212,243,273,304,334};
     if((year%4==0&&year%100!=0)||(year%400==0))
     {
        if(month>2)
          return a[month]+day+1;
        else
          return a[month]+day;
     }
     else
          return a[month]+day;
}
int main()
{
     int year,month,day;
     while(~scanf("%d/%d/%d",&year,&month,&day))
     {
          printf("%d\n",judge(year,month,day));
     }
     return 0;

}

G - 求奇数的乘积 

http://acm.hdu.edu.cn/showproblem.php?pid=2006

Problem Description
给你n个整数,求他们中所有奇数的乘积。
 

Input
输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。
 

Output
输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
 

Sample Input
   
   
3 1 2 3 4 2 3 4 5
 

Sample Output
   
   
3 15
#include<stdio.h>
int main()
{
     int n,x;
     while(~scanf("%d",&n))
     {
          int i,ans=1;
          for(i=0;i<n;i++)
          {
              scanf("%d",&x);
              if(x%2)
                 ans*=x;
          }
          printf("%d\n",ans);
     }
     return 0;
}

H - 平方和与立方和 

http://acm.hdu.edu.cn/showproblem.php?pid=2007

Problem Description
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
 

Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
 

Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
 

Sample Input
   
   
1 3 2 5
 

Sample Output
   
   
4 28 20 152
#include<stdio.h>
int main()
{
     int x,y,temp;
     while(~scanf("%d%d",&x,&y))
     {
          if(x>y)
          {
             temp=x;
             x=y;
             y=temp;
          }
          
          int sum1=0,sum2=0,i;
          for(i=x;i<=y;i++)
          {
              if(i%2==1)
                sum2+=i*i*i;
              else
                sum1+=i*i;
          }
          printf("%d %d\n",sum1,sum2);
     }
     return 0;

}
I - 数值统计 
http://acm.hdu.edu.cn/showproblem.php?pid=2008

Problem Description
统计给定的n个数中,负数、零和正数的个数。
 

Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
 

Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
 

Sample Input
   
   
6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0
 

Sample Output
   
   
1 2 3 0 0 5

#include<stdio.h>
int main()
{
     int n;
     while(~scanf("%d",&n),n)
     {
         int i,zero=0,plus=0,negative=0;
         double x;
         for(i=0;i<n;i++)
         {
            scanf("%lf",&x);
            if(x<0.0)
                negative++;
            else if(x==0.0)
                zero++;
            else
                plus++;
         }
         printf("%d %d %d\n",negative,zero,plus);
     }
     return 0;
}

K - 水仙花数 

http://acm.hdu.edu.cn/showproblem.php?pid=2010

Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。
 

Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
 

Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
 

Sample Input
   
   
100 120 300 380
 

Sample Output
   
   
no 370 371
 

#include<stdio.h>
int main()
{
	int m,n,t,i,j,d[5],g,s,b;
	while(~scanf("%d%d",&m,&n))
	{
	      t=0;
		  for(i=m;i<=n;i++)
		  {
  			    g=i%10;
  			    s=(i/10)%10;
  			    b=i/100;
  			    if(g*g*g+s*s*s+b*b*b==i)
  			    {
					  d[t++]=i;  	
		        }
	      }
	      if(!t)
	      {
      	       printf("no");	
      	  }
		  else
		  {
  		       for(j=0;j<t;j++)
			    {
 				    printf(j==(t-1)?"%d":"%d ",d[j]);	
 				}	
  		  }	
  		  printf("\n");
	}
	return 0;
}

L - 多项式求和

http://acm.hdu.edu.cn/showproblem.php?pid=2011

Problem Description
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。
 

Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。
 

Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。
 

Sample Input
   
   
2 1 2
 

Sample Output
   
   
1.00 0.50
 

#include<stdio.h>
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
        double i,n,sign=1,ans=0;
		scanf("%lf",&n);
		for(i=1;i<=n;i++)
		{
		     ans+=(1.0/i)*sign;
			 sign*=-1;	
		}
		printf("%.2lf\n",ans);
	}
	return 0;
	
}


M - 素数判定 

http://acm.hdu.edu.cn/showproblem.php?pid=2012


Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
 

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
 

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
 

Sample Input
   
   
0 1 0 0
 

Sample Output
   
   
OK
 
#include<stdio.h>
#include<math.h>
int main()
{
	int x,y;
	while(~scanf("%d%d",&x,&y),(x||y))
	{
	      int i,j,n,flag=1;
		  for(i=x;i<=y;i++)
		  {
  		        n=i*i+i+41;
				for(j=2;j*j<n;j++)
				{
					if(n%j==0)
					  flag=0;
				}	
  		  }
		  if(flag)
		  {
  			   printf("OK\n");
  		  }
		  else
		  {
  		        printf("Sorry\n");	
          }	
	}
	return 0;
}

N - 蟠桃记 

http://acm.hdu.edu.cn/showproblem.php?pid=2013

Problem Description
喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵^-^
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?
 

Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。
 

Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。
 

Sample Input
   
   
2 4
 

Sample Output
   
   
4 22
 

#include<stdio.h>
#include<stdlib.h>

int f(int n, int x)
{
	if (n == 1)
	{
		return  x;
	}
	else
	{
		return f(n - 1, (x + 1) * 2);
	}
}
int main()
{
	int num;
	while (scanf("%d", &num) != EOF)
	{
		printf("%d\n", f(num, 1));
	}

	return 0;
}


#include<stdio.h>
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		int m=1,i,ans=1;
		for(i=2;i<=n;i++)
		{
	        ans=m;   
			m=((m*2)+1);
			ans+=m;
		}
		printf("%d\n",ans);
	} 
	return 0;
}

O - 青年歌手大奖赛_评委会打分

http://acm.hdu.edu.cn/showproblem.php?pid=2014

Problem Description
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。
 

Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。
 

Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。
 

Sample Input
   
   
3 99 98 97 4 100 99 98 97
 

Sample Output
   
   
98.00 98.50
 

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
        double *p,sum=0,temp;
        int i,j,k; 
        p=(double *)malloc(sizeof(double)*n);
		for( i=0;i<n;i++)
		{
			scanf("%lf",(p+i));
			sum+=*(p+i);
		}
		for(i=0;i<n-1;i++)
		{
			 k=i;
			for(j=i+1;j<n;j++)
			{
				if(*(p+j)<*(p+k))
				{
					k=j;
				}
			}
			if(k!=i)
			{
				 temp;
				 temp=*(p+i);
				 *(p+i)=*(p+k);
				 *(p+k)=temp;
			} 
		}
		double a=*p;
		double b=*(p+n-1);
		sum=sum-(a+b);
		printf("%.2lf\n",sum/(n-2));
	}
}


P - 偶数求和 

http://acm.hdu.edu.cn/showproblem.php?pid=2015

Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
 

Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。
 

Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。
 

Sample Input
   
   
3 2 4 2
 

Sample Output
   
   
3 6 3 7
#include<stdio.h>
#define N 105
int main()
{
	 int n,m;
	while(~scanf("%d%d",&n,&m))
	{
	   int a[N],ans[N],i,j=1,k;
	   for(i=1;i<=n;i++)
	   {
   	       a[i]=i*2;	
   	   }
	    for(i=1;i<=n;i+=(m))
	    {
   	        k=n-i+1;
   	        if(k>m)
	   	   {
        	   ans[j++]=(( (a[i]+a[i+m-1]) )/2); 
       	   }
       	   else
       	   {
   	       	   ans[j]=(a[i]+a[n]) /2;
   	       	   break;
   	       }
    	}
    	for(i=1;i<=j;i++)
    	{
	    	printf(i==j?"%d\n":"%d ",ans[i]);
	    }
	}
	return 0;
}

Q - 数据的交换输出 

http://acm.hdu.edu.cn/showproblem.php?pid=2016

Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。
 

Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。
 

Output
对于每组输入数据,输出交换后的数列,每组输出占一行。
 

Sample Input
   
   
4 2 1 3 4 5 5 4 3 2 1 0
 

Sample Output
   
   
1 2 3 4 1 4 3 2 5

#include<stdio.h>
int main()
{
	int n,i,j,temp,min,a[105];
	while(~scanf("%d",&n),n)
	{
		scanf("%d",&a[0]);
		min=a[0];
		for(i=1;i<n;i++)
		{
		    scanf("%d",&a[i]);
		}
		for(i=0;i<n;i++)
		{
		   if(min>=a[i])
			{
		        min=a[i];
				j=i;
			}   	
		}
			temp=a[0];
			a[0]=a[j];
			a[j]=temp;
			
		for(i=0;i<n;i++)
			{
				printf(i==n-1?"%d\n":"%d ",a[i]);
			}
	}
	return 0;
}

R - 字符串统计 

http://acm.hdu.edu.cn/showproblem.php?pid=2017

Problem Description
对于给定的一个字符串,统计其中数字字符出现的次数。
 

Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
 

Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
 

Sample Input
   
   
2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdf
 

Sample Output
   
   
6 9
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
	char a[1000];
	int i,len,n,count;
	scanf("%d",&n);
	while(n--)
	{
        count=0;
		scanf("%s",a);
		for(i=0;i<strlen(a);i++)
		{
			if(isdigit(a[i]))
			    count++;
		} 
		printf("%d\n",count);
	}
	return 0;
}

S - 母牛的故事 

http://acm.hdu.edu.cn/showproblem.php?pid=2018

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

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

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

Sample Input
   
   
2 4 5 0
 

Sample Output
   
   
2 4 6

#include<stdio.h>
int a[100]={
   0,1,2,3,4	
};
void solve()
{
    for(int i=5;i<=60;i++)
	{
		a[i]=a[i-1]+a[i-3];
	}	
}
int main()
{
      int n;
      solve();
	  while(~scanf("%d",&n),n)
	  {
  		  printf("%d\n",a[n]);
  	}
	  return 0;	
}

T - 数列有序! 

http://acm.hdu.edu.cn/showproblem.php?pid=2019

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
 

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
 

Output
对于每个测试实例,输出插入新的元素后的数列。
 

Sample Input
   
   
3 3 1 2 4 0 0
 

Sample Output
   
   
1 2 3 4
#include<stdio.h>
#include<stdlib.h>
int main()
{
      int n,m;
      while(~scanf("%d%d",&n,&m),(n||m))
      {
             int *a,i,flag=-1;
             a=(int *)calloc(n,sizeof(n));
             for(i=0;i<n;i++)
             {
                  scanf("%d",&a[i]);
                  if(a[i]>=m&&flag==-1)
                 {
                     flag=i;
                 }
             }
             for(i=0;i<n;i++)
             {
                printf((i==n-1&&i!=flag)?"%d\n":"%d ",i==flag?m:a[i]);
                if(i==flag)
                {
                    flag=-1; i--;
                }
             }
      }
      return 0;

}


U - 绝对值排序 

http://acm.hdu.edu.cn/showproblem.php?pid=2020

Problem Description
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
 

Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。 
 

Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
 

Sample Input
   
   
3 3 -4 2 4 0 1 2 -3 0
 

Sample Output
   
   
-4 3 2 -3 2 1 0

#include<stdio.h>
#include<stdlib.h>
int main()
{
     int n;
     while(~scanf("%d",&n),n)
     {
            int *a,i,j,k,temp;
            a=(int *)calloc(n,sizeof(int));
            for(i=0;i<n;i++)
            {
                 scanf("%d",a+i);
            }
            for(i=1;i<n;i++)
            {
                 if(abs(i[a])>abs((i-1)[a]))
                 {
                       temp=i[a];
                       (i)[a]=(i-1)[a];
                       for(j=i-2;j>=0&&abs(temp)>abs(j[a]);j--)
                       {
                             (j+1)[a]=(j)[a];
                       }
                       (j+1)[a]=temp;
                 }
            }
            for(i=0;i<n;i++)
            {
               printf(i==n-1?"%d\n":"%d ",i[a]);
            }
     }
     return 0;


}


  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值