1、北航 计算机软件基础 作业讲解1、编写一程序,输入全班40名同学的姓名(字符串)、学号、语文成绩、数学成绩、外语成绩;保存在结构体数组中。然后对此结构体数组排序,使得排序后的结构体数组中的元素按学生平均成绩非递增有序。#include#include#define NUM 40/定义NUM代表全班人数struct STUDENT char name20; int number; float chinese,math,english; float average;studentNUM;/定义结构体数组typedef struct STUDENT S;/声明一个结构体变量void sort(S s
2、NUM)/冒泡法进行排序 int i,j; char temp120; int temp2;float temp3,temp4,temp5,temp6; for(i=0;iNUM;i+) for(j=0;jNUM-i;j+) if(sj.averagesj+1.average) strcpy(temp1,sj.name); strcpy(sj.name,sj+1.name); strcpy(sj+1.name,temp1);temp2=sj.number;sj.number=sj+1.number;sj+1.number=temp2; temp3=sj.chinese;sj.chinese=s
3、j+1.chinese;sj+1.chinese=temp3; temp4=sj.math;sj.math=sj+1.math;sj+1.math=temp4; temp5=sj.english;sj.english=sj+1.english;sj+1.english=temp5; temp6=sj.average;sj.average=sj+1.average;sj+1.average=temp6; void main() int i; for(i=0;iNUM;i+) printf(请的输入第%d个学生学生的信息:n,i+1); printf(name: ); scanf(%s,&stud
4、enti.name); printf(number: ); scanf(%d,&studenti.number); printf(chinese: ); scanf(%f,&studenti.chinese); printf(math: ); scanf(%f,&studenti.math); printf(english: ); scanf(%f,&studenti.english); studenti.average=(studenti.chinese+studenti.math+studenti.english)/3; printf( 姓名学号语文数学英语平均n); for(i=0;iN
5、UM;i+) printf( %s %d %5.2f %5.2f %5.2f %5.2fn,studenti.name,studenti.number,studenti.chinese,studenti.math,studenti.english,studenti.average); sort(student);printf(排序结果为:n); printf( 姓名学号语文数学英语平均n); for(i=0;iNUM;i+) printf( %s %d %5.2f %5.2f %5.2f %5.2fn,studenti.name,studenti.number,studenti.chinese
6、,studenti.math,studenti.english,studenti.average); 图 12、编写一程序,输入全班N名同学的姓名、学号、语文成绩、数学成绩、外语成绩。然后按照学生平均成绩非递增排序。程序需能方便地进行插入、删除等操作。#include#include#includetypedef struct node / 定义一个结构体并声明结构体类型 char name20; int number; float chinese,math,english; float average; struct node* Link;Node;typedef Node* List; /
7、 声明结构类类型的指针Node*NewNode() / 生成新节点 Node*p=(Node*)malloc(sizeof(Node); p-Link=NULL; return p;List BuildList(int n) / 建立空链表 Node*p,*r=NULL,*first=NULL; int i; for(i=0;iLink=p; else first=p; r=p; return first;List Sort(List SL) / 冒泡法进行排序 List p,q; char temp120; int temp2; float temp3, temp4, temp5,temp6
8、; for(p=SL;p!=NULL;p=p-Link) for(q=p-Link;q!=NULL;q=q-Link) if(p-averageaverage) strcpy(temp1,q-name); strcpy(q-name,p-name); strcpy(p-name,temp1);temp2=q-number; q-number=p-number; p-number=temp2; temp3=q-chinese; q-chinese=p-chinese; p-chinese=temp3; temp4=q-math; q-math=p-math; p-math=temp4; temp
9、5=q-english; q-english=p-english; p-english=temp5; temp6=q-average; q-average=p-average; p-average=temp6; return SL;void main() Node*LS,*p; int n; printf(the number of student is n); scanf(%d,&n); LS=BuildList(n); p=LS; for(;p;p=p-Link) printf(the name is ); scanf(%s,&p-name); printf(the number is )
10、; scanf(%d,&p-number); printf(the chinese is ); scanf(%f,&p-chinese); printf(the math is ); scanf(%f,&p-math); printf(the english is ); scanf(%f,&p-english); p-average=(p-chinese+p-math+p-english)/3; for(p=LS;p;p=p-Link) printf(name number chinese math english averagen); printf( %s %5d %5.2f %5.2f %
11、5.2f %5.2f n,p-name,p-number,p-chinese,p-math,p-english,p-average); LS=Sort(LS); printf(排序后的结果为:n); for(p=LS;p;p=p-Link) printf(name number chinese math english averagen); printf( %s %5d %5.2f %5.2f %5.2f %5.2f n,p-name,p-number,p-chinese,p-math,p-english,p-average); 图 23、实现约瑟夫环#include stdio.h#incl
12、ude stdlib.hstruct node /定义一个结构体 int num; int code; struct node *Link;typedef struct node NODE; /声明一个结构体类型NODE *createlinklist(int n) /建立链表 NODE *head,*p,*q; int i=1; head=p=(struct node*)malloc(sizeof(struct node); p-num=i; p-code=rand()%100; p-Link=NULL; for(i=2;iLink=NULL; if(q=0) return(0); p-Li
13、nk=q; p=q; p-num=i; p-code=rand()%100; p-Link=head; /使链表尾指向链表头形成循环链表 return head;void printlinklist(NODE *p,int n) /输出链表 int i; NODE *q = p; if(NULL = q-Link) printf(the list is NULL!); return; printf(所有玩家的信息列表:n); for(i=1;inum,p-code); p=p-Link; void joseph(NODE *p,int n,int m) /实现约瑟夫环 int i,j; NOD
14、E *q; for(i=1;in;i+) for(j=1;jLink; q=p-Link; p-Link = q-Link; printf(%d %dn ,q-num,q-code); free(q); printf(n最后剩余的是第%d号的密码是%d.n,p-num,p-code); p-Link=NULL;void main() NODE *head; int n,m; printf(请输入人数N:n); scanf(%d,&n); printf(输入K:n); scanf(%d,&m); head=createlinklist(n); printlinklist(head,n); pri
15、ntf(依次被选出的是:n); joseph(head,n,m);图 34、实现多项式的表示与三则运算(+,-,)#include#include#includetypedef struct node / 定义并声明一个结构体变量 int coe; int exp; struct node* Link;Node;typedef Node* List; /声明一个结构体指针Node*NewNode() / 生成一个新节点 Node*p=(Node*)malloc(sizeof(Node); p-Link=NULL; return p;List BuildList(int n) /生成链表 Nod
16、e*p,*r=NULL,*first=NULL; int i; for(i=0;iLink=p; else first=p; r=p; return first;void printList(Node*p) /多项式显示 for(;p;p=p-Link) printf(%dx%d,p-coe,p-exp); if(p-Link) printf(+); printf(n);List Sort(List SL) / 多项式排序 List p,q; int temp1,temp2; for(p=SL;p!=NULL;p=p-Link) for(q=p-Link;q!=NULL;q=q-Link) i
17、f(p-expq-exp) temp1=q-coe; q-coe=p-coe; p-coe=temp1; temp2=q-exp; q-exp=p-exp; p-exp=temp2; return SL;List AddList(Node*la,Node*lb) /多项式相加 Node *p,*lc=NULL,*r=NULL; while(la!=NULL&lb!=NULL) if(la-exp=lb-exp) p =(Node*)malloc(sizeof(Node); p-coe=la-coe+lb-coe; p-exp=la-exp; p-Link=NULL; la=la-Link; l
18、b=lb-Link; else if(la-expexp) p =(Node*)malloc(sizeof(Node); p-coe=la-coe; p-exp=la-exp; p-Link=NULL; la=la-Link; else p =(Node*)malloc(sizeof(Node); p-coe=lb-coe; p-exp=lb-exp; p-Link=NULL; lb=lb-Link; if(lc!=NULL) r-Link=p; else lc=p; r=p; if(la) r-Link=la; else r-Link=lb; return lc;List MultiList
19、( List LA, List LB) /多项式相乘 List la = LA; List lb = LB; List s=NULL; List lc100 ; List r100 ; List node = NULL; int i=0; for(;iLink) for(lb=LB; lb; lb=lb-Link) node = (List)malloc(sizeof(Node); node-coe = la-coe * lb-coe; node-exp = la-exp + lb-exp; node-Link=NULL; if(lci!=NULL) ri-Link=node; else lc
20、i=node; ri=node; i+; s=lc0; for(i=1;iLink) printf(the coe is ); scanf(%d,&p-coe); printf(the exp is ); scanf(%d,&p-exp); printf(the second multinomial is n); scanf(%d,&b); LB=BuildList(b); r=LB; for(;r;r=r-Link) printf(the coe is ); scanf(%d,&r-coe); printf(the exp is ); scanf(%d,&r-exp); LA=Sort(LA
21、);LB=Sort(LB); printList(LA); printList(LB); LC=AddList(LA,LB); printf(the result of LA+LB isn); printList(LC); S=MultiList( LA, LB); printf(the result of LA*LB isn); printList(S); 图 45.判断输入的数是否为素数?#include #includeusing namespace std;main() long N; coutinput a number from 1 to 100000:N; cout is it
22、a prime number?endl; if (N=2) cout yes2) int i=2; while(isqrt(N) if(N%i=0) cout noendl; return; i+; cout yesendl; else cout errorendl;图 5.1 图 5.2图 5.36. 解释程序#include main() int n; printf(请输入一个数: ); scanf(%d,&n); int a=0x1f; printf(n的十六进制为:%xn,n); printf(a的十六进制为:%xn,a); if (n&a=a) printf(the result i
23、s %dn,n); else printf(do nothing n); 图 6.1 图6.2图 6.3 图6.4#include main() int n; printf(请输入一个数: ); scanf(%d,&n); int a=0x1f; printf(n的十六进制为:%xn,n); printf(a的十六进制为:%xn,a); if (n&a)=a) printf(the result is %dn,n); else printf(do nothing n); 图 6.5 图 6.6 图 6.7 图 6.8主要是运算符号的优先级判断: =(等于) 高于 &(位与)7.时间复杂度分析:分析算法的复杂度,分析频度的次数.按数量级递增排列,常见的时间复杂度:O(1) O(loglog n)O(log n)O()O(n)O(nloglog n)O(nlog n)O()O()O()O()O(n!)O()
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1