#include <stdio.h>
main()
{ int days(int,int,int);//计算天数并返回
int y,m,d,j,z,t,z1;//y年m月d日
scanf("%d %d %d",&y,&m,&d);
j=days(y,m,d);//j为总天数,起始日期2012.4.9
if(j%7==0)
z1=j/7;
else
z1=j/7+1;//第z1周
z=z1%52;//折合成第z周,整除时0代表第52周
t=j%7;//星期t
printf("总天数%d\n第%d周\n折合成第%d周\n星期%d\n",j,z1,z,t);
if(z>=1&&z<=13)//1~13周
switch(t)
{ case 1:printf("3 and 8.\n");break;
case 2:printf("4 and 9.\n");break;
case 3:printf("5 and 0.\n");break;
case 4:printf("1 and 6.\n");break;
case 5:printf("2 and 7.\n");break;
case 6:;
case 0:printf("Free.\n");break;
}
if(z>=14&&z<=26)//14~26周
switch(t)
{ case 1:printf("2 and 7.\n");break;
case 2:printf("3 and 8.\n");break;
case 3:printf("4 and 9.\n");break;
case 4:printf("5 and 0.\n");break;
case 5:printf("1 and 6.\n");break;
case 6:;
case 0:printf("Free.\n");break;
}
if(z>=27&&z<=39)//27~39周
switch(t)
{ case 1:printf("1 and 6.\n");break;
case 2:printf("2 and 7.\n");break;
case 3:printf("3 and 8.\n");break;
case 4:printf("4 and 9.\n");break;
case 5:printf("5 and 0.\n");break;
case 6:;
case 0:printf("Free.\n");break;
}
if(z>=40&&z<=51||z==0)//40~52周
switch(t)
{ case 1:printf("5 and 0.\n");break;
case 2:printf("1 and 6.\n");break;
case 3:printf("2 and 7.\n");break;
case 4:printf("3 and 8.\n");break;
case 5:printf("4 and 9.\n");break;
case 6:;
case 0:printf("Free.\n");break;
}
}
int days(int a,int b,int c)//这是一个给定日期求到2012.4.9之间总天数的函数
{
int i,x,y,m,j; /*a年b月c日*/
for(i=a-1,x=0;i>=2012;i--) /*i用来计年,x用来计算闰年数*/
if(i%4==0&&i%100!=0||i%400==0)//从1900.1.1开始
x++; /*若i为闰年,x加1*/
for(i=b-1,y=0;i>=1;i--) /*i用来计月,y用来计算小月数*/
if(i==4||i==6||i==9||i==11)
y++; /*i为小月,y加1*/
if(b>2)
{
if(a%4==0&&a%100!=0||a%400==0)
j=366*x+365*(a-2012-x)+30*y+31*(b-y-2)+c+29;
else
j=366*x+365*(a-2012-x)+30*y+31*(b-y-2)+c+28;
}
else
j=366*x+365*(a-2012-x)+31*(b-1)+c;
j=j-99;//j为总天数(在未减去99时假设是从2012.1.1开始)
return j;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:相信考察的是程序员的逻辑而不是数学。
// 输入三个字符串,按由小到大的顺序输出,利用指针实现。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define array_size 10
int main(int argc, char const *argv[])
{
char char_array_first[array_size], char_array_second[array_size], char_array_third[array_size];
char temp_swap[array_size];
char *char_pointer_first, *char_pointer_second, *char_pointer_third;
puts("please type three strings to order!");
gets(char_array_first);
gets(char_array_second);
gets(char_array_third);
printf("before order: %s\t%s\t%s\n",char_array_first,char_array_second,char_array_third);
char_pointer_first = char_array_first;
char_pointer_second = char_array_second;
char_pointer_third = char_array_third;
if (strcmp(char_array_first, char_array_second) > 0) // make sure the fist < second
{
strcpy(temp_swap, char_array_first); // first arg is Destination,second arg is source
strcpy(char_array_first, char_array_second);
strcpy(char_array_second, temp_swap);
}
if (strcmp(char_array_second, char_array_third) > 0) // make sure the second < third
{
strcpy(temp_swap, char_array_second);
strcpy(char_array_second, char_array_third);
strcpy(char_array_third, temp_swap);
}
if (strcmp(char_array_first, char_array_second) > 0) // make sure the fist < second
{
strcpy(temp_swap, char_array_first); // first arg is Destination,second arg is source
strcpy(char_array_first, char_array_second);
strcpy(char_array_second, temp_swap);
}
printf("after order: %s\t%s\t%s\n",char_array_first,char_array_second,char_array_third);
return 0;
}
result:
please type three strings to order!
ccc
abf
acb
before order: ccc abf acb
after order: abf acb ccc
3.21
指针,只使用strcmp
//输入三个字符串,按由小到大的 顺序输出,用指针来实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 5
int main()
{
char str_1[SIZE];
char str_2[SIZE];
char str_3[SIZE];
char *p1, *p2, *p3;
scanf("%s%s%s", str_1, str_2, str_3);
p1 = str_1;
p2 = str_2;
p3 = str_3;
if (strcmp(p1, p2))
{
char *tmp = p1;
p1 = p2;
p2 = tmp;
}
if (strcmp(p2, p3))
{
char *tmp = p2;
p2 = p3;
p3 = tmp;
}
if (strcmp(p1, p2))
{
char *tmp = p1;
p1 = p2;
p2 = tmp;
}
// printf("%s,%s,%s\n", str_1, str_2, str_3);
printf("%s,%s,%s", p1, p2, p3);
return 0;
}
result:
zzz bbb aaa
aaa,bbb,zzz
首先需要检查时间数据输入和存储的方式是否正确,如果时间数据存储的方式不正确,会导致输出结果不正确。
其次,需要检查日期处理的算法是否正确,比如计算一年的第几周可以使用ISO 8601规定的标准,以周四作为一周的起始日,然后第一周包含当年1月1日的那个周四。如果使用其他方式计算周数,可能会出现问题。
此外,还需要考虑闰年的影响,比如在计算一年的第几周时,闰年和平年的算法不同。如果没有考虑到闰年的影响,也会导致计算结果不正确。
在编写代码时,可以使用现成的日期处理库来避免出现问题。比如在Python中,可以使用datetime模块来处理日期和时间数据,而在Java中,可以使用Joda-Time库或者Java 8中的时间API来处理日期和时间数据。使用这些库可以避免出现常见的日期处理问题,并且提高代码的可读性和可维护性。