編程題(15_01.c)
結(jié)構(gòu)
struct student
{
long num
char name[20]
int score
struct student *next
}
鏈表練習(xí):
(1).編寫函數(shù)struct student * creat(int n),創(chuàng)建一個(gè)按學(xué)號(hào)升序排列的新鏈表,每個(gè)鏈表中的結(jié)點(diǎn)中
的學(xué)號(hào)、成績(jī)由鍵盤輸入,一共n個(gè)節(jié)點(diǎn)。
(2).編寫函數(shù)void print(struct student *head),輸出鏈表,格式每行一個(gè)結(jié)點(diǎn),包括學(xué)號(hào),姓名,分?jǐn)?shù)。
(3).編寫函數(shù)struct student * merge(struct student *a,struct student *b), 將已知的a,b兩個(gè)鏈表
按學(xué)號(hào)升序合并,若學(xué)號(hào)相同則保留成績(jī)高的結(jié)點(diǎn)。
(4).編寫函數(shù)struct student * del(struct student *a,struct student *b),從a鏈表中刪除b鏈表中有
相同學(xué)號(hào)的那些結(jié)點(diǎn)。
(5).編寫main函數(shù),調(diào)用函數(shù)creat建立2個(gè)鏈表a,b,用print輸出倆個(gè)鏈表;調(diào)用函數(shù)merge升序合并2個(gè)
鏈表,并輸出結(jié)果;調(diào)用函數(shù)del實(shí)現(xiàn)a-b,并輸出結(jié)果。
a:
20304,xxxx,75,
20311,yyyy,89
20303,zzzz,62
20307,aaaa,87
20320,bbbb,79
b:
20302,dddd,65
20301,cccc,99
20311,yyyy,87
20323,kkkk,88
20307,aaaa,92
20322,pppp,83
標(biāo)簽:
student
struct
score
long
上傳時(shí)間:
2016-04-13
上傳用戶:zxc23456789
1.Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and
another integer x, determines whether or not there exist two elements in S whose sum is exactly x. (Implement exercise 2.3-7.)
#include<stdio.h>
#include<stdlib.h>
void merge(int arr[],int low,int mid,int high){
int i,k;
int *tmp=(int*)malloc((high-low+1)*sizeof(int));
int left_low=low;
int left_high=mid;
int right_low=mid+1;
int right_high=high;
for(k=0;left_low<=left_high&&right_low<=right_high;k++)
{
if(arr[left_low]<=arr[right_low]){
tmp[k]=arr[left_low++];
}
else{
tmp[k]=arr[right_low++];
}
}
if(left_low<=left_high){
for(i=left_low;i<=left_high;i++){
tmp[k++]=arr[i];
}
}
if(right_low<=right_high){
for(i=right_low;i<=right_high;i++)
tmp[k++]=arr[i];
}
for(i=0;i<high-low+1;i++)
arr[low+i]=tmp[i];
}
void merge_sort(int a[],int p,int r){
int q;
if(p<r){
q=(p+r)/2;
merge_sort(a,p,q);
merge_sort(a,q+1,r);
merge(a,p,q,r);
}
}
int main(){
int a[8]={3,5,8,6,4,1,1};
int i,j;
int x=10;
merge_sort(a,0,6);
printf("after Merging-Sort:\n");
for(i=0;i<7;i++){
printf("%d",a[i]);
}
printf("\n");
i=0;j=6;
do{
if(a[i]+a[j]==x){
printf("exist");
break;
}
if(a[i]+a[j]>x)
j--;
if(a[i]+a[j]<x)
i++;
}while(i<=j);
if(i>j)
printf("not exist");
system("pause");
return 0;
}
標(biāo)簽:
c語(yǔ)言
算法
排序
上傳時(shí)間:
2017-04-01
上傳用戶:糖兒水嘻嘻