1.简单计算器
题目描述:
KK实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalidoperation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“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想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。
输入描述:
输出描述:
输入:
输出:
参考代码:
#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,错误网关)。
输入描述:
输出描述:
输入:
输出:
参考代码:
#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矩阵)表示,求它们的相似度。若两幅图像在相同位置上的像素
输入描述:
输出描述:
输入:
3 3
1 0 1
0 0 1
1 1 0
1 1 0
0 0 1
0 0 1
输出:
参考代码:
#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.有序序列插入一个数
题目描述:
有一个有序数字序列,从小到大排序,将一个新输入的数插入到序列中,保证插入新数后,序列仍然是升序。
输入描述:
输出描述:
输入:
输出:
参考代码:
#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;
}