1.定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
int main()
{
struct Date
{
int year;
int month;
int day;
};
printf("输入日期(xxxx-xx-xx):");
struct Date date;
scanf("%d-%d-%d",&date.year,&date.month,&date.day);
int Days[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i,days=0;
for(i=1;i<date.month;i++)
{
days += Days[i];
}
days += date.day;
if(date.month >2)
{
if(date.year%400 == 0 || (date.year%4 == 0 && date.year%100 != 0) )
{
++days;
}
}
printf("这是今年第%d天",days);
return 0;
}
2.写一个函数days,实现第1 题的计算。由主函数将年、月、日传递给days函数,计算后将日子数传回主函数输出。
struct Date
{
int year;
int month;
int day;
};
int days(struct Date date)
{
int Days[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i,num=0;
for(i=1;i<date.month;i++)
{
num+= Days[i];
}
num += date.day;
if(date.month >2)
{
if(date.year%400 == 0 || (date.year%4 == 0 && date.year%100 != 0) )
{
++num;
}
}
return num;
}
int main()
{
printf("输入日期(xxxx-xx-xx):");
struct Date date;
scanf("%d-%d-%d",&date.year,&date.month,&date.day);
printf("这是今年第%d天",days(date));
return 0;
}
6、13个人围成一圈,从第1个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。要求用链表实现。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 13
typedef struct People
{
int num;
struct people *next;
}people;
int main()
{
int count=N;
people p[N];
people *head;
head = p;
for(int i=0;i<N;i++)
{
head->num =i+1;
head->next = &p[i+1];
head = head->next;
}
p[N-1].next = p;
head =p;
int i=1;
while(count>1)
{
if(head->num == 0)
{
head = head->next;
continue;
}
if(i==3)
{
head->num = 0;
count--;
}
head = head->next;
i++;
if(i>3)
{
i=1;
}
}
while(head->num ==0)
{
head = head->next;
if(head->num != 0)
{
printf("留下的是%d号",head->num);
}
}
return 0;
}
7.在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的节点
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int num;
struct LNode *next;
}node;
node* creat(int n)
{
node *head,*p;
head = (node *)malloc(N);
p = head;
head->num = 0;
head->next = NULL;
for(int i=1;i<=n;i++)
{
node *newNode = (node *)malloc(N);
newNode->num =i;
newNode->next = NULL;
p->next =newNode;
p = p->next;
}
return head;
}
void print(node *head)
{
node *p = head;
if(head!=NULL)
do
{
printf("%d ",p->num);
p = p->next;
}while(p!=NULL);
}
node* del(node *head,int target)
{
node *pre= head;
node *p = head->next;
while (p!=NULL)
{
if(p->num==target)
{
pre->next = p->next;
free(p);
break;
}
else
{
pre = p;
p = p->next;
}
}
return head;
}
int main()
{
node *head = creat(10);
head = del(head,3);
print(head);
return 0;
}
10.已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并, 按学号升序排列。
typedef struct Student
{
int num;
float score;
struct Student *next;
} student;
student* creat(void)
{
student *head,*p1,*p2;
int n=0;
p1=p2=(student *)malloc(N);
scanf("%d %f",&p1->num,&p1->score);
head = NULL;
while (p1->num!=0)
{
n = n+1;
if(n==1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (student *)malloc(N);
scanf("%d %f",&p1->num,&p1->score);
}
p2->next =NULL;
return head;
}
void print(student *head)
{
student *p = head;
if(head!=NULL)
do
{
printf("%d %5.1f\n",p->num,p->score);
p = p->next;
}
while(p!=NULL);
}
student *merge(student *head1,student *head2)
{
student *pre,*cur;
pre = head1;
cur = head1->next;
while(pre->next!=NULL)
{
pre = pre->next;
}
pre->next = head2;
pre = head1;
while(pre->next !=NULL)
{
cur = head1->next;
while(cur !=NULL)
{
if(cur->num < pre->num)
{
int num = cur->num;
float score = cur->score;
cur->num = pre->num;
cur->score = cur ->score;
pre->num = num;
pre->score = score;
}
cur = cur->next;
}
pre = pre->next;
}
return head1;
}
int main()
{
printf("组1:\n");
student *head1 = creat();
printf("组2:\n");
student *head2 =creat();
printf("排序后:\n");
print(merge(head1,head2));
return 0;
}