1.编写函数,将一个十进制整数转换为二进制输出。
#include <stdio.h>
void change(int n){
if(n>1)
change(n/2);
printf("%d",n%2);
}
int main(){
change(4);
}
2.编写函数 int mergearr (int a[], int m, int b[], int n),将两个严格增序数组a和b合并后存储在a中,并保证处理后的数组仍然严格增序,函数值返回合并后数组a中的元素个数,不能定义额外的新数组。
#include <stdio.h>
int mergerr(int a[],int m,int b[],int n) {
for(int i=m; i<n+m; i++)
a[i]=b[i-m];
sort(a,n+m);
int count = result(a,n+m);
printf("\n%d",m+n-count);
}
void sort(int *a,int n) {
for(int i=0; i<n-1; i++)
for(int j=0; j<n-1-i; j++)
if(a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
int result(int *a,int n) {
int count=0;
for(int i=0; i<n-1-count; i++) {
if(a[i]==a[i+1]) {
count++;
for(int j=i; j<n-count; j++)
a[j]=a[j+1];
}
}
return count;
}
int main() {
int a[]= {1,2,3,4,6};
int b[]= {2,3,4,5,6,7,8};
mergerr(a,5,b,7);
}
3.给定一个凸n边形(n>=3),可以用n-3条不相同的对角线把它剖分成n-2个三角形。编写递归函数,计算凸n边形的三角形剖分数(不同剖分方法的个数)。提示,递归公式如下:
#include <stdio.h>
int func(int n) {
if(n==2)
return 1;
int sum=0;
for(int i=2; i<n; i++)
sum+=func(i)*func(n+1-i);
}
4.职工的信息卡至少包括工号、姓名出生年月等信息。限定:工号为整形且不超过整形的取值范围。
1)定义存储职工信息的单向链表的节点类型;
2)编写函数,由文件in.txt中依次读入k个职工的完整信息,创建一个用于管理职工信息的单向链表;
3)假定管理职工信息的单向链表已经按照工号从小到大排序。编写函数,由键盘键入1个职工的信息,按工号顺序插入到用于管理职工信息的单向链表中。
#include <stdio.h>
#include <stdlib.h>
typedef struct Date {
int y,m;
} Date;
typedef struct node {
int num;
char name[20];
struct Date birthdate;
struct node *next;
} node;
struct node *create(int n) {
FILE *file;
if((file=fopen("in.txt","r"))==NULL) {
printf("open error");
exit(0);
}
struct node *head=NULL,*p;
for(int i=0; i<n; i++) {
p=(struct node*)malloc(sizeof(struct node));
fscanf(file,"%d%10s%d%d",&(p->num),&(p->name),&(p->birthdate.y),&(p->birthdate.m));
p->next=head;
head=p;
}
fclose(file);
return head;
}
struct node *search(struct node *head) {
struct node *temp=(struct node *)malloc(sizeof(struct node));
scanf("%d%10s%d%d",&(temp->num),&(temp->name),&(temp->birthdate.y),&(temp->birthdate.m));
if(temp->num<head->num) {
temp->next=head;
return temp;
}
struct node *p=head->next,*q=head;
while(p!=NULL&&temp->num>p->num) {
q=p;
p=p->next;
}
q->next=temp;
temp->next=p;
return head;
}