1.簡單計算器
題目描述:
KK實現一個簡單計算器,實現兩個數的“加減乘除”運算,用戶從鍵盤輸入算式“操作數1運算符操作數2”,計算并輸出表達式的值,如果輸入的運算符號不包括在(+、-、*、/)范圍內,輸出“Invalidoperation!”。當運算符為除法運算,即“/”時。如果操作數2等于0.0,則輸出“Wrong!Division by zero!”
輸入描述:
多組輸入,一行,操作數1運算符操作數2(其中運算符號包括四種:+、-、*、/)。
輸出描述:
針對每組輸入,輸出為一行。如果操作數和運算符號均合法,則輸出一個表達式,操作數1運算符操作數2=運算結果,各數小數點后均保留4位,數和符號之間沒有空格。如果輸入的運算符號不包括在(+、-、*、/)范圍內,輸出“Invalid operation!”。當運算符為除法運算,即“/”時。如果操作數2等于0.0,則輸出“Wrong!Division by zero!”。
輸入:
1.0+3.0
1.0;4.0
44.0/0.0
輸出:
1.0000+3.0000=4.0000
Invalid operation!
Wrong!Division by zero!
參考代碼:
#include <stdio.h>int main()
{double x1 = 0;double x2 = 0;char ch = 0;while (scanf("%lf%c%lf", &x1, &ch, &x2) != EOF){switch (ch){case '+':printf("%.4lf%c%.4lf=%.4lf\n", x1, ch, x2, x1+x2);break;case '-':printf("%.4lf%c%.4lf=%.4lf\n", x1, ch, x2, x1- x2);break;case '*':printf("%.4lf%c%.4lf=%.4lf\n", x1, ch, x2, x1 * x2);break;case '/':if (x2 == 0.0){printf("Wrong!Division by zero!\n");break;}printf("%.4lf%c%.4lf=%.4lf\n", x1, ch, x2, x1 / x2);break;default:printf("Invalid operation!\n");break;}}return 0;
}
2.獲得月份天數
題目描述:
KK想獲得某年某月有多少天,請幫他編程實現。輸入年份和月份,計算這一年這個月有多少天。
輸入描述:
多組輸入,一行有兩個整數,分別表示年份和月份,用空格分隔。
輸出描述:
針對每組輸入,輸出為一行,一個整數,表示這一年這個月有多少天。
輸入:
2008 2
輸出:
29
參考代碼:
#include <stdio.h>int main()
{int year = 0;int month = 0;int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31};while (scanf("%d %d", &year, &month) != EOF){int day = days[month - 1];if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){if (month == 2){day += 1;}}printf("%d\n", day);}return 0;
}
3.HTTP狀態碼
題目描述:
KK訪問網站,得到HTTP狀態碼,但他不知道什么含義,BoBo老師告訴他常見HTTP狀態碼:
200(OK,請求已成功),202(Accepted,服務器已接受請求,但尚未處理。)400(Bad
Request,請求參數有誤),403(Forbidden,被禁止),404(Not Found,請求失敗),
500(Internal Server Error,服務器內部錯誤),502(Bad Gateway,錯誤網關)。
輸入描述:
多組輸入,一行,一個整數(100~600),表示HTTP狀態碼。
輸出描述:
針對每組輸入的HTTP狀態,輸出該狀態碼對應的含義,具體對應如下:
200-OK
202-Accepted
400-Bad Request
403-Forbidden
404-Not Found
500-Internal Server Error
502-Bad Gateway
輸入:
200
輸出:
OK
參考代碼:
#include <stdio.h>int main()
{int n = 0;while (scanf("%d", &n) != EOF){switch (n){case 200:printf("OK\n");break;case 202:printf("Accepted\n");break;case 400:printf("Bad Request\n");break;case 403:printf("Forbidden\n");break;case 404:printf("Not Found\n");break;case 500:printf("Internal Server Error\n");break;case 502:printf("Bad Gateway\n");break;default:break;}}
}
4.?圖像相似度
題目描述:
給出兩幅相同大小的黑白圖像(用0-1矩陣)表示,求它們的相似度。若兩幅圖像在相同位置上的像素
點顏色相同,則稱它們在該位置具有相同的像素點。兩幅圖像的相似度定義為相同像素點數占總像素點數的百分比。
輸入描述:
第一行包含兩個整數m和n,表示圖像的行數和列數,用單個空格隔開。1≤m≤100, 1≤n≤100。之后m行,每行n個整數0或1,表示第一幅黑白圖像上各像素點的顏色,相鄰兩個數用單個空格隔開。之后m行,每行n個整數0或1,表示第二幅黑白圖像上各像素點的顏色,相鄰兩個數用單個空格隔開。
輸出描述:
一個實數,表示相似度(以百分比的形式給出),精確到小數點后兩位。
輸入:
3 3
1 0 1
0 0 1
1 1 0
1 1 0
0 0 1
0 0 1
輸出:
44.44
參考代碼:
#include <stdio.h>int main()
{int m = 0;int n = 0;int a[100][100] = { 0 };int b[100][100] = { 0 };float count = 0.0;int i = 0;int j = 0;scanf("%d %d", &m, &n);for (i = 0; i < m; i++){for (j = 0; j < n; j++){scanf("%d", &a[i][j]);}}for (i = 0; i < m; i++){for (j = 0; j < n; j++){scanf("%d", &b[i][j]);}}for (i = 0; i < m; i++){for (j = 0; j < n; j++){if (a[i][j] == b[i][j]){count++;}}}printf("%.2f\n", 100.0 * count / (m*n));return 0;
}
5.有序序列插入一個數
題目描述:
有一個有序數字序列,從小到大排序,將一個新輸入的數插入到序列中,保證插入新數后,序列仍然是升序。
輸入描述:
第一行輸入一個整數(0≤N≤50)。第二行輸入N個升序排列的整數,輸入用空格分隔的N個整數。第三行輸入想要進行插入的一個整數。
輸出描述:
輸出為一行,N+1個有序排列的整數。
輸入:
5
1 6 9 22 30
8
輸出:
1 6 8 9 22 30
參考代碼:
#include <stdio.h>int main(){int n = 0;int m = 0;int arr[50] = { 0 };int i = 0;scanf("%d", &n);for (i = 0; i < n; i++){scanf("%d", &arr[i]);}scanf("%d", &m);for (i = n; i > 0; i--){if (arr[i-1] > m){arr[i] = arr[i - 1];}else{arr[i] = m;break;}}if (i == 0){arr[0] = m;}for (i = 0; i < n + 1; i++){printf("%d ", arr[i]);}return 0;}