GDPU C语言 天码行空3

1. 分段函数

#include<stdio.h>

int main(){
	double x,y;
	scanf("%lf", &x);
	y = x;
	if (x >= 1 && x < 10)
		y = x - 1;

	if (x >= 10)
		y = 3 * x - 11;

	printf("y=%.2lf", y);
	return 0;
}

2. 三角形判断

⭐ 海伦公式求面积
GDPU C语言 天码行空3_第1张图片
⭐ getchar() 吃掉输入缓冲区的换行符

#include
#include

int main(){
	double x1,y1,x2,y2,x3,y3,tmp,p,primeter,area;
	scanf("%lf,%lf", &x1,&y1);
	getchar();//吃掉换行符
	scanf("%lf,%lf", &x2, &y2);
	getchar();
	scanf("%lf,%lf", &x3, &y3);
	
	double a = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
	double b = sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2));
	double c = sqrt((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1));


//	判断是否构成三角形(任意两边之和大于第三边) 并且 每条边都不能为 0
	if (a + c > b  && a + b >c && b + c > a && a && b && c)
	{
		primeter = a + b + c;
		p = primeter / 2;
		area = sqrt(p*(p - a)*(p - b)*(p - c));
		printf("primeter=%.2lf,area=%.2lf", primeter, area);
		return 0;//输出完提前返回结束 main 函数
	}
	printf("Impossible");
	
	return 0;
}

3. 月份对应季节

⭐ case:里边要break 不然就会继续走下一个 case (当然可以利用)

#include
#include

int main(){
	int m;
	scanf("%d", &m);

	switch (m)
	{
	case 2:
	case 3:
	case 4:
		printf("Spring");
		break;
	case 5:
	case 6:
	case 7:
		printf("Summer");
		break;
	case 8:
	case 9:
	case 10:
		printf("Autumn");
		break;
	case 11:
	case 12:
	case 1:
		printf("Winter");
		break;
	default:
		printf("Error");
	}
	return 0;
}

4. 整数判断

⭐ 统一输出变量 c

#include

int main(){
	int n;
	scanf("%d", &n);

	char c;
	if (n % 5 == 0 && n % 7 == 0)
		c = 'A';
	else if (n % 5 == 0)
		c = 'B';
	else if (n % 7 == 0)
		c = 'C';
	else
		c = 'D';

	printf("%c",c);
	return 0;
}

5. 判断闰年

⭐ 闰年( 模 4 == 0 并且 模 100 != 0 )或者(模 400 == 0)
⭐ C语言 非0为真,布尔变量转int,真为1,假为0

#include

int main(){
	int year,leap;//leap为0,不是闰年;leap为1,是闰年
	scanf("%d", &year);
	leap = year % 4 == 0 && year % 100 || year % 400 == 0;

	if (leap)printf("yes");
	else printf("no");
	
	return 0;
}

6. 判断日期是否合法

⭐ 特判平闰年的二月就好

#include

int judgeyear(int year){
	int flag = 0;//平年返回0
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		flag = 1;//闰年返回1
	return flag;
}

int main(){

	int year, month, day;
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	int flag_1 = 0;

	scanf("%d%d%d", &year, &month, &day);

	//判断输入的合法性
	if (day > 0 && month != 2 && day <= days[month] && month > 0 && month <= 12)
		flag_1 = 1;
	if (month == 2)
	{
		if (day > 0 && day <= days[month] + judgeyear(year))
			flag_1 = 1;
	}

	if (flag_1)printf("yes");

	else  printf("no");

	return 0;
}

7. 同上

8. 某年某日(倒序)

已更正一月 error (2023-03-23 10:15:16)
⭐ 提示:3 月 3 日 并不用加上 3 月份的 天数,只需算 1 月+2 月的天数 + 3 (day)即可

#include

int judgeyear(int year){
	int flag = 0;//平年返回0
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		flag = 1;//闰年返回1
	return flag;
}

int main(){

	int year, month, day;

	int dayNumber = 0;

	scanf("%d%d%d", &year, &month, &day);
	month -= 1;//月份减一,天数==当前月前面所有月的天数和 + 日数

	switch (month){

	case 12:dayNumber += 31;

	case 11:dayNumber += 30;

	case 10:dayNumber += 31;

	case 9:dayNumber += 30;

	case 8:dayNumber += 31;

	case 7:dayNumber += 31;

	case 6:dayNumber += 30;

	case 5:dayNumber += 31;

	case 4:dayNumber += 30;

	case 3:dayNumber += 31;//3月的情况,请填写多行代码

	case 2:dayNumber += (28 + judgeyear(year));//2月的情况,请填写一行代码

	case 1:dayNumber += 31;//1月的情况,请填写多行代码
	
	case 0:dayNumber += day;
		break;
	default:printf("Input error!");

	}
	printf("%d", dayNumber);

	return 0;
}

9. 某年某日(顺序)

⭐ break;

#include

int judgeyear(int year){
	int flag = 0;//平年返回0
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		flag = 1;//闰年返回1
	return flag;
}

int main(){

	int year, month, day;

	int dayNumber = 365;

	scanf("%d%d%d", &year, &month, &day);

	if(judgeyear(year))
	{
		dayNumber = 366;
	}

	switch (month){

	case 1:dayNumber -= 31;

	case 2:dayNumber -= 28 + judgeyear(year);

	case 3:dayNumber -= 31;

	case 4:dayNumber -= 30;

	case 5:dayNumber -= 31;

	case 6:dayNumber -= 30;

	case 7:dayNumber -= 31;

	case 8:dayNumber -= 31;

	case 9:dayNumber -= 30;

	case 10:dayNumber -= 31;

	case 11:dayNumber -= 30;

	case 12:dayNumber -= 31;
		break;

	default:
		printf("Input error!");

	}

	printf("%d", dayNumber + day);

	return 0;
}

10. 某年某日

⭐ 递推(数据范围小)

#include

int main(void)
{
	int year, month, day, Feb, sum;

	scanf("%d%d%d", &year, &month, &day);

	if ((day>31) || (day<1)){

		printf("input error!");

		return;

	}

	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

	{
		Feb = 29;
	}

	else        { Feb = 28; }


	//printf("%d\n", Feb);

	switch (month)

	{
	case 1: sum = 31;break;
	case 2: 
		if (day > Feb){
					printf("input error\n ");
					return 0;
		}
		else
			sum = 31 + Feb; break;
	case 3:
		sum = 31 + Feb + day; break;

	case 4: sum = 62 + Feb + day; break;

	case 5: sum = 92 + Feb + day; break;

	case 6: sum = 123 + Feb + day; break;

	case 7: sum = 153 + Feb + day; break;

	case 8: sum = 184 + Feb + day; break;

	case 9: sum = 215 + Feb + day; break;

	case 10: sum = 245 + Feb + day; break;

	case 11: sum = 276 + Feb + day; break;

	case 12: sum = 306 + Feb + day; break;

	defult:
		printf("input error\n ");
	}
	printf("%d", sum);

	return 0;

}

11. 数组实现某年某日

⭐ for 循环 变量 i 枚举的是月份

#include

int main(){

	int month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	int year, month, day;

	int i;

	scanf("%d%d%d", &year, &month, &day);

	if (month<0 || month>12)

		printf("date error");

	else{

		for (i = 1; i<month; i++){

			day = day + month_day[i];

			if (i == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
				day++;
		}
	}
	printf("%d", day);

	return 0;
}

12. 某年某日求星期数

#include

int main(){

	int year, month, day, week,i;
	int month_day[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	scanf("%d%d%d", &year, &month, &day);

	scanf("%d", &week);//输入当年元旦是星期几

	if (month<0 || month>12)

		printf("date error");

	else{

		for (i = 1; i<month; i++){

		

				//填写多行代码,用switch结构,

				//或者else – if结构,或者两者结合,确定每月的天数

			day = day + month_day[i];

			if (i == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
				day++;

		}



	}

	printf("%d", (day + week - 1) % 7);

}

扩展

⭐ 目标:如何上学校作业平台做实验(非 GDPU wifi)

① 安装 Easy Connect 客户端(此软件必须在运行状态才可以正常连接

官网下载传送门

奶牛下载传送门

② 广药官网右上导航栏入口

③ 登录(完)

你可能感兴趣的:(大学水课,c语言,算法,开发语言)