ACM實驗一:順序結構程序設計


A - C語言實驗——Hello World!(printf練習)

Description

很高興你能上機實踐所學的C語言知識!
編程不是在課本上的幾頁紙就能學會的,你必須多思考、多上機才能真正學會一門編程語言,這也是我們出這些題目的初衷。
這些題目都是課本上的基本題目,主要目的是讓大家鞏固課堂上所學到的,希望大家能夠認真對待!
為了便於調試題目,做這些題目時可以先在CodeBlocks、DevC++或Microsoft VC++6.0中調試成功后再提交。
下面我們就開始吧:

利用C語言基本格式顯示以下內容: Hello World!

Input

本題沒有輸入數據

Output

輸出字符串Hello World!輸出后需要換行。

Sample

Output 

Hello World!
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 int main() 5 { 6     printf("Hello World!\n"); 7     return 0; 8 }

 

B - C語言實驗——計算A+B(順序結構)

Description

這是一道在各個ACM訓練網站上最基本的題目,一般都是他們的第一道題,來讓大家熟悉在線評測系統的環境!
從鍵盤上輸入兩個整數,然后計算他們的和,並把他們的和打印出來。

Input

從鍵盤上輸入兩個整數,這兩個整數在同一行上!

Output

在這兩個整數的下面一行是輸出這兩個整數的和!

Sample

Input 

2 3

Output 

5
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b;
 7     scanf("%d %d",&a,&b);
 8     printf("%d",a+b);
 9     return 0;
10 }

C - C語言實驗——交換兩個整數的值(順序結構)

Description

交換兩個變量的值,由終端輸入兩個整數給變量x、y,然后交換x和y的值后,輸出x和y。

Input

從鍵盤輸入兩個整數變量x和y;

Output

在交換x、y的值后將x和y輸出!

Sample

Input 

4 6

Output 

6 4
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b,t;
 7     scanf("%d %d",&a,&b);
 8     t=a,a=b,b=t;
 9     printf("%d %d",a,b);
10     return 0;
11 }

D - C語言實驗——逆置正整數

Description

輸入一個三位正整數,將它反向輸出。

Input

3位正整數。

Output

逆置后的正整數。

Sample

Input 

123

Output 

321
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int x,a,b,c;
 7     scanf("%d",&x);
 8     a=x/100;
 9     b=x/10%10;
10     c=x%10%10;
11     x=a+b*10+c*100;
12     printf("%d",x);
13     return 0;
14 }

E - C語言實驗——買糖果

Description

小瑜是個愛吃糖果的饞鬼,天天嚷着要爸爸買糖果,可是爸爸很忙,哪有時間啊,於是就讓小瑜自己去了,糖果3角錢一塊,爸爸給小瑜n元錢,請你告訴小瑜最多能買幾塊糖,還剩幾角錢?

Input

輸入爸爸給小瑜的錢n元,n為整數。

Output

小瑜最多能買回的糖塊數以及剩下的錢(單位為:角),用空格分隔。

Sample

Input 

2

Output 

6 2
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a;
 7     scanf("%d",&a);
 8     printf("%d %d",a*10/3,a*10%3);
 9     return 0;
10 }

F - C語言實驗——三個整數和、積與平均值

Description

給出三個整數,請你設計一個程序,求出這三個數的和、乘積和平均數。

Input

輸入只有三個正整數a、b、c。

Output

輸出一行,包括三個的和、乘積、平均數。 數據之間用一個空格隔開,其中平均數保留小數后面兩位。

Sample

Input 

2 3 3

Output 

8 18 2.67
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b,c,sum,mul;
 7     double p;
 8     scanf("%d %d %d",&a,&b,&c);
 9     sum=a+b+c;
10     mul=a*b*c;
11     p=sum/3.0;
12     printf("%d %d %.2lf",sum,mul,p);
13     return 0;
14 }

G - C語言實驗——圓柱體計算

Description

已知圓柱體的底面半徑r和高h,計算圓柱體底面周長和面積、圓柱體側面積以及圓柱體體積。其中圓周率定義為3.1415926。

Input

輸入數據有一行,包括2個正實數r和h,以空格分隔。

Output

輸出數據一行,包括圓柱體底面周長和面積、圓柱體側面積以及圓柱體體積,以空格分開,所有數據均保留2位小數。

Sample

Input 

1 2

Output 

6.28 3.14 12.57 6.28
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define PI 3.1415926
 4 int main()
 5 {
 6     double r,h,l,s,cs,v;
 7     scanf("%lf %lf",&r,&h);
 8     l=2*PI*r;
 9     s=PI*r*r;
10     cs=l*h;
11     v=s*h;
12     printf("%.2lf %.2lf %.2lf %.2lf",l,s,cs,v);
13     return 0;
14 }

H - C語言實驗——溫度轉換

Description

輸入一個華氏溫度,輸出攝氏溫度,其轉換公式為:C=5(F-32)/9。

Input

輸入數據只有一個實數,即華氏溫度。

Output

輸出數據只有一個,即攝氏溫度,保留2位小數。

Sample

Input 

32.0

Output 

0.00
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define PI 3.1415926
 4 int main()
 5 {
 6     double F,C;
 7     scanf("%lf",&F);
 8     C=5*(F-32)/9;
 9     printf("%.2lf",C);
10     return 0;
11 }

I - C語言實驗——單個字符輸入和輸出(順序結構)

Description

用getchar()從鍵盤上輸入一個字符,用putchar()打印出來!

Input

從鍵盤上輸入一個字符!

Output

把剛剛輸入的字符打印出來!

Sample

Input 

A

Output 

A
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define PI 3.1415926
 4 int main()
 5 {
 6     char c;
 7     c=getchar();
 8     putchar(c);
 9     return 0;
10 }

J - C語言實驗——轉換字母(順序結構)

Description

從鍵盤上輸入一個小寫字母,然后將小寫字母裝換成大寫字母輸出!

Input

從鍵盤上輸入一個小寫字母。

Output

小寫字母裝換成大寫字母輸出。

Sample

Input 

a

Output 

A
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define PI 3.1415926
 4 int main()
 5 {
 6     char c;
 7     c=getchar();
 8     putchar(c-32);
 9     return 0;
10 }

K - 實數的輸出和占位

Description

輸入一個實數,請你按如下要求輸出:

第一行按雙精度默認輸出,

第二行雙精度數輸出共占 10 位,其中 3 位小數,右對齊,左補空格並在兩端添加星號包裹,

第三行雙精度數輸出共占 10 位,其中 3 位小數,左對齊,右補空格並在兩端添加星號包裹。

Input

一個double范圍內的正實數 a 。 

Output

共三行,按題目描述輸出。

Sample

Input 

123.56789

Output 

123.567890
*   123.568*
*123.568   *
 1 #include <stdio.h>
 2 #include <stdlib.h>

 5 int main()
 6 {
 7     double a;
 8     scanf("%lf",&a);
 9     printf("%lf\n",a);
 10     printf("*%10.3lf*\n",a);//%(”+“右對齊,”-“左對齊)(占位數)(.小數個數)lf
 11     printf("*%-10.3lf*\n",a);
 12     return 0;
 13 }

L - 大整數的輸入輸出

Description

輸入兩個 long long 范圍內的整數,輸出他們的和。

Input

兩個 long long 范圍內的整數。

Output

輸出的兩個大整數的和,保證結果在 long long 范圍內。

Sample

Input 

2222222222
3333333333

Output 

5555555555
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     long long a,b;
 7     scanf("%lld %lld",&a,&b);
 8     printf("%lld",a+b);
 9     return 0;
10 }

M - 帶’ 和 ”字符的輸出

Description

輸入一個字符,輸出兩行。

第一行將字符用 ' 包裹。

第二行將字符用 " 包裹。

Input

輸入一個字符。

Output

按題目描述輸出。

Sample

Input 

A

Output 

'A'
"A"
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     scanf("%c",&c);
 8     printf("\'%c\'\n",c);
 9     printf("\"%c\"",c);
10     return 0;
11 }

N - '%'字符的輸入輸出

Description

輸入三個用 `` % `` 分割的正整數 a b c 代表月,日,年,

要求按照輸入樣式原樣輸出。

Input

三個int范圍內的正整數,中間用 `` %`` 分割。

Output

按題目描述原樣輸出。

Sample

Input 

9%17%2018

Output 

9%17%2018
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b,c;
 7     scanf("%d%%%d%%%d",&a,&b,&c);
 8     printf("%d%%%d%%%d",a,b,c);
 9     return 0;
10 }

O - ‘\’字符的輸入輸出

Description

輸入三個用 `` \ `` 分割的正整數 a b c 代表日,月,年,

要求按照輸入樣式原樣輸出。

Input

三個int范圍內的正整數,中間用 `` \ `` 分割。

Output

按題目描述原樣輸出。

Sample

Input 

9\17\2018

Output 

9\17\2018
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b,c;
 7     scanf("%d\\%d\\%d",&a,&b,&c);
 8     printf("%d\\%d\\%d",a,b,c);
 9     return 0;
10 }

P - 十六進制數輸出和占位

Description

輸入一個整數,請你按如下要求輸出:

第一行按原樣輸出,

第二行以十六進制輸出(字母小寫),

第三行以十六進制輸出(字母大寫)。

Input

一個int范圍內的正整數 a 。 

Output

共三行,按題目描述輸出。

Sample

Input 

456

Output 

456
1c8
1C8
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a;
 7     scanf("%d",&a);
 8     printf("%d\n",a);
 9     printf("%x\n",a);
10     printf("%X\n",a);
11     return 0;
12 }

Q - 八進制數輸出和占位

Description

輸入一個整數,請你按如下要求輸出:

第一行按原樣輸出,

第二行以八進制靠右輸出,不足 8 位左補 0 並在兩端添加星號包裹,

第三行以八進制靠左輸出,不足 8 位右補空格並在兩端添加星號包裹。

Input

一個int范圍內的正整數 a 。 

Output

共三行,按題目描述輸出。

Sample

Input 

123

Output 

123
*00000173*
*173     *
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a;
 7     scanf("%d",&a);
 8     printf("%d\n",a);
 9     printf("*%08o*\n",a);//%(默認補空格,0則補零)(占位數)(.小數個數)(進制)
10     printf("*%-8o*\n",a);
11     return 0;
12 }

R - 十進制輸入輸出和其它非空格占位

Description

輸入一個整數,請你按如下要求輸出:

第一行按原樣輸出,

第二行整數靠右原樣輸出,不足 8 位左補 0 並在兩端添加星號包裹,

第三行整數靠左原樣輸出,不足 8 位右補空格並在兩端添加星號包裹。

Input

一個int范圍內的正整數 a 。 

Output

共三行,按題目描述輸出。

Sample

Input 

123456

Output 

123456
*00123456*
*123456  *
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a;
 7     scanf("%d",&a);
 8     printf("%d\n",a);
 9     printf("*%08d*\n",a);//%(默認補空格,0則補零)(占位數)(.小數個數)(進制)
10     printf("*%-8d*\n",a);
11     return 0;
12 }

S - 十進制輸入輸出和空格占位

Description

輸入一個整數,請你按如下要求輸出:

第一行按原樣輸出,

第二行按原樣靠右輸出,不足 8 位左補空格並在兩端添加星號包裹,

第三行按原樣靠左輸出,不足 8 位右補空格並在兩端添加星號包裹。

Input

一個int范圍內的正整數 a 。 

Output

共三行,按題目描述輸出。

Sample

Input 

123456

Output 

123456
*  123456*
*123456  *
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a;
 7     scanf("%d",&a);
 8     printf("%d\n",a);
 9     printf("*%8d*\n",a);//%(默認補空格,0則補零)(占位數)(.小數個數)(進制)
10     printf("*%-8d*\n",a);
11     return 0;
12 }

T - 字符型數據輸出和占位

Description

輸入一個字符,請你按如下要求輸出:

第一行字符數據默認輸出,

第二行字符型數據輸出共占 4 位,右對齊,左補 3 個空格並在兩端添加星號包裹,

第三行字符型數據輸出共占 4 位,左對齊,右補 3 個空格並在兩端添加星號包裹。

Input

輸入一個字符 。 

Output

共三行,按題目描述輸出。

Sample

Input 

c

Output 

c
*   c*
*c   *
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     char a;
 7     scanf("%c",&a);
 8     printf("%c\n",a);
 9     printf("*%4c*\n",a);//%(默認補空格,0則補零)(占位數)(.小數個數)(進制)
10     printf("*%-4c*\n",a);
11     return 0;
12 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM