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