天天看點

Aizu Online Judge Introduction to Programming I C語言實作 ITP1 Topic # 2                       Aizu Online Judge Introduction to Programming I

                       Aizu Online Judge Introduction to Programming I

                                                        ITP1 Topic # 2 Branch on Condition

2_A Small, Large, or Equal

  • Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved :

Write a program which prints small/large/equal relation of given two integers a and b.

Input

Two integers a and b separated by a single space are given in a line.

Output

For given two integers a and b, print

            a < b

            if a is less than b,

            a > b

            if a is greater than b,

            and

            a == b

            if a equals to b.

Constraints

-1000 ≤ a, b ≤ 1000

Sample Input 1

1 2
           

Sample Output 1

a < b
           

Sample Input 2

4 3
           

Sample Output 2

a > b
           

Sample Input 3

5 5
           

Sample Output 3

a == b
           

附上傳送門:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/2/ITP1_2_A

題意為比較a,b兩個數的大小并輸出相應的結果。我們把這道題拆分成ab兩數的輸入;判斷兩數的大小;根據相應大小輸出相應結果。由于實作思路并不困難,下直接給出代碼:

#include <stdio.h>
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	if(a<b)
		printf("a < b\n");
	else if(a==b)
			printf("a == b\n");
		 else printf("a > b\n");
	return 0;
}
           

需要注意一點的是大于小于等于号與a,b中要有空格,不然會CE。

  • 2_B Range

               Write a program which reads three integers a, b and c, and prints “Yes” if a<b<c, otherwise “No”.

    Input

    Three integers a, b and c separated by a single space are given in a line.

    Output

    Print “Yes” or “No” in a line.

    Constraints

  • 0 ≤ a, b, c ≤ 100

    Sample Input 1

    1 3 8
               
    Sample Output 1
    Yes
               
    Sample Input 2
    3 8 1
               
    Sample Output 2
    No
               

    附上傳送門:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/2/ITP1_2_B

    題意為如果a,b,c按依次遞增的順序排列,則輸出Yes,否則輸出No。我·們遵循上題的大體思路:先輸入a,b,c三個數;判斷是否滿足a<b且b<c的條件;輸出Yes或者No。實作過程比較簡單,一個if判斷即可,注意“且”在C語言裡用“&&”表示,是兩個&。

    #include <stdio.h>
    int main()
    {
    	int a,b,c;
    	scanf("%d%d%d",&a,&b,&c);
    	if(a<b&&b<c)
    		printf("Yes\n");
    	else  printf("No\n");
    	return 0;
    }
    
               
  1. 2_C Sorting Three Numbers
  2. Time Limit : 1 sec , Memory Limit : 131072 KB , isSolved : --

    Write a program which reads three integers, and prints them in ascending order.

    Input

    Three integers separated by a single space are given in a line.

    Output

    Print the given integers in ascending order in a line. Put a single space between two integers.

    Constraints

  3. 1 ≤ the three integers ≤ 10000
  4. Sample Input 1
    3 8 1
               
    Sample Output 1
    1 3 8
               

    附上傳送門:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/2/ITP1_2_C

    sort,排序。題目要求我們對三個數進行排序。

    這道題解法是,先将abc三數的值輸入;然後兩兩數分别進行比較;最後輸出Yes或者No。具體實作方式是用if else判斷并嵌套。這裡筆者為了友善不去多層嵌套(其實是嵌套了WA了…),我自定義了一個比較大小并交換的函數max(x,y),用操縱指針去交換兩者的值。(因為不用指針操縱記憶體的話實際參數隻是在函數體内部交換)這樣的話隻需要對ab,ac,bc三次比較并交換兩數的大小,最後輸出a,b,c的值即可實作排序。

    關于排序有許多種特殊的方法,這裡隻是單純的練習一下判斷1,沒有去深入了解。在Algorithms and Data Structures I中我會去詳細解釋。

    代碼如下:

    #include <stdio.h>
    void max(int *x,int *y)
    {
    	if(*x>*y)
    	{
    		int t=*x;
    		*x=*y;
    		*y=t;
    	}
    }
    int main()
    {
    	int a,b,c;
    	scanf("%d%d%d",&a,&b,&c);
    	max(&a,&b);
    	max(&a,&c);
    	max(&b,&c);
    	printf("%d %d %d\n",a,b,c);
    	return 0;
    }
    
               
  5. 2_D Circle in a Rectangle
  6. Time Limit : 1 sec , Memory Limit : 131072 KB , isSolved : --

    Write a program which reads a rectangle and a circle, and determines whether the circle is arranged inside the rectangle. As shown in the following figures, the upper right coordinate (W,H)(W,H) of the rectangle and the central coordinate (x,y)(x,y) and radius rr of the circle are given.

    Aizu Online Judge Introduction to Programming I C語言實作 ITP1 Topic # 2                       Aizu Online Judge Introduction to Programming I

    Input

    Five integers WW, HH, xx, yy and rr separated by a single space are given in a line.

    Output

    Print "Yes" if the circle is placed inside the rectangle, otherwise "No" in a line.

    Constraints

  7. −100≤x,y≤100−100≤x,y≤100
  8. 0<W,H,r≤1000<W,H,r≤100
  9. Sample Input 1
    5 4 2 2 1
               
    Sample Output 1
    Yes
               
    Sample Input 2
    5 4 2 4 1
               
    Sample Output 2
No
           
  1. 附上傳送門:https://onlinejudge.u-aizu.ac.jp/problems/ITP1_2_D

    這道題看着簡單,實則讓筆者回憶起了被校内網賽支配的恐懼…網賽的那道題是兩個相當于正交的矩形,這道題似乎沒有那麼麻煩。看題,這裡有一個矩形,你知道一個點定在了(0,0)即坐标原點,其對角線所指的那個頂點則是(W,H),W,H均已知,為輸入。還有一個圓形已知圓心坐标(x,y)和半徑r。目标是判斷圓是否完全内含于矩形。

    這道題筆者想了很久也沒有辦法,因為我的思路一直執着于找出兩者圖形中心距離與某個極限的聯系。而肉眼觀察比較幾個位置就能發現,這個極限也在不斷變化且極限不能排除一些情況。是以我去請教了老師。老師把恰好内含圓的矩形畫了出來。他說隻要比較圓上的點與矩形邊的關系即可。

    代碼如下:

    #include <stdio.h>
    
    int main()
    {
      int W,H,x,y,r;
      int  ans=1;
    
      scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
      if(x-r<0)ans=0;
      if(r+x>W)ans=0;
      if(y-r<0)ans=0;
      if(y+r>H)ans=0;
    
      if(ans)printf("Yes\n");
      else printf("No\n");
    
      return 0;
    }
               
    #筆者最近苦于馬上到來的考試周,代碼沒有寫注釋,題目解析也不夠完善,解法不一定是最優解,請看官多海涵。

繼續閱讀