天天看点

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;
    }
               
    #笔者最近苦于马上到来的考试周,代码没有写注释,题目解析也不够完善,解法不一定是最优解,请看官多海涵。

继续阅读