1  实验题目

3.实现方法

#### 7-7 计算火车运行时间

#include <stdio.h>
int main (void)
{
int x,y,a,b;
scanf ("%d%d",&x,&y);
a=y/100-x/100;
b=y%100-x%100;
if(b<0)
{
b+=60;
a-=1;
}
printf ("%02d:%02d",a,b);
return 0;

}

#### 4-5 求简单交错序列前N项和

#include <stdio.h>
int main (void)
{
int i,N,flag,denominator;
double sum,item;
scanf ("%d",&N);
sum=0;
flag=1;
denominator=1;
for (i=1;i<=N*3;i+=3)
{
item=flag*1.0/denominator;
sum=sum+item;
flag=-flag;
denominator=denominator+3;

}
printf ("sum = %.3lf\n",sum);
return 0;

}

#### 4-2-8 计算天数

#include <stdio.h>
int main(void) {
int year, month, day;
int days;
int i;
scanf("%d/%d/%d", &year, &month, &day);
days = day;
for(i = 1; i < month; ++i) {
switch(i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
days += 29;
else
days += 28;
}
}
printf("%d\n", days);
return 0;
}

posted on 2017-10-29 18:19  牵丝戏  阅读(750)  评论(0编辑  收藏  举报