1.编写程序根据如下公式计算X的值(精确到1e-5)。
#include <stdio.h>
int main(){
int i=1;
double flag=1.0/(2*i-1)*2.0*i/(2*i-1);
double sum=0;
while(flag>1e-5){
sum+=flag;
i++;
flag=1.0/(2*i-1)*2.0*i/(2*i-1);
}
printf("%lf",sum);
return 0;
}
2.编写程序,输出一个n行n列矩阵中所有鞍点aij,即元素aij满足下列条件:
1)aij是第i行中所有元素的最小值
2)aij是第j列中所有元素的最大值
#include <stdio.h>
#include <stdbool.h>
int main() {
int n;
scanf("%d",&n);
int a[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&a[i][j]);
int count=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++) {
bool max=true,min=false;
for(int k=0; k<n; k++)
if(a[i][k]<a[j][j])
min=false;
for(int k=0; k<n; k++)
if(a[k][j]>a[i][j])
max=false;
if(max&&min) {
printf("%d\n",a[i][j]);
count++;
}
}
if(count==0)
printf("NONE\n");
return 0;
}
4.编写程序,将一个单链表转换为一个反向的单链表。
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node *next;
} node;
struct node* reverse(struct node* head) {
struct node* dummyhead = (struct node*)malloc(sizeof(struct node));
dummyhead -> next = NULL;
while(head != NULL) {
struct node* temp = head -> next;
head -> next = dummyhead -> next;
dummyhead -> next = head;
head = temp;
}
return dummyhead -> next;
}
5.每个教师的信息卡片包括教工号、姓名、职称和学院名称三项,编写程序,由键盘依次输入n个教师的信息,创建一个用于管理教师信息的单链表,如下图所示(必须说明该单链表中每个结点的数据类型定义);并保证添加完0所有信息后,该单链表按照教工号从小到大排序。
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct teacher {
int num;
char name[30];
char detail[30];
struct teacher *next;
} teacher;
struct teacher *create(int n) {
struct teacher *head=(struct teacher *)malloc(sizeof(struct teacher));
head->next=NULL;
struct teacher *pre=head;
for(int i=0; i<n; i++) {
struct teacher *p=(struct teacher *)malloc(sizeof(struct teacher));
scanf("%d %s %s",&p->num,&p->name,&p->detail);
while(pre->next!=NULL&&pre->next->num<p->num)
pre=pre->next;
p->next=pre->next;
pre->next=p;
pre=head;
}
return head->next;
}