//题目4:输入某年某月某日,判断这一天是这一年的第几天? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //分析:某年决定是平年还是闰年,某月决定本月有多少天,最后结果是前几个月的时间+本月已过的天数 //闰年的判断 (year%4==0&&year%100!=0)||(year%400==0) int protect(int year, int month){ //year==1表示闰年,year=0表示平年 int res = 0; int tuemonth = 28; if (year) { tuemonth = 29; } switch (month - 1) { case 0: res = 0; break; case 1: res = 31; break; case 2: res = 31 + tuemonth; break; case 3: res = 31 + tuemonth + 31; break; case 4: res = 31 + tuemonth + 31 + 30; break; case 5: res = 31 + tuemonth + 31 + 30 + 31; break; case 6: res = 31 + tuemonth + 31 + 30 + 31 + 30; break; case 7: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31; break; case 8: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31 + 31; break; case 9: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 10: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 11: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; default: res = 31 + tuemonth + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31; break; } return res; } void main(){ int year = 0; int month = 0; int day = 0; int res = 0; printf("请输入年月日! "); scanf("%d,%d,%d", &year,&month,&day); // //判断年 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { //判断该年是闰年,二月份有29天 res = protect(1, month) + day; } else { res = protect(0, month) + day; } printf("这一天是这一年的第%d天", res); system("pause"); }
//题目5:输入三个整数x,y,z,请把这三个数由小到大输出。 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void sortnum(int *x,int *y,int *z){ int temp = 0; if (*x>*y) { temp = *x; *x = *y; *y = temp; } if (*y>*z) { temp = *z; *z = *y; *y = temp; } //至此*z的数值最大 if (*x>*y) { temp = *x; *x = *y; *y = temp; } //至此*x的数值最小 } void main(){ int x, y, z; int *px, *py, *pz; px = &x; py = &y; pz = &z; scanf("%d%d%d", px, py, pz); printf(" 你输入的数据是X=%d;Y=%d;Z=%d", *px, *py, *pz); sortnum(px, py, pz); printf(" 你排序之后的数据是X=%d;Y=%d;Z=%d", *px, *py, *pz); system("pause"); }
//题目6:用*号输出圆形图案。 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<math.h> //分析;(x1-x2)^2+(y1-y2)^2=R^2;其中r是园的半径,(x1-x2)^2+(y1-y2)^2是获取一个点与园心坐标的距离 //本题假设r=10,圆心坐标就是(10,0) //pow() 函数用来求 x 的 y 次幂(次方),其原型为:double pow(double x, double y); //本圆不圆,是因为屏幕的行间距和列间距不相等 void main(){ //上方y轴 for (int i = 10; i >-11; i--) { //左侧x轴 for (int j = 0; j < 11; j++) { if ((pow((j - 10),2.0)+pow(i,2.0))==pow(10.0,2.0)) { printf("*"); } else{ printf(" "); } } //右侧x轴 for (int j = 11; j < 21; j++) { if ((pow((j - 10), 2.0) + pow(i, 2.0)) == pow(10.0, 2.0)) { printf("*"); } else{ printf(" "); } } printf(" "); } system("pause"); }