天天看點

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

                           Aizu Online Judge Introduction to Programming I

                                                            ITP1 Topic # 1 Getting Started

  • 1_A Hello World
  • Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved :

    Welcome to Online Judge!

    Write a program which prints “Hello World” to standard output.

    Input

    There is no input for this problem.

    Output

    Print “Hello World” in a line.

    Sample Input 1

    No input
               
    Sample Output 1
    Hello World
               

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

    本題要求我們去列印"Hello World",這道情懷題沒有什麼輸入,在C語言中可以直接使用printf函數來實作。對于要展示在shell小黑屏上的内容,可直接用printf(“你所要展示的内容”)即可。初學者一定不要忘記最後的分号;!!!

    下面展示一下代碼:

    #include <stdio.h>
        int main()
        {
        	printf("Hello World\n"); //列印結果“Hello World”
        	return 0;
        }
               
  • 1_B X Cubic
  • Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved :

    Write a program which calculates the cube of a given integer x.

    Input

    An integer x is given in a line.

    Output

    Print the cube of x in a line.

    Constraints

    • 1 ≤ x ≤ 100
    Sample Input 1
    2
               
    Sample Output 1
    8
               
    Sample Input 2
    3
               
    Sample Output 2
    27
               

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

    Cubic,即所謂“立方”的意思,題目名字就叫“X的立方”,題目會輸入一個1到100的整數型X,求出它的立方和并輸出。

    從思路上來說,無非是先把X設出來,然後讓一個數作為結果輸出,這個數的值是X的三次方。我們可以把這個結果設為result,result=x的三次方,然後把result列印出來。

    下面給出流程圖:

    Aizu Online Judge Introduction to Programming I C語言實作 ITP1 Topic # 1                           Aizu Online Judge Introduction to Programming I
    代碼如下:
    #include <stdio.h>
    int main()
    {
    	int X,result;//設定X,result
    	scanf("%d",&X);//輸入X
    	result=X*X*X;//對result指派
    	printf("%d\n",result); //輸出result結果
    	return 0;
    }
               

    該問題有一個問題就是如何表達X的三次方。上圖中我用了一種很原始的方法,即将X乘三次。對于這種次方問題,我們可以用pow(a,b)函數解決。

    首先在加上頭檔案 #include <math.h>,pow(a,b)的意思就是a的b次方,即a^b。這裡pow的結果和參數均要double類型,在本題可暫時忽略。是以運用pow函數的代碼如下:

    #include <math.h>
    #include <stdio.h>
    int main()
    {
    	int X,result;
    	scanf("%d",&X);
    	result=pow(X,3);
    	printf("%d\n",result); 
    	return 0;
    }
               
    其實代碼可以更加簡潔,在printf函數中本身也可以進行計算,是以我們完全可以不設result這個變量直接進行打printf("%d",X * X * X)或者printf("%d",pow(X,3)),下舉其中一例:
    #include <math.h>
    #include <stdio.h>
    int main()
    {
    	int X;
    	scanf("%d",&X);
    	printf("%d\n",X*X*X); 
    	return 0;
    }
               

    值得一提的是這道題給出了X的範圍,不是很大。對于幂指函數級别的增長,我的高中數學老師曾經給出了一個說法“爆炸式增長”,自己在高三數學二模的時候曾被這個知識點坑過,至今記憶猶新是第四道選擇題B項,當時是要判斷2^x和一個函數的交點個數,我圖沒畫完不過腦子,想當然是兩個,結果沒考慮幂指函數潛力是如此之大最後又超過那個函數又出現一個交點…是以要考慮時間複雜度和整數類型大小限制的問題。

    還要注意一下scanf輸入變量的時候不要忘記前面的&!!!如果你程式設計的時候程式莫名的停止,亦或是光标在閃卻死活輸不進去,那恭喜你,趕集檢查scanf加沒加&!!!

  • 1_C Rectangle
  • Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved :

    Write a program which calculates the area and perimeter of a given rectangle.

    Input

    The length a and breadth b of the rectangle are given in a line separated by a single space.

    Output

    Print the area and perimeter of the rectangle in a line. The two integers should be separated by a single space.

    Constraints

  • 1 ≤ a, b ≤ 100

    Sample Input 1

    3 5
               
    Sample Output 1
    15 16
               

    放出傳送門:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_C

    Rectangle,即矩形之意。本題的意思是給出矩形的長和寬a,b,求出其邊長和面積。作為受過我國優秀九年義務教育的人,我很快想到矩形就是平常我們的長方形,周長是長寬之和的兩倍,面積是長乘寬。

    下面是流程圖:

    Aizu Online Judge Introduction to Programming I C語言實作 ITP1 Topic # 1                           Aizu Online Judge Introduction to Programming I
    代碼如下:
    #include <stdio.h>
    int main()
    {
    	int a,b;//設出長寬 
    	scanf("%d%d",&a,&b);//輸入長寬數值 
    	printf("%d %d\n",2*(a+b),a*b); //輸出周長,面積 
    	return 0;
    }
               
  • 1_D Watch
  • Time Limit : 

    1 sec

     , Memory Limit : 

    131072 KB

     , isSolved :
  • Write a program which reads an integer S [second] and converts it to h : m : s where h, m, s denote hours, minutes (less than 60) and seconds (less than 60) respectively.

    Input

    An integer S is given in a line.

    Output

    Print hh, mm and ss separated by ‘:’. You do not need to put ‘0’ for a value, which consists of a digit.

    Constraints

  • 0≤S≤86400

    Sample Input 1

    46979
               
    Sample Output 1
    13:2:59
               

    放出傳送門:https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_D

    Watch,手表,不是overwatch!它會給你一個看起來亂七八糟的數字S(秒),讓你從中剝離出小時,分鐘,秒,并按照該順序輸出。

    這道題我們可以這麼思考,既然它這麼大,我們肯定想把它先變的小一點。它給的是秒,那我偏要轉成最大的小時(h)以便數值上最小,受過優秀九年義務教育的我立刻想到秒換成小時需要除以3600。偷偷打開電腦上受信任的電腦,把樣例46979/3600,先看看。果不其然,是13.0497222222…13剛好是樣例輸出小時h的數值!由于c語言裡整數型相整除會把小數點後面的東西都抹去隻剩下整數部分,是以按圖索骥我們仿照剛才的試探,用S/3600即可得到小時h。接着由于我們知道了小時,趕緊馬不停蹄把小時這部分的秒數除去,即S-3600*h。這個剩下的結果我們如法炮制,把相減的結果用/60做處理剝離出分鐘數m。最後,隻要把S裡的小時數和分鐘數包含的秒數減去即可得到秒數(s)。

    代碼如下:

    #include <stdio.h>
    	int main()
    	{
    		int S,h,m,s;//設出時分秒 
    		scanf("%d",&S);//輸入秒數數值 
    		h=S/3600;//小時數 
    		m=(S-3600*h)/60;//分鐘數 
    		s=S-3600*h-60*m;//秒數 
    		printf("%d:%d:%d\n",h,m,s);//列印結果 
    		return 0;
    	}
               

繼續閱讀