建网站广州,dw网页设计源代码,免费的建设网站软件下载,超链接到网站怎么做视频文件1.编写函数#xff0c;判定正整数m和n#xff08;均至少为2#xff09;是否满足#xff1a;数m为数n可分解的最小质因数#xff08;数n可分解的最小质因数为整除n的最小质数#xff09; 提示#xff1a;判定m为质数且m是n的最小因数
#include stdio.h
#include…1.编写函数判定正整数m和n均至少为2是否满足数m为数n可分解的最小质因数数n可分解的最小质因数为整除n的最小质数 提示判定m为质数且m是n的最小因数
#include stdio.h
#include stdbool.hbool isprime(int m,int n) {for(int i0; im; i) {if(m%i0||n%i0)return false;}if(n%m!0)return false;return true;
}2.编写函数对给定的整数数组a的前n个元素排序使得所有正整数均出现在负整数和零之前。 提示排序结果中正整数之间的出现顺序不受限制负整数和0之间的出现顺序不限制。 例如原数组为-1、4、-3、0、2、1、-9、7则排序后可以为4、2、1、7、0、-1、-3、-9
#include stdio.hvoid sort(int *arr,int n) {for(int i0; in-1; i)for(int j0; jn-i-1; j)if(arr[j]0) {int temparr[j];arr[j]arr[j1];arr[j1]temp;}for(int i0; in-1; i)for(int j0; jn-i-1; j)if(arr[j]0) {int temparr[j];arr[j]arr[j1];arr[j1]temp;}
}int main() {int arr[] {-1,4,-3,0,2,1,-9,7};sort(arr,8);for(int i0; i8; i) {printf(%d ,arr[i]);}
}
3.编写递归函数实现按照如下公式计算的功能。其中n为自然数。 提示如果独立编写阶乘运算函数可以不考虑存储溢出默认结果类型为整型。 #include stdio.hint fac(int n) {if(n0)return 1;elsereturn n*fac(n-1);
}double func(int n) {if(n0)return 0;return 1.0*n/((n1)*fac(n2))func(n-1);
}
4.定义一个表示学生的结构体包含3个字段姓名、性别、成绩编写函数将下图所示的结构体数组s中的前n个学生的信息存储到当前目录下的output.txt中。 提示性别可以定义为bool、int、enum等类型均可存储信息的具体形式不限制
张三李四......赵九男true女false)男true837697
例如一个教师的信息为Zhangsan、true、50另一个教师的信息为Lisi、false、37。
#include stdio.h
#include stdbool.h
#include stdlib.htypedef struct student {char name[20];bool sex;int score;
} student;void save(struct student stu[],int n) {FILE *file;if((filefopen(out.txt,w))NULL) {printf(open error);exit(0);}for(int i0; in; i) {fprintf(file,%s %d %d\n,stu[i].name,stu[i].sex,stu[i].score);}fclose(file);
}
5.定义一个单链表每个结点包含2个字段整数信息、后继指针如下图所示。编写函数删除该单链表中所含整数信息等于x的多个重复结点。 例如若单链表中存储的整数信息依次是1、5、5、0、5、6、0、0、5、1如果x为5则得到的单链表中相应信息依次是1、0、6、0、0、1。
#include stdio.h
#include stdbool.h
#include stdlib.htypedef struct node {int key;struct node *next;
} node;void del(struct node *head,int x) {struct node *dummyhead(struct node *)malloc(sizeof(struct node));dummyhead-nexthead;struct node *phead,*predummyhead;while(p!NULL) {if(p-keyx)pre-nextp-next;elseprep;pp-next;}
}