你哪来这么多事(五):职工信息插入
职工信息插入
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld
Description
某单位有n个职工(不超过100),每个职工的信息包括工号(long no),姓名(char name[9])和出生年月日(int year,birth,day)。
编写程序,先将n个职工的信息录入到结构体数组中(n个职工的数据已按出生年月日升序有序,即出生早的在前),
然后需要将另外m个职工的信息插入到该结构体数组中,使得插入后该数组仍然按出生日期有序。
最后输出所有学生的信息。
注意:main函数已经给定(如下所示)。
请将程序补充完整。
提交时只需要提交自己补充的代码部分,不需要提交给定的main函数的代码部分。
#include<stdio.h>
int main()
{int i,n,m;struct employee emp[100];while(scanf("%d",&n)!=EOF){input(emp,n); //读入n个职工的数据scanf("%d",&m);for(i=0;i<m;i++){insert(emp,n); //录入一个职工的信息,然后将它插入到结构体数组中,仍然按出生日期有序n++;}print(emp,n); //输出n个职工的信息}return 0;
}
Input
包含多组测试数据,每组测试数据第一行包含1个正整数n,表示已有职工人数。
接下来的n行,每行为1个职工的详细信息(已按出生日期有序),包括工号,姓名和出生年月日,各数据之间用空格隔开。
接下来的一行包括一个正整数m,表示待插入的职工人数。
最后是m行,每行为1个待插入职工的信息,包括工号,姓名和出生年月日,各数据之间用空格隔开。
其中,n+m的值不超过100。
Output
每组测试数据输出占n+m行,每行输出一个学生的信息。每一行的输出数据依次为:工号,姓名和出生年日期,。各部分数据之间用空格隔开。(具体详见样例输出)
Sample Input
3 1001 aaa 1962 5 9 1002 bbb 1978 10 28 1003 ccc 1989 6 7 2 1004 ddd 1970 3 21 1005 eee 1978 10 29
Sample Output
1001 aaa 1962-5-9 1004 ddd 1970-3-21 1002 bbb 1978-10-28 1005 eee 1978-10-29 1003 ccc 1989-6-7
题目分析:
跟学生排分数有什么区别吗,
嘤嘤嘤……
#include<stdio.h>
struct employee
{long no;char name[9];int birth[4];
};
void input(struct employee emp[100],int n)
{int i;for(i=1;i<=n;i++){scanf("%ld %s %d %d %d",&emp[i].no,emp[i].name,&emp[i].birth[1],&emp
[i].birth[2],&emp[i].birth[3]);}return;
}
void sort(struct employee emp[100],int n)
{int i,j;struct employee t;for(i=1;i<n;i++)for(j=i+1;j<=n;j++){if(emp[i].birth[1]>emp[j].birth[1] || emp[i].birth[1]==emp
[j].birth[1] && emp[i].birth[2]>emp[j].birth[2] || emp[i].birth[1]==emp[j].birth[1] &&emp[i].birth[2]==emp[j].birth[2] && emp[i].birth[3]>emp[j].birth[3]){t=emp[i];emp[i]=emp[j];emp[j]=t;}}return;
}
void insert(struct employee emp[100],int n)
{int i,j,k,f=0;struct employee t;scanf("%ld %s %d %d %d",&t.no,t.name,&t.birth[1],&t.birth[2],&t.birth[3]);for(i=1;i<=n;i++){if(emp[i].birth[1]>t.birth[1] || emp[i].birth[1]==t.birth[1] && emp
[i].birth[2]>t.birth[2] || emp[i].birth[1]==t.birth[1] && emp[i].birth[2]==t.birth[2] &&emp[i].birth[3]>t.birth[3]){f=1;for(j=n;j>=i;j--){emp[j+1]=emp[j];}emp[i]=t;return;}}if(f==0)emp[n+1]=t;return;
}
void print(struct employee emp[100],int n)
{int i;for(i=1;i<=n;i++){printf("%ld %s %d-%d-%d\n",emp[i].no,emp[i].name,emp[i].birth[1],emp
[i].birth[2],emp[i].birth[3]);}return;
}
int main()
{int i,n,m;struct employee emp[100];while(scanf("%d",&n)!=EOF){input(emp,n); //读入n个职工的数据scanf("%d",&m);for(i=0;i<m;i++){insert(emp,n); //录入一个职工的信息,然后将它插入到结构体数组中,仍然按出生日期
有序n++;}print(emp,n); //输出n个职工的信息}return 0;
}
总结
以上是生活随笔为你收集整理的你哪来这么多事(五):职工信息插入的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 神话故事作者是谁啊?
- 下一篇: 你哪来这么多事(六):职工信息查找