1.判斷是不是字母
題目描述:
KK想判斷輸入的字符是不是字母,請幫他編程實現。
輸入描述:
多組輸入,每一行輸入一個字符。
輸出描述:
針對每組輸入,輸出單獨占一行,判斷輸入字符是否為字母,輸出內容詳見輸出樣例。
輸入:
A
6
輸出:
A is an alphabet.
6 is not an alphabet.
參考代碼:
#include <stdio.h>int main()
{int ch = 0;while ((ch = getchar()) != EOF){if (isalpha(ch)){printf("%c is an alphabet.\n", ch);}else{printf("%c is not an alphabet.\n", ch);}getchar();}return 0;
}
2.三角形判斷
題目描述:
KK想知道已經給出的三條邊a,b,c能否構成三角形,如果能構成三角形,判斷三角形的類型(等邊三角形、等腰三角形或普通三角形)。
輸入描述:
題目有多組輸入數據,每一行輸入三個a,b,c(0<a,b,c<1000),作為三角形的三個邊,用空格分隔。
輸出描述:
針對每組輸入數據,輸出占一行,如果能構成三角形,等邊三角形則輸出“Equilateral triangle!”,等腰三角形則輸出“Isosceles triangle!”,其余的三角形則輸出“Ordinary triangle!”,反之輸出“Not a triangle!”。
輸入:
2 3 2
3 3 3
輸出:
Isosceles triangle!
Equilateral triangle!
參考代碼:
#include <stdio.h>int main()
{int a = 0;int b = 0;int c = 0;while (scanf("%d %d %d", &a, &b, &c) != EOF){if (a + b > c || a + c > b || c + b > a){if (a == b && b == c){printf("Equilateral triangle!\n");}else if ((a==b && a!= c) || (a==c && a!=b) ||(b==c && b!= a)){printf("Isosceles triangle!\n");}else{printf("Ordinary triangle!\n");}}else{printf("Not a triangle!\n");}}return 0;
}
3.衡量人體胖瘦程度
題目描述:
在計算BMI(BodyMassIndex ,身體質量指數)的案例基礎上,判斷人體胖瘦程度。BMI中國標準如下表所示。
BMI范圍 | 分類 |
---|---|
BMI<18.5 | 偏瘦(Underweight) |
BMI>=18.5且BMI<=23.9 | 正常(Normal) |
BMI>23.9且BMI<=27.9 | 過重(Overweight) |
BMI>27.9 | 肥胖(Obese) |
輸入描述:
多組輸入,每一行包括兩個整數,用空格隔開,分別為體重(公斤)和身高(厘米)。
輸出描述:
針對每行輸入,輸出為一行,人體胖瘦程度,即分類。
輸入:
80 170
60 170
90 160
50 185
輸出:
Overweight
Normal
Obese
Underweight
參考代碼:
#include <stdio.h>int main()
{int h = 0;int w = 0;double bmi = 0.0;while (scanf("%d %d", &w, &h) != EOF){bmi = w / ((h / 100.0) * (h / 100.0));if (bmi < 18.9)printf("Underweight\n");else if (bmi >= 18.5 && bmi <= 23.9)printf("Normal\n");else if (bmi > 23.9 && bmi <= 27.9)printf("Overweight\n");elseprintf("Obese\n");}return 0;
}
4.翻轉金字塔圖案
題目描述:
KK學習了循環,BoBo老師給他出了一系列打印圖案的練習,該任務是打印用“*”組成的翻轉金字塔圖案。
輸入描述:
多組輸入,一個整數(2~20),表示翻轉金字塔邊的長度,即“*”的數量,也表示輸出行數。
輸出描述:
針對每行輸入,輸出用“”組成的金字塔,每個“”后面有一個空格。
輸入:
5
輸出:
* * * * * * * * ** * * * * *
參考代碼:
#include <stdio.h>int main()
{int n = 0;while (~scanf("%d", &n)){int i = 0;for (i = 0; i < n; i++){int j = 0;for (j = 0; j < i; j++){printf(" ");}for (j = 0; j < n-i; j++){printf("* ");}printf("\n");}}return 0;
}
5.平均身高
題目描述:
從鍵盤輸入5個人的身高(米),求他們的平均身高(米)。
輸入描述:
一行,連續輸入5個身高(范圍0.00~2.00),用空格分隔。
輸出描述:
一行,輸出平均身高,保留兩位小數。
輸入:
1.68 1.75 1.82 1.60 1.92
輸出:
1.75
參考代碼:
#include <stdio.h>int main()
{float score = 0.0;float sum = 0.0;int i = 0;for (i = 0; i < 5; i++){scanf("%f", &score);sum += score;}printf("%.2f\n", sum/5.0);return 0;
}