1.编程序,打印前十对孪生素数。若两个素数之差为2,则称为孪生素数,例如(3,5)(5,7)(11,13)
#include <stdio.h>
#include <math.h>
int isprime(int n) {
if(n<=1)
return 0;
for(int i=2; i<=sqrt(n); i++) {
if(n%i==0)
return 0;
}
return 1;
}
int main() {
int count=0;
for(int i=3; count<10; i++)
if(isprime(i)&&isprime(i+2)) {
printf("%d %d\n",i,i+2);
count++;
}
}
2.设幂数为非负整数的多项式。将每一项的系数和幂次存于下表:
#include <stdio.h>
typedef struct muti {
int pow;
float coe;
} muti;
float getresult(struct muti p[],int x) {
float result=0,temp;
int i=0;
while(p[i].pow>0) {
temp=p[i].coe;
for(int j=0; j<p[i].pow; j++)
temp*=x;
result+=temp;
i++;
}
if(p[i].pow==0)
result+=p[i].coe;
return result;
}
3.编程序判断10阶整数方阵是否关于主对角线对称
#include <stdio.h>
int issym(int maxt[][10]) {
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
if(maxt[i][j]!=maxt[j][i])
return 0;
return 1;
}
4.编写一个函数,用递归计算Hermite多项式的函数,Hermite多项式定义为
#include <stdio.h>
int func(int n,int x) {
if(n==0)
return 1;
else if(n==1)
return 2*x;
return 2*x*func(n-1,x)-2*(n-1)*func(n-2,x);
}
5.编函数,用冒泡实现由整数组成的单向链表的排序
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node *next;
} node;
int listlen(struct node *head) {
int count=0;
while(head!=NULL) {
count++;
head=head->next;
}
return count;
}
struct node *sort(struct node *head) {
if(head==NULL||head->next==NULL)
return head;
int n=listlen(head);
struct node *dummyhead=(struct node*)malloc(sizeof(struct node));
dummyhead->next=head;
for(int i=0; i<n-1; i++) {
int swap=0;
struct node *p=dummyhead->next,*pre=dummyhead;
for(int j=0; j<n-i-1; j++) {
if(p->key>p->next->key) {
struct node *q=p->next;
pre->next=q;
pre=q;
p->next=q->next;
q->next=p;
swap=1;
} else {
pre=p;
p=p->next;
}
}
if(swap==0);
break;
}
return dummyhead->next;
}