ACM第二次ABCDEFGHI

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

#include <iostream>
#include <algorithm>
using namespace std;

int main()
 {
    char n[4];

     while (cin >> n)
    {
        if (n[0] > n[1]) swap(n[0], n[1]);
         if (n[1] > n[2]) swap(n[1], n[2]);
         if (n[0] > n[1]) swap(n[0], n[1]);
         cout << n[0] <<" "<< n[1] <<" "<< n[2] << endl;
     }
    return 0;
 }

B - 计算两点间的距离
输入两点坐标(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(void)
{
    double x1,x2,y1,y2,s;
    while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
    {
      s =sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
      printf("%.2lf\n",s);
    }
    return 0;
}

C - 计算球体积
根据输入的半径值,计算球的体积。
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(void)
{
    double r,s;
    while(scanf("%lf",&r)!=EOF)
    {
      s=4.0/3.0*PI*r*r*r;
      printf("%.3lf\n",s);
    }
    return 0;
}

D - 求绝对值
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123
-234.00
Sample Output
123.00
234.00

#include<stdio.h>
int main(void)
{
    double x;
    while(scanf("%lf",&x)!=EOF)
    {
        if(x>0)
            printf("%.2lf\n",x);
        else
            printf("%.2lf\n",-x);
    }
    return 0;
}

E - 成绩转换
输入一个百分制的成绩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 t;
    while(scanf("%d",&t)!=EOF)
    {
        if(90<=t&&t<=100)printf("%c\n",'A');
        else if(80<=t&&t<=89)printf("%c\n",'B');
        else if(70<=t&&t<=79)printf("%c\n",'C');
        else if(60<=t&&t<=69)printf("%c\n",'D');
        else if(0<=t&&t<=59)printf("%c\n",'E');
        else printf("Score is error!\n");
    }
    return 0;
}

用switch如下

#include<stdio.h> 
int main() 
{ 
  int n,i; 
  while(scanf("%d",&n)!=EOF) 
  { 
    if(n<0||n>100) 
    { 
        printf("Score is error!\n"); 
    } 
      else 
      { 
        n/=10; 
        switch(n)
        { 
          case 10:
          case 9: printf("A\n"); break; 
          case 8: printf("B\n"); break;
          case 7: printf("C\n"); break; 
          case 6: printf("D\n"); break;
          default: printf("E\n"); break;
        } 
      } 
  } 
   return 0;
}

F - 第几天?
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71

#include<iostream>
using namespace std;
bool leapyear(int n)
{
    if(n%4==0&&n%100!=0||n%400==0)
        return 1;
    else
        return 0;
}
int main()
{
    int year,mouth,day;
    char ch1,ch2;
    while(cin>>year>>ch1>>mouth>>ch2>>day)
    {
        int sum=0;
        for(int k=1;k<mouth;k++)
        {
            if(k==1||k==3||k==5||k==7||k==8||k==10||k==12)
                sum+=31;
            else if(k==2)
            {
                if(leapyear(year))
                    sum+=29;
                else
                    sum+=28;
            }
            else
                sum+=30;
        }
        sum+=day;
        cout<<sum<<endl;
    }
    return 0;
}

G - 求奇数的乘积
给你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,a[100],j,i;
   while(scanf("%d",&n))
   {
        j=1;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%2!=0)
            j*=a[i];
        }
        printf("%d\n",j);
   }
     return 0;
}

H - 平方和与立方和
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample Input
1 3
2 5
Sample Output
4 28
20 152

#include<stdio.h>
int main(void)
{
    int m,n,even_sum,odd_sum,i;
    while(scanf("%d%d",&m,&n)!=EOF){
     //和清零
         odd_sum=even_sum=0;
     //计算平方和和立方和
         if(m>n){//保证m<=n,以便控制循环
         i=m;
         m=n;
         n=i;
         } 
         for(i=m; i<=n; i++) { 
         if(i&1) //或i%2==1
            odd_sum+=i*i*i; //奇数立方
            else
            even_sum+=i*i; //偶数平方
         }
         //输出结果
          printf("%d %d\n",even_sum,odd_sum);
    }
    return 0;
}

I - 数值统计
统计给定的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,count1=0,count2=0,count3=0; 
  double a; 
  while(scanf("%d",&n)&&n)/*输入一个n值 并判断n值
如果n不等于0哪么就执行 while下面的语句。。如果等于0就不执行while下面的语句。*/ 
  { 
        while(n--) 
        { 
            scanf("%lf",&a); 
            if(a<0) 
              count1++; 
            else 
                if(a==0) 
                    count2++; 
                else 
                    count3++; 
        } 
         printf("%d %d %d\n",count1,count2,count3);
  } 
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值