天天看點

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;
}
           

繼續閱讀