天天看点

Aizu Online Judge Introduction to Programming I C语言实现 ITP1 Topic #3                Aizu Online Judge Introduction to Programming I

                Aizu Online Judge Introduction to Programming I

                                               ITP1 Topic # 3 Repetitive Processing

3_A Print Many Hello World

Time Limit : 1 sec , Memory Limit : 131072 KB , isSolved : 

Write a program which prints 1000 "Hello World".

Input

There is no input for this problem.

Output

The output consists of 1000 lines. Print "Hello World" in each line.

Sample Input

No input

Sample Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
.
.
.
.
.
.
Hello World
           

附上传送门:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/3/ITP1_3_A

该题是情怀题的加强版,题意是打印Hello World1000次,每打一次换行输出。

听起来像是小学时代的罚抄,可计算机最喜欢干的就是这种机械重复的事情。程序员的作用其实就像是计算机的御用铲屎官,把解决问题的办法给计算机想好,一口一口喂到计算机嘴里才行。

这里考察的主要是循环。

for循环:

#include <stdio.h>

int main()
{
    int i;//初始化计数器i
    for(i = 0;i < 1000;i++)//循环至i达到1000,正好1000次
        printf("Hello World\n");//打印Hello World,结束时\n换行
    return  0;
}
           

对于C99标准而言,我们甚至可以简化设置计数器这一步,放进for循环里定义计数器i,例如for(int i = 0; i < 1000; i++)这样。

当然我们也可以用while循环:

#include <stdio.h>

int main()
{
    int i = 0;//初始化计数器i
    while((i++) < 1000)//循环至i达到1000,正好1000次
        printf("Hello World\n");//打印Hello World,结束时\n换行
    return  0;
}
           
  1. 3_B Print Test Cases

    Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved : 

    In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.

    Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.

    Input

                The input consists of multiple datasets. Each dataset consists of an integer x in a line.

    The input ends with an integer 0. You program should not process (print) for this terminal symbol.

    Output

                For each dataset, print x in the following format:

    Case i: x
               

    where i is the case number which starts with 1. Put a single space between “Case” and i. Also, put a single space between ‘:’ and x.

    Constraints

    Sample Input

    3
     5
     11
     7
     8
     19
     0
               
    Sample Output
    Case 1: 3
     Case 2: 5
     Case 3: 11
     Case 4: 7
     Case 5: 8
     Case 6: 19
               
    附上传送门:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/3/ITP1_3_B
    • 1 ≤ x ≤ 10000
    • The number of datasets ≤ 10000
  2. 题意为给你一组数据,用Case 计数器: 数字  的形式输出直到数字0出现,结束程序。
  3. 这种格式在我校OJ经常出现,因为笔者在大学之前没有接触过编程,所以在打学校国庆新生赛的时候无从下手,就是因为这个格式,每道题都是先给测试数据的组数如T,然后后面输入T组数据这样云云。面对这种格式,基本上是用for循环来解决的,如:
  4. for(i = 0;i < T;i ++)
    {
        //程序
    }
               
    当然一些情况也可以用while,不过笔者认为在有确数的前提下for循环会更合适,while循环如:
  5. while(scanf("%d",&N)!=EOF)
    {
        //程序
    }
               
    说了这么多回到题上。正所谓人类的本质就是复读机,这道题的本质也的确如此,只要我们稍微注意一下格式。根据题意,我们需要加一个计数器来记录数据的次数,而且我们每次输入数据时需要判断该数会否为0,若为0要终止程序。由于没有确数,我们采用while循环:
  6. #include <stdio.h>
    
    int main()
    {
        int i = 1,x;//初始化计数器i
        while(scanf("%d",&x)!=EOF&&x)//输入的同时检查x是否为0
            printf("Case %d: %d",i++,x);//人类的本质
        return  0;
    }
               
    3_C  Swapping Two Numbers
  7. Time Limit : 1 sec , Memory Limit : 131072 KB , isSolved : 

    Write a program which reads two integers x and y, and prints them in ascending order. 

    Input

    The input consists of multiple datasets. Each dataset consists of two integers x and y separated by a single space.

    The input ends with two 0 (when both x and y are zero). Your program should not process for these terminal symbols.

    Output

    For each dataset, print x and y in ascending order in a line. Put a single space between x and y.

    Constraints

  8. 0 ≤ x, y ≤ 10000
  9. the number of datasets ≤ 3000
  10. Sample Input
    3 2
    2 2
    5 3
    0 0
               
    Sample Output
  11. 2 3
    2 2
    3 5
               
    放下传送门:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/3/ITP1_3_C
  12. 这道题由于笔者英语水平有限造成了WA。
  13. Aizu Online Judge Introduction to Programming I C语言实现 ITP1 Topic #3                Aizu Online Judge Introduction to Programming I
          <---------------惨不忍睹
  14. 题意为有x,y两数,在两数均为0输入进系统之前对两数进行从小到大的排序并按x(空格)y的格式输出。
  15. 我一直卡在排序上,因为根本没有翻译出来这个话,后来看了solution里大家的代码,发现:???怎么还要排序???我哭了。
  16. 那么进入正题,这道题思路并不难,大概分为以下几步:
  17. 先判断并输入这两个数;然后判断这个数的大小关系并进行调整;输出。
  18. #include<stdio.h>
    int main()
    {
      int i,n,m;//设置变量
      for(i=1;;i++)
      {
        scanf("%d %d",&n,&m);//输入两数
        if(n==0 && m==0)break;//判断两数是否为0结束程序
        if(n>m)printf("%d %d\n",m,n);//比大小,下同
        else printf("%d %d\n",n,m);
      }
      return 0;
    }
               
    3_D How Many Divisors?
  19. Time Limit : 1 sec , Memory Limit : 131072 KB , isSolved : 

    Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b.

    Input

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

    Output

    Print the number of divisors in a line.

    Constraints

1 ≤ a, b, c ≤ 10000

a ≤ b

Sample Input 1

5 14 80
           

Sample Output 1

3
           

Put down the portal!https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/3/ITP1_3_D

Divisors?什么玩意???别着急,咱看看其他的。肉眼观察,这道题有三个数ABC,注意关键:c between a and b.想必大家已经清楚了,字面上是c在a,b之间,而实际我们想一想,事情没有那么简单,从a到b之间所有的数,能被c整除的个数才是这道题的内涵。当然了,如果大家没有我这么懒选择去查查或者英语功底过关当然没有我上面那么多内心戏了。

回到题目,既然是所有的数,想必还得用循环。开动我们智慧的大脑,会发现这是一道循环次数已知的题。for循环是不二选择。那么这道题就有几个步骤:

for循环对从a到b所有数遍历;每次循环判断一次能不能被c整除;若是计数器加一,否则无事发生;输出计数器。

#include <stdio.h>
int main()
{
	int a,b,c,flag=0;//初始化
	scanf("%d%d%d",&a,&b,&c);//输入三个数
	for(;a<=b;a++)//用a作为循环计数器
	{
		if(c%a==0)flag++;//判断是否能被c整除
	}
	printf("%d\n",flag);//最后输出计数器i
	return 0;
}
           

继续阅读