ASCII碼排序
Problem Description
輸入三個字符後,按各字符的ASCII碼從小到大的順序輸出這三個字符。
Input
輸入數據有多組,每組佔一行,有三個字符組成,之間無空格。
Output
對於每組輸入數據,輸出一行,字符中間用一個空格分開。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z
這個題注意char類型可以自動轉換成int類型,因此可以直接比較大小,這樣題目就變成三個數比較大小要求從小到大輸出
#include<iostream>
using namespace std;
int main(){
char a,b,c,d;
while(cin>>a>>b>>c){
if(a>b){
d = a;
a = b;
b = d;
}
if(b>c){
d = b;
b =c;
c = d;
}
if(a>b){
d = a;
a = b;
b = d;
}
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
計算兩點間的距離
Problem Description
輸入兩點座標(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
此題注意開根號可以使用sqrt()函數,需要導入#include<cmath>。要保留兩位小數可以使用cout<<setiosflags(ios::fixed)<<setprecision(2) ,如果單獨使用cout<<setprecision(2)表示保留兩位有效數字,需要導入#include<iomanip>。
#include<iostream>
#include <iomanip>
#include<cmath>
using namespace std;
int main(){
double x1,y1,x2,y2,distence;
while(cin>>x1>>y1>>x2>>y2){
distence = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
cout<<setiosflags(ios::fixed)<<setprecision(2)<<distence<<endl;
}
}