1.题目给定int arrayun(int a[],int alen,int b[],int blen,int c[]),将无序数组a和b中的元素按照递减顺序存放到c中,同一个元素只能出现一次,最后返回c中的有效元素个数
#include <stdio.h>
int array(int a[],int alen,int b[],int blen,int c[]) {
for(int i=0; i<alen; i++) {
c[i]=a[i];
}
for(int j=0; j<blen; j++) {
c[alen+j]=b[j];
}
sort(c,alen+blen);
int n = deletemany(c,alen+blen);
for(int i=0; i<n; i++)
printf("%d",c[i]);
}
void sort(int *arr,int n) {
for(int i=0; i<n-1; i++)
for(int j=0; j<n-1-i; j++)
if(arr[j]<arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
int deletemany(int *arr,int n) {
for(int i=0; i<n-1; i++)
if(arr[i]==arr[i+1]) {
for(int j=i; j<n-1; j++)
arr[j]=arr[j+1];
n--;
i--;
}
return n;
}
int main() {
int a[]= {1,2,3,3,3,4,5,6};
int b[]= {1,2,3,3,3,4,5,6};
int c[16];
array(a,8,b,8,c);
}
2.一根绳子长n,在他的5/13处截断,如果n*5/13大于m,(n,m都为整数),则递归调用floor(n*5/13)和floor(n-n*5/13),利用递归思想求长度为n的绳子,分为几段才能使每段的长度小于m
#include <stdio.h>
int floor(int n, int m) {
if (n < m) {
return 1;
} else if (n * 5 / 13 > m) {
return 1 + floor(n * 5 / 13, m) + floor(n - n * 5 / 13, m);
} else {
return 1 + floor(n - n * 5 / 13, m);
}
}
int main() {
int n = 12;
int m = 5;
int count = floor(n, m);
printf("%d",count);
return 0;
}
3.输入任意正整数,以'0'为结尾,请设计一个单链表结构,将输入的任意整数插入单链表要求
1.依次插入,但要保持链表由大到小排序
2.返回链表的头结点
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node *next;
} node;
struct node * createlist() {
int n;
struct node *head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
scanf("%d",&n);
while(n!=0) {
struct node *p=(struct node *)malloc(sizeof(struct node));
p->key=n;
if(head->next==NULL) {
p->next=head->next;
head->next=p;
} else {
struct node *pre=head;
while(pre->next!=NULL&&pre->next->key>p->key)
pre=pre->next;
p->next=pre->next;
pre->next=p;
}
scanf("%d", &n);
}
return head->next;
}
void printLinkedList(struct node* head) {
struct node* curr = head;
while (curr != NULL) {
printf("%d ", curr->key);
curr = curr->next;
}
printf("\n");
}
int main() {
node* list = createlist();
printLinkedList(list);
return 0;
}