School‘s test

在这里插入图片描述

题目栏

  • 第三周
    • 学分绩计算
    • 一尺之捶,日取其半
    • 网购打折商品
    • 计算时间差
  • 第四周
    • 分数比较
    • 存款利率计算器v2.0
    • 存款利率计算器v3.0
    • 博弈论之Best Response
  • 第五周
    • 马克思手稿中的趣味数学题
    • 猜神童年龄
      • mooc通过版
      • 输入猜年龄版
    • 闰年相关的问题
    • 闰年相关的问题v4.0——计算心跳数
  • 第六周
    • 计算阶乘的和v2.0
    • 计算最大的三位约数
    • 孔融分梨
    • 素数求和
  • 第七周
    • n层嵌套平方根的计算
    • 递归法求和
    • 猴子吃桃程序_扩展3
    • 网购打折商品V2.0
  • 第八周
    • 摘苹果
    • 好数对
    • 组合三位数
      • 超时版本
      • 时间优化版本(mooc通关版)
    • 编程计算n(n<=500)以内的10个最大素数及其和
  • 第九周
    • 重复数字检查
    • 教授的课
    • 寻找鞍点
    • 计算三位阶乘和数
  • 第十周
    • 数字字符串转换为整型数
    • 查找子串
    • 统计重复字符
    • 凯撒密码
  • 第十一周
    • 山地训练(mooc通关,但mooc出错版)
      • 正确版本fun函数
    • 奇偶数分离
    • 子串判断
    • 星期查找
  • 第十二周
    • 计算时间差V2.0
    • 奖学金发放
    • 评选最牛群主v1.0
    • 星期判断
  • PTA题目
    • 1、计算分段函数(pta)
    • 3、计算天数(pta)
    • 4、求最大值及其下标(pta)
    • 5.统计数字字符和空格
    • 17、找最小字符串(pta)
    • 8、字符串逆序(pta)
    • 10、查找书籍(Pta)
    • 12、数字统计(个位数统计)(pta)
    • 14、统计素数并求和(pta)
    • 19、逆序的三位数(pta)
    • 20、字符串排序(pta)
    • 11、a串数列求和

第三周

学分绩计算

#include 
int main()
{
	int m1, e, m2;
	printf("Input math1, English and math2:");
	scanf("%d,%d,%d", &m1, &e, &m2);
	float res = (m1 * 5 + e * 1.5 + m2 * 3.5) / 10;
	printf("Final score = %.2f\n", res);
	return 0;
}

一尺之捶,日取其半

#include 
int main()
{
	float len;
	int n = 0;
	printf("Input length and days:");
	scanf("%f,%d", &len, &n);
	int i = 0;
	for (i = 0; i < n; i++)
	{
		len /= 2;
	}
	printf("length=%.5f\n", len);
	return 0;
}

网购打折商品

#include 
int main()
{
	float p = 0;
	printf("Input payment p:");
	scanf("%f", &p);
	float res = p - p * 0.08;
	printf("price = %.1f\n", res);
	return 0;
}

计算时间差

#include 
#include 
int main()
{
	int h1, m1,h2,m2;
	printf("Input time one(hour, minute):");
	scanf("%d,%d", &h1, &m1);
	printf("Input time two(hour, minute):");
	scanf("%d,%d", &h2, &m2);
	if (h1 >= 0 && h1 <= 24 && h2 >= 0 && h2 <= 24 && m1 >= 0 && m1 <= 60 && m2 >= 0 && m2 <= 60)
	{
		int sub_m =fabs((h1 * 60 + m1) - (h2 * 60 + m2));
		int h = sub_m / 60, m = sub_m % 60;
		printf("%d hour %d minute\n", h,m);

	}
	return 0;

}

第四周

分数比较

#include 
int main()
{

		int a, b, c, d;
		printf("Input a/b, c/d:");
		scanf("%d/%d,%d/%d", &a, &b, &c, &d);
		float res1 = (float)a / b, res2 = (float)c / d;//需要将类型强制转换为float,防止数据损失
		if (res1 > res2)
			printf("%d/%d>%d/%d\n", a, b, c, d);
		else if (res1 < res2)
			printf("%d/%d<%d/%d\n", a, b, c, d);
		else
			printf("%d/%d=%d/%d\n", a, b, c, d);
	

	return 0;
}

存款利率计算器v2.0

#include 
#include 
#include 
int main()
{
	double rate,capital;
	int year;
	char n = 0;
	double res = 0;
	printf("Input rate, year, capital:");
	scanf("%lf,%d,%lf", &rate, &year, &capital);
	printf("Compound interest (Y/N)?");
	scanf(" %c", &n);
	if (n=='Y'||n=='y')
	{
		res = capital * pow(1 + rate, year);
		printf("deposit = %.4f\n", res);
	}
	if(n=='N'||n=='n')
	{
		res = capital * (1 + rate*year);
		printf("deposit = %.4f\n", res);
	}
	return 0;
}

存款利率计算器v3.0

#include 
#include 
#include 
double get_rate(int n)
{
	if (n == 1)
		return 0.0225;
	if (n == 2)
		return 0.0243;
	if (n == 3)
		return 0.0270;
	if (n == 5)
		return 0.0288;
	if (n == 8)
		return 0.0300;
}
int main()
{
	double rate, capital;
	int year;
	char n = 0;
	double res = 0;
	printf("Input capital, year:");
	scanf("%lf,%d", &capital,&year);
	printf("Compound interest (Y/N)?");
	scanf(" %c", &n);
	if (year != 1 && year != 2 && year != 3 && year != 5 && year != 8)
	{
		printf("Error year!\n");
		return 0;
	}
	else
	{
		rate = get_rate(year);
	}
	if (n == 'Y'||n=='y')
	{
		res = capital * pow(1 + rate, year);
		printf("rate = %.4f, deposit = %.4f\n",rate, res);
	}
	if (n == 'n'||n=='N')
	{
		res = capital * (1 + rate * year);
		printf("rate = %.4f, deposit = %.4f\n",rate, res);
	}

	return 0;
}

博弈论之Best Response

int main()
{
	float a, b;
	printf("Input percent of A and B:");
	scanf("%f%f", &a, &b);
	float compete = a * 10.0 + b * 6.0;
	float standard = a * 8.0 + b * 10.0;
	if (compete > standard)
	{
		printf("compete = %.4f\nstandard = %.4f\n", compete, standard);
		printf("The Best Response is compete!");
	}
	else
	{
		printf("compete = %.4f\nstandard = %.4f\n", compete, standard);
		printf("The Best Response is standard!");
	}
	return 0;
	}

第五周

马克思手稿中的趣味数学题

int main()
{
	int i, j, k;
	printf("Man   Women   Children\n");
	//我们的思路就是将男人,女人,小孩从0~30人数中所有可能情况遍历过去
	for (i = 0; i <= 10; i++)//男人最多就只能有10人
	{
		for (j = 0; j <= 20; j++)//女人最多就只能有20人
		{
			for (k = 0; k <= 30; k++)
			{
				if ((i * 3 + j * 2 + k == 50)&&(i+j+k==30))
				{
					printf("%3d%8d%8d\n", i, j, k);
				}
			}
		}
	}
	return 0;
}

猜神童年龄

mooc通过版

int getlen(int n)
{
	if (n > 9)
		return getlen(n / 10) + 1;
	else
		return 1;
}
int main()
{
	int i = 0;
	int arr[30] = { 0 };
	for (i = 10; i <= 22; i++)
	{
		int j = 0, k = 0;
			int n_4 = pow(i, 3);
	       int n_6 = pow(i, 4);
			while (n_4)
	{
		arr[j] = n_4 % 10;
		n_4 /= 10;
		j++;
	}
	while (n_6)
	{
		arr[j] = n_6 % 10;
		n_6 /= 10;
		j++;
	}
	//遍历
		int flag = 1;
	for (j = 0; j < 10; j++)
	{
		for (k = j + 1; k < 10; k++)
		{
			if (arr[j] == arr[k])
			{
				flag = 0;
				goto again;
			}
		}
	}
again:
	if (flag == 1)
	{
		printf("age=%d\n", i);
		break;
	}

	}
	return 0;
}

输入猜年龄版

int getlen(int n)
{
	if (n > 9)
		return getlen(n / 10) + 1;
	else
		return 1;
}
int main()
{
	int n = 0;
	printf("请输入年龄(10~22):");
	scanf("%d", &n);
	if (n < 10 || n>22)
	{
		return 0;
	}
	int n_4 = pow(n, 3);
	int n_6 = pow(n, 4);
	int len_4 = getlen(n_4);
	int len_6 = getlen(n_6);
	if (len_4 != 4 || len_6 != 6)
	{
		printf("该年龄不满足3立方为4位数或4立方为6位数\n");
		return 0;
	}
	int i = 0;
	int arr[30] = { 0 };
	//将4位数和6位数中的数分别拆分存入输入中
	while (n_4)
	{
		arr[i] = n_4 % 10;
		n_4 /= 10;
		i++;
	}
	while (n_6)
	{
		arr[i] = n_6 % 10;
		n_6 /= 10;
		i++;
	}
	//此时遍历其中是否有重复数字
	int flag = 1;
	for (i = 0; i < 10; i++)
	{
		int j = 0;
		for (j = i+1; j <10 ; j++)
		{
			if (arr[i] == arr[j])
			{
				flag = 0;
				goto again;
			}
		}
	}
      again:
	if (flag == 1)
		printf("age=%d\n", n);
	else
		printf("不是\n");
	return 0;
}

闰年相关的问题

int judge(int n)
{
	if ((n % 100 == 0 && n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
		return 1;
	else
		return 0;
}
int main()
{
	int n = 0,m=0;
	printf("Input your birth year:");
	scanf("%d", &n);
	printf("Input this year:");
	scanf("%d", &m);
	int i = 0;
	int count = 0;
	for (i = n; i <= m; i++)
	{
		if (judge(i))
		{
			printf("%d\n", i);
			count++;
		}
	}
	printf("count=%d\n", count);


}

闰年相关的问题v4.0——计算心跳数

//我们知道,一分钟心跳跳75下,一小时就是4500,一天24小时就是108000
//所以我们只要得到某年的天数即可求出一年中的总心跳数
int ret_days(int n)
{
	if ((n % 100 == 0 && n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
		return 366;//闰年返回366天
	else
		return 365;
}
int main()
{
	int n, m;
	printf("Input your birth year:");
	scanf("%d", &n);
	printf("Input this year:");
	scanf("%d", &m);
	int i = 0;
	unsigned long sum = 0;
	for (i = n; i < m; i++)
	{
		int res = ret_days(i)* 108000;
		sum += res;
	}
	printf("The heart beats in your life: %lu", sum);
	return 0;
}

第六周

计算阶乘的和v2.0

int get_n(int n)
{
	int sum = 1;
	int res = n;
	while (n)
	{
		sum *= n;
		n--;
	}
	if (res != 0)
		return sum;
	else
		return 1;
}
int main()
{
	int i = 0,j=0;
	int sum = 0;
	for (i = 100; i < 1000; i++)
	{
		int n = i;
		for (j = 0,sum=0; j < 3; j++)
		{
			int ret = get_n(n%10);
			sum += ret;
			n /= 10;
		}
		if (sum == i)
		{
			printf("%d\n", sum);
		}
		
	}
}

计算最大的三位约数

int judge(int n,int x)
{
	if (n % x == 0)
		return 1;
	else
		return 0;
}
int main()
{
	int n = 0;
	printf("Input n:");
	scanf("%d", &n);
	if (!(n >= 1000 && n <= 1000000))
	{
		printf("Input error!\n");
		return 0;
	}
	int i = 0;
	for (i = 999; i >= 100; i--)
	{
		if (judge(n, i))
		{
			printf("%d\n", i);
			return 0;
		}
	}

}

孔融分梨

int Gcd(int a, int b)
{
	int min = a < b ? a : b;
	int i = 0;
	for (i = min; i >= 1; i--)
	{
		if (a % i == 0 && b % i == 0)
		{
			return i;
		}
	}
}
int main()
{
	int m,n;
	printf("Input m,n:");
	scanf("%d,%d", &m, &n);
	if (m < 1 || m>10000 || (n < 1 || n>10000))
	{
		printf("Input error!\n");
		return 0;
	}
	int ret = Gcd(m, n);
	int spf_m = m / ret;
	int spf_n = n / ret;
	printf("%d/%d\n", spf_m, spf_n);

	return 0;
}

素数求和

int IsPrime(int n)
{
	int i = 0;
	int flag = 1;
	for (i = 2; i < n; i++)
	{
		if (n % i == 0)
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1&&n!=1)//最小的素数是2,1不是
		return 1;
	else
		return 0;
}
int main()
{
	int n = 0;
	int sum = 0;
	int i = 0;
	printf("Input n:");
	scanf("%d", &n);
	if (n <=2)
	{
		printf("sum=%d\n",sum);
		return 0;
	}
	else
	{
		for (i = 2; i <= n; i++)
		{
			if (IsPrime(i))
				sum += i;
		}
		printf("sum=%d\n", sum);
	}
	return 0;
}

注意,1不是素数,最小素数是2

第七周

n层嵌套平方根的计算

#include 
//我们使用递归思想搞清楚f(n)与f(n-1)之间的关系
//f(n)=sqrt(x+f(n-1))
double fun(double x, int n)
{
	if (n > 1)
		return sqrt(x + fun(x, n - 1));
	else if (n == 1)
		return sqrt(x);
	else
		return 0;
}
int main()
{
	double x;
	int n;
	printf("Please input x and n:");
	scanf("%lf,%d", &x, &n);
	double res = fun(x, n);
	printf("Result=%.2f\n", res);
	return 0;
}

递归法求和

int fun(int n)
{
	if (n > 1)
		return fun(n - 1) + n;
	else
		return 1;
}
int main()
{
	int n = 0;
	printf("Please input n:");
	scanf("%d", &n);
	if (n < 1)
	{
		printf("data error!\n");
		return 0;
	}
	int sum = fun(n);
	printf("sum=%d\n", sum);
	return 0;
}

猴子吃桃程序_扩展3

//我们创建一个函数fun,返回吃了n天时,第一天摘的桃子数
int fun(int n)
{
	if (n > 1)
		return (fun(n - 1) + 1) * 2;
	else
		return 1;
	
}
int main()
{
	int n = 0;
	printf("Input days n:");
	scanf("%d", &n);
	int x = fun(n);
	printf("x=%d\n", x);
	return 0;
}

我们随便举个例子就可以发现其中的规律
School‘s test_第1张图片
我们可以发现f(4)=(f(3)+1)x2;
由此得到f(n)与f(n-1)的关系
f(n)=(f(n-1)+1)x2

网购打折商品V2.0

int main()
{
	float n = 0;
	printf("Input payment:");
	scanf("%f", &n);
	int c = n / 100;
	if (c < 1)
		n = n;
	else if (c >= 1 && c < 2)
		n = n * 0.95;
	else if (c >= 2 && c < 5)
		n = n * 0.92;
	else if (c >= 5 && c < 10)
		n = n * 0.90;
	else
		n = n * 0.85;
	printf("price = %.1f\n", n);
	return 0;
}

第八周

摘苹果

int judge(int arr[], int n)
{
	int i = 0;
	int flag = 1;
	for (i = 0; i < 10; i++)
	{
		if (arr[i] < 100 || arr[i]>200)
		{
			flag = 0;
			break;
		}
	}
	if (flag == 0 || (n < 100 || n>120))
		return 1;
	else
		return 0;
}
int get_num(int arr[], int n)
{
	int count = 0;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		if (n + 30 >= arr[i])
			count++;
	}
	return count;
}
int main()
{
	int arr[10] = { 0 };
	int i = 0;
	int n = 0;
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	scanf("%d", &n);
	//此时创建一个函数判断输入的数值是否在合理范围内
	//if (judge(arr, n))//我们需要这个函数,但sb oj不让通过
	//{
	//	return 0;
	//}
	int res = get_num(arr, n);
	printf("%d", res);

	return 0;
}

好数对

int search_sim(int arr[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		if (arr[i] == n)
			return 1;
	}
	return 0;
}
int main()
{
	int arr[1000] = { 0 };
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	int count = 0;
	for (i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (i = 0; i < n; i++)
	{
		int j = 0;
		for (j = i + 1; j < n; j++)
		{
			if (arr[i] != arr[j])
			{
				if (search_sim(arr, arr[i] + arr[j]))//创建一个函数判断集合中有元素等于其和
				{
					count++;
				}
			}
		}
	}
	printf("%d", count);

	return 0;
}

组合三位数

超时版本

#include 
int judge(int n)
{
	int arr[10] = { 0 };
	int count = 0;
	while (n)//将每一位装进数组当中
	{
		arr[count] = n % 10;
		count++;
		n /= 10;
	}
	//开始遍历数组中是否有重复数字
	int flag = 1,i;
	for (i = 0; i < count; i++)
	{
		int j = 0;
		for (j = i + 1; j < count; j++)
		{
			if (arr[i] == arr[j])
			{
				flag = 0;
				goto end;
			}
		}
	}
end:
	if (flag == 1)//若无重复,返回1
		return 1;
	else
		return 0;
}
int main()
{
	int i, j, k;
	int res = 0;
	for (i = 100; i <= 333; i++)
	{
		if (judge(i))//先判断i中是否有重复数字 
		{
			for (j = 200; j <= 666; j++)
			{
				if (judge(j))
				{
					for (k = 300; k <= 999; k++)
					{
						if (judge(k))
						{
							//三位数中已经判断都无重复数字,接下来要判断9个数字中是否有重复数字
							int num_9 = i * pow(10, 6) + j * pow(10, 3) + k;
							if (judge(num_9))
							{
								//接下来判断是否满足相应的倍数关系
								if (k == 3 * i && j ==2 * i)
								{
									printf("%d,%d,%d\n", i,j,k);
								}
							}
						}
					}
				}
			}
		}
	}
     
	return 0;

}

时间优化版本(mooc通关版)

#include 
int judge(int n)
{
	int arr[10] = { 0 };
	int count = 0;
	while (n)//将每一位装进数组当中
	{
		arr[count] = n % 10;
		count++;
		n /= 10;
	}
	//开始遍历数组中是否有重复数字
	int flag = 1, i;
	for (i = 0; i < count; i++)
	{
		int j = 0;
		for (j = i + 1; j < count; j++)
		{
			if (arr[i] == arr[j])
			{
				flag = 0;
				goto end;
			}
		}
	}
end:
	if (flag == 1)//若无重复,返回1
		return 1;
	else
		return 0;
}
int main()
{
	int i = 0,j=0,k=0;
	for (i = 100; i <= 333; i++)
	{
		j = 2 * i;//第二个三位数
		k = 3 * i;//第三个三位数
		int num_9 = i * pow(10, 6) + j * pow(10, 3) + k;
		if (judge(num_9))
		{
			if (k == 3 * i && j == 2 * i)
			{
				printf("%d,%d,%d\n", i, j, k);
			}
												
		}
	}
	return 0;
}

效果如下
School‘s test_第2张图片

编程计算n(n<=500)以内的10个最大素数及其和

int IsPrime(int n)
{
	int i = 0;
	int flag = 1;
	for (i = 2; i < n; i++)
	{
		if (n % i == 0)
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1&&n!=1)//最小的素数是2,1不是
		return 1;
	else
		return 0;
}
int main()
{
	int n = 0;
	printf("Input n(n<=500):");
	scanf("%d", &n);
	if (n < 1 || n>500)
	{
		return 0;
	}
	int i = 0;
	int sum = 0;
	int count = 0;
	while (count < 10)
	{
		if (IsPrime(n))
		{
			printf("%6d", n);
			sum += n;
			count++;
		}
		n--;
		if (n < 1)
			break;
	}
	printf("\nsum=%d\n", sum);

	return 0;
}

第九周

重复数字检查

int main()
{
	long n = 0;
	long arr[100] = { 0 };
	int i = 0,j=0,k=0;
	printf("Input n:\n");
	scanf("%ld", &n);
	while (n)
	{
		arr[i] = n % 10;
		i++;
		n /= 10;
	}
	int flag = 1;
	for (j = 0; j < i; j++)
	{
		for (k = j + 1; k < i; k++)
		{
			if (arr[j] == arr[k])
			{
				flag = 0;
				goto end;
			}
		}
	}
end:
	if (flag == 1)
		printf("No repeated digit!\n");
	else
		printf("Repeated digit!\n");

	return 0;
}

教授的课

int main()
{
	int n, k;//n为学生人数,k为教授能容忍的最低学生人数
	int arr[1000] = { 0 };
	int i = 0;
	int count = 0;
	printf("Input n,k:\n");
	scanf("%d,%d", &n, &k);
	//判断学生人数是否不合理
	if (n < 0 || n>1000)
		return 0;
	for (i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
		//接下来还要再判断学生到达时间的绝对值是否超过100
		if (fabs(arr[i]) > 100)
			return 0;
		//接下来对未迟到的人进行计数
		if (arr[i] < 0)
			count++;
	}
	if (count < k)
		printf("YES");
	else
		printf("NO");
	return 0;
}

寻找鞍点

int IsSaddle(int a,int arr[][100],int n,int i,int j)
{
	int k=0;
	for (k = 0; k < n; k++)
	{
		if ( arr[k][j]< a||arr[i][k]>a)
		{
			return 0;//一旦有任何情况不符合,则元素是该行上的最大值,是该列上的最小值的条件不成立
		}
	}
	return 1;
}
int main()
{
	int arr[100][100] = { 0 };
	int n, m;
	printf("Input m,n:\n");
	scanf("%d,%d", &n, &m);
	printf("Input matrix:\n");
	int i = 0, j = 0;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			scanf("%d", &arr[i][j]);
		}
	}
	//接下来我们遍历所有的元素,若符合条件的则打印出来,若一个都没有,则输出"No saddle point!\n"
	int flag = 0;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (IsSaddle(arr[i][j],arr,n,i,j))
			{
				printf("a[%d][%d] is %d\n", i,j,arr[i][j]);
				flag = 1;
			}
		}
	}
	if (flag == 0)
		printf("No saddle point!\n");
	return 0;
}

计算三位阶乘和数

long calculate(long unit, long tens, long hundred)
{
	long a = unit, b = tens, c=hundred;
	long fac_a = 1, fac_b = 1, fac_c = 1;
	while (a)
	{
		fac_a *= a;
		a--;
	}
	while (b)
	{
		fac_b *= b;
		b--;
	}
	while (c)
	{
		fac_c *= c;
		c--;
	}
	if (unit == 0)
	{
		fac_a  = 1;
	}
	if (tens == 0)
	{
		fac_b = 1;
	}
	if (hundred == 0)
	{
		fac_c = 1;
	}
	return fac_a + fac_b + fac_c;
}
int main()
{
	long i = 0;
	for (i = 100; i <= 999; i++)
	{
		long unit = i%10;
		long tens = (i - unit) / 10 % 10;
		long hundred = (i - tens * 10 - unit) / 100;
		long ret = calculate(unit, tens, hundred);
		if (ret == i)
		{
			printf("%ld\n", ret);
		}
	}
	return 0;
}

第十周

数字字符串转换为整型数

int main()
{
	char arr[10] = { 0 };
	printf("Input a string:");
	gets(arr);
	char* p = arr;
	int flag = 0;
	while (*p != '\0')
	{
		if (*p >= '0' && *p <= '9')
		{
			printf("%d", *p-'0');
			flag = 1;
		}
		p++;
	}
	if (flag == 0)
		printf("0");
	return 0;
}

查找子串

#include 
int main()
{
	char arr1[80] = {0};
	char arr2[80] = { 0 };
	printf("Input a string:");
	gets(arr1);
	printf("Input another string:");
	gets(arr2);
	char* p = strstr(arr1, arr2);
	if (p != NULL)
	{
		printf("Searching results:%d\n", p - arr1+1);
	}
	else
		printf("Not found!\n");

	return 0;
}

统计重复字符

#include 
int main()
{
	char arr1[80] = { 0 };
	char arr2[80] = { 0 };
	int count = 0;
	int sum = 0;
	char c=0;
	printf("Input a string:\n");
	gets(arr1);
	char* p = arr1;
	int i = 0;
	while (*p)
	{
		char* str = strchr(arr2, *p);//strchr用来查找字符串中的一个字符
		if (str == NULL)//如果这个字符是第一次出现,则存放到arr2数组当中去
		{
			arr2[i] = *p;
			i++;
		}
		p++;
	}
	//借此我们已经得到了输入字符串中所有第一次出现的字符
	//接下里我们就单独对其进行遍历计数,将出现最多的字符输出
	char* s = arr2;
	while (*s)
	{
		 count = 0;
		 p = arr1;
		while (*p)
		{
			if (*p == *s)
				count++;
			p++;
		}
		if (count >= sum)
		{
			c = *s;
			sum = count;
		}
		s++;
	}
	printf("%c:%d\n", c, sum);
	return 0;
}

凯撒密码

int main()
{
	char arr1[100] = { 0 };
	char arr2[100] = { 0 };
	printf("Input a string:");
	gets(arr1);
	char* p1 = arr1;
	char* p2 = arr2;
	while (*p1)
	{
		if (*p1 < 'x')
			*p2 = *p1+3;
		else
			*p2 = *p1 - 23;
		p1++;
		p2++;
	}
	puts(arr2);
	return 0;
}

第十一周

山地训练(mooc通关,但mooc出错版)

long Fun(long M, long T, long U, long F, long D, char* str)
{
	int count = 0;
	long time_all = 0;//time_all算的是往返总时间
	while (*str)
	{
		if (*str == 'u' || *str == 'd')
			time_all += (U + D);
		else
			time_all += 2*F;
		if (time_all > M)
			break;
		count++;
		str++;
	}
	return count-1;//实际应为count
}
int main()
{
	long M, T, U, F, D;
	char arr[100] = { 0 };
	printf("Input M,T,U,F,D:");
	scanf("%ld%ld%ld%ld%ld", &M, &T, &U, &F, &D);
	printf("Input conditions of road:");
	scanf("%s", arr);
	long ret=Fun(M, T, U, F, D, arr);
	printf("num=%ld\n", ret);
}

正确版本fun函数

long Fun(long M, long T, long U, long F, long D, char* str)
{
	int count = 0;
	long time_all = 0;//time_all算的是往返总时间
	while (*str)
	{
		if (*str == 'u' || *str == 'd')
			time_all += (U + D);
		else
			time_all += 2*F;
		if (time_all > M)
			break;
		count++;
		str++;
	}
	return count;
}

奇偶数分离

int main()
{
	int arr[100] = { 0 };
	int n = 0;
	printf("Input n:");
	scanf("%d", &n);
	int i = 0;
	printf("Input numbers:");
	for (i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	//打印奇数
	int count = 0;
	for (i = 0; i < n; i++)
	{
		if (arr[i] % 2 == 1 && count == 0)
		{
			printf("%d", arr[i]);
			count++;
			continue;
		}
		if (arr[i] % 2 == 1 && count == 1)
			printf(",%d", arr[i]);
	}
	printf("\n");
	//打印偶数
	for (i = 0,count=0; i < n; i++)
	{
		if (arr[i] % 2 == 0 && count == 0)
		{
			printf("%d", arr[i]);
			count++;
			continue;
		}
		if (arr[i] % 2 == 0 && count == 1)
			printf(",%d", arr[i]);
	}

	return 0;
}

子串判断

#include 
int main()
{
	char arr1[100] = { 0 };
	char arr2[100] = { 0 };
	printf("Input the first string:");
	gets(arr1);
	printf("Input the second string:");
	gets(arr2);
	if (strstr(arr1, arr2) != NULL)
		printf("Yes\n");
	else
		printf("No\n");

	return 0;
}

星期查找

#include 
int main()
{
	char arr[][10] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" };
	char str[10] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	printf("Please enter a string:\n");
	gets(str);
	int i = 0;
	int flag = 0;//假设没有
	for (i = 0; i < sz; i++)
	{
		if (strcmp(arr[i], str) == 0)
		{
			flag = 1;
			break;
		}
	}
	if (flag)
		printf("%s is %d\n", arr[i], i);
	else
		printf("Not found!\n");
	return 0;
}

第十二周

计算时间差V2.0

#include 
typedef struct clock

{
    int hour;
    int minute;
    int second;
} CLOCK;
int main()
{
    CLOCK t1, t2;
    printf("Input time one:(hour,minute):");
    scanf("%d,%d", &(t1.hour), &(t1.minute));
    printf("Input time two: (hour,minute):");
    scanf("%d,%d", &(t2.hour), &(t2.minute));
    int gap = fabs(t1.hour * 60 + t1.minute - t2.hour * 60 - t2.minute);
    int h = gap / 60,m=gap%60;
    printf("%dhour,%dminute\n", h, m);
    return 0;
}

奖学金发放


#include
typedef struct winners
{
	char name[20];
	int finalScore;
	int classScore;
	char work;
	char west;
	int paper;
	int scholarship;
} WIN;
 
main(){
int i,count=0,n,s;
 
  WIN stu[100];
 
 printf("Input n:");
 scanf("%d",&n);
 s = n;
 while (n--)
 {
	 stu[count].scholarship = 0;
	 printf("Input name:");
	 scanf("%s", &stu[count].name);
	 printf("Input final score:");
	 scanf("%d", &stu[count].finalScore);
	 printf("Input class score:");
	 scanf("%d", &stu[count].classScore);  
	 printf("Class cadre or not?(Y/N):");
	 scanf(" %c", &stu[count].work);
	 printf("Students from the West or not?(Y/N):");
	 scanf(" %c", &stu[count].west); 
	 printf("Input the number of published papers:");
	 scanf("%d", &stu[count].paper);
	 if (stu[count].finalScore > 90)
	 {
		 stu[count].scholarship += 2000;
	 }
	
	  if (stu[count].finalScore > 85&& stu[count].classScore>80)
	 {
		 stu[count].scholarship += 4000;
	 }
	
	  if(stu[count].classScore>80)
	 {
		 if (stu[count].work != 78)
		 {
			 stu[count].scholarship += 850;
		 }
	 }
	 
	  if (stu[count].finalScore>85)
	 {
		 if (stu[count].west != 78) {
			 stu[count].scholarship += 1000;
		 }
	 }
	
	  if (stu[count].finalScore > 80 && stu[count].paper)
	 {
		 stu[count].scholarship += 8000;
	 } 
	 printf("name:%s,scholarship:%d\n", stu[count].name, stu[count].scholarship);
	 count++;
	
}
 
 int max;
 count = 0;
 max = stu[0].scholarship;
 for (i = 0; i < s; i++)
 {
	 if (max < stu[i].scholarship)
	 {
		 max = stu[i].scholarship;
		 count = i;
		 
	 }
 }
 printf("%s get the highest scholarship %d\n", stu[count].name, max);
 
}

评选最牛群主v1.0

#include 
#include 
int main()
{
	int n = 0;
	int i = 0;
	int count[3] = { 0 };//0为tom,1为jack,2为rose
	char arr[20] = { 0 };
	char arr2[3][10] = { "tom","jack","rose" };
	int num = 0;
	int winner = 0;
	printf("Input the number of electorates:");
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		printf("Input vote %d:",i+1);
		scanf("%s", arr);
		char* str = arr;
		int flag = 1;//假设名字输入正确
		while (*str)
		{
			if (islower(*str) == 0)
			{
				flag = 0;
				break;
			}
			str++;
		}
		if (flag)
		{
			if (strcmp(arr, "tom") == 0)
				count[0]++;
			if (strcmp(arr, "jack") == 0)
				count[1]++;
			if (strcmp(arr, "rose") == 0)
				count[2]++;
		}
	}
		//选出胜者
	for (i = 0; i < 3; i++)
	{
		if (count[i] > winner)
		{
			winner = count[i];
			num = i;
		}
		}
	//打印票数
	printf("Election results:\n");
	for (i = 0; i < 3; i++)
	{
		printf("%s:%d\n", arr2[i], count[i]);
	}
	printf("%s wins\n", arr2[num]);
	

	return 0;
}

星期判断

#include 
#include 
int main()
{
	char a, b;
	char arr[] = "MmTtWwFfSs";
	printf("please input the first letter of someday:\n");
	scanf(" %c", &a);
	printf("please input second letter:\n");
	scanf(" %c", &b);
	if (strchr(arr, a) != NULL)
	{
		if (a == 'M' || a == 'm')
		{
			if (b == 'o')
				printf("monday\n");
			else
				printf("data error\n");
		}
		if (a == 'T' || a == 't')
		{
			if (b == 'u')
				printf("tuesday\n");
			else if (b == 'h')
				printf("thursday\n");
			else
				printf("data error\n");
		}
		if (a == 'W' || a == 'w')
		{
			if (b == 'e')
				printf("wednesday\n");
			else
				printf("data error\n");
		}
		if (a == 'F' || a == 'f')
		{
			if (b == 'r')
				printf("friday\n");
			else
				printf("data error\n");
		}
		if (a == 'S' || a == 's')
		{
			if (b == 'a')
				printf("saturday\n");
			else if (b == 'u')
				printf("sunday\n");
			else
				printf("data error\n");
		}
		
	}
	else
		printf("data error\n");
	return 0;
}

PTA题目

1、计算分段函数(pta)

计算分段函数(pta)

#include 
int main()
{
    float x;
    scanf("%f",&x);
    float res=0.0;
    if(x!=0)
        res=1.0/x;
    else
        res=0;
    printf("f(%.1f) = %.1f",x,res);
    return 0;
}

3、计算天数(pta)

计算天数(pta)

#include 
int IsPrime(int y)
{
    if((y%400==0&&y%100==0)||(y%4==0&&y%100!=0))
        return 1;
    else
        return 0;
}
int GetDays(int y,int m)
{
    if(m==4||m==6||m==9||m==11)
        return 30;
    else if(m==2)
    {
        if(IsPrime(y))
            return 29;
        else
            return 28;
    }
    else
        return 31;
}
int main()
{
    int year,month,day;
    scanf("%d/%d/%d",&year,&month,&day);
    int i=0;
    int sum=day;
    for(i=1;i<month;i++)
    {
        sum+=GetDays(year,i);
    }
    printf("%d",sum);
    return 0;
}

4、求最大值及其下标(pta)

求最大值及其下标(pta)

#include 
int main()
{
    int n=0,i=0;
    int arr[100]={0};
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int max=arr[0];
    int sub=0;
     for(i=1;i<n;i++)
    {
       if(arr[i]>max)
       {
           max=arr[i];
           sub=i;
       }
    }
    printf("%d %d",max,sub);
    return 0;
}

5.统计数字字符和空格

#include 
#include 
int main()
{
    char str[100];
    int Num[3] = { 0 };
    gets(str);
    int i = 0;
    while (*(str+i))
    {
        if (*(str+i)==' ')
            Num[0]++;
        else if (*(str+i)>='0'&& *(str + i)<='9')
            Num[1]++;
        else
            Num[2]++;
        i++;
    }
    printf("blank = %d, digit = %d, other = %d", Num[0], Num[1], Num[2]);

    return 0;
}

17、找最小字符串(pta)

#include
#include
int main()
{
    char str1[100], str2[100];
    int i = 0, n;
    scanf("%d", &n);
    for (i = 0; i < n ; i++)
    {
        scanf("%s", str1);
        getchar();
        if (i == 0)
            strcpy(str2, str1);
        if (strcmp(str1,str2)<=0)
        {
            strcpy(str2, str1);
        }
    }
    printf("Min is: %s", str2);
    return 0;
}

8、字符串逆序(pta)

#include 
#include 
void reverse(char* str)
{
    
    int left=0,right=strlen(str)-1;
    while(left<right)
    {
        char tmp=*(str+left);
        *(str+left)=*(str+right);
        *(str+right)=tmp;
        left++;
        right--;
    }
}
int main()
{
    char str[81];
    gets(str);
    reverse(str);
    puts(str);
    return 0;
}

10、查找书籍(Pta)

#include 
struct book
{
    char name[40];
    double price;
};
int main()
{
    struct book stu[1000];
    int n=0,i=0;
    scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++)
    {
        gets(stu[i].name);
        scanf("%lf",&stu[i].price);
        getchar();
    }
   int max=0,min=0;
    //找大找小
    for(i=1;i<n;i++)
    {
       if(stu[i].price>stu[max].price)
       {
           max=i;
       }
        if(stu[i].price<stu[min].price)
        {
            min=i;
        }
    }

    printf("%.2lf, %s\n",stu[max].price,stu[max].name);

    printf("%.2lf, %s",stu[min].price,stu[min].name);
    
    return 0;
}

这里有点坑,price的类型必须为double,不能为float

12、数字统计(个位数统计)(pta)

#include 
#include 

int main() {
    char str[1001];
    int count[10] = {0};
    scanf("%s", str);
    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        int digit = str[i] - '0';
        count[digit]++;
    }
    for (int i = 0; i < 10; i++) {
        if (count[i] > 0) {
            printf("%d:%d\n", i, count[i]);
        }
    }
    return 0;
}

14、统计素数并求和(pta)

#include 
int IsPrime(int n)
{
    if(n>1)
    {
        int i=2;
        for(i=2;i<n;i++)
        {
            if(n%i==0)
                return 0;
        }
        return 1;
    }
    else
        return 0;
}
int main()
{
    int m,n;
    int i=0,count=0,sum=0;
    scanf("%d %d",&m,&n);
    for(i=m;i<=n;i++)
    {
        if(IsPrime(i))
        {
            count++;
            sum+=i;
        }
    }
    printf("%d %d",count,sum);
    return 0;
}

19、逆序的三位数(pta)

#include 

int main() {
    int n;
    scanf("%d", &n);
    int a = n / 100;
    int b = n / 10 % 10;
    int c = n % 10;
    if (c == 0) {
        if (b == 0) {
            printf("%d\n", a);
        } else {
            printf("%d%d\n", b, a);
        }
    } else {
        printf("%d%d%d\n", c, b, a);
    }
    return 0;
}

20、字符串排序(pta)

#include 
#include 
int main()
{
    char str[5][81];
    char* p[5];
    int i=0,j=0;
    for(i=0;i<5;i++)
    {
        scanf("%s",&str[i]);
        p[i]=str[i];
    }
    //进行排序
    for(i=0;i<4;i++)
    {
        for(j=0;j<4-i;j++)
        {
            if(strcmp(p[j],p[j+1])>0)
            {
                char* tmp=p[j];
                p[j]=p[j+1];
                p[j+1]=tmp;
            }
        }
    }
    //打印
    printf("After sorted:\n");
      for(i=0;i<5;i++)
    {
        printf("%s\n",p[i]);
    }
    
    return 0;
}

11、a串数列求和

#include 
int main()
{
	int a, n,i=0;
	scanf("%d %d", &a, &n);
	int sum = 0,x=a;
	for (i = 0; i < n; i++)
	{
		sum += a;
		a =(a * 10 + x);
	}
	printf("%d", sum);
	return 0;
}

School‘s test_第3张图片


如上便是本期的所有内容了,如果喜欢并觉得有帮助的话,希望可以博个点赞+收藏+关注❤️ ,学海无涯苦作舟,愿与君一起共勉成长
School‘s test_第4张图片

在这里插入图片描述

你可能感兴趣的:(题目解析,c++,算法,开发语言)