ACM第二次

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章