Archive for the 'Hello World' Category

计算两点间的距离

Posted by Danfi on 2010-01-07 添加评论

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

  1. #include <math.h>
  2. int main()
  3. {
  4.     float a,b,c,d;
  5.     while(scanf("%f %f %f %f",&a,&b,&c,&d)==4)
  6.     {
  7.         printf("%.2f\n",sqrt((a-c)*(a-c)+(b-d)*(b-d)));
  8.     }
  9.     return 0;
  10. }

ASCII码排序

Posted by Danfi on 2010-01-06 添加评论

Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output
对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input
qwe
asd
zxc

Sample Output
e q w
a d s
c x z

  1. #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5.     char ch[3];
  6.     while (cin>>ch)
  7.     {
  8.         for (int a=0;a<3;a++)
  9.         {
  10.             for (int b=a+1;b<3;b++)
  11.             {
  12.                 if (ch[a]>ch[b])
  13.                 {
  14.                     char temp=ch[a];
  15.                     ch[a]=ch[b];
  16.                     ch[b]=temp;
  17.                 }
  18.             }
  19.         }
  20.  
  21.             cout<<ch[0]<<" "<<ch[1]<<" "<<ch[2]<<endl;
  22.     }
  23. }

Sum Problem

Posted by Danfi on 2010-01-06 添加评论

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + … + n.

Input
The input will consist of a series of integers n, one integer per line.

Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.

Sample Input
1
100

Sample Output
1

5050

  1. #include <stdio.h>
  2. int main()
  3. {           
  4.     int n,i,sum;           
  5.     while(scanf("%d",&n)!=EOF)       
  6.     {           
  7.         sum=0;           
  8.     for(i=0;i<=n;i++)           
  9.         sum+=i;           
  10.     printf("%d\n\n",sum);
  11.     }       
  12.     return 0;
  13. }

A + B Problem

Posted by Danfi on 2010-01-06 添加评论

Problem Description
Calculate A + B.

Input
Each line will contain two integers A and B. Process to end of file.

Output
For each case, output A + B in one line.

Sample Input
1 1

Sample Output
2

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int a,b;
  5.     while(scanf("%d %d",&a,&b)==2)
  6.     {
  7.     printf("%d\n",a+b);
  8.     }
  9. }