高级语言期末2015级唐班B卷(软件学院)

1.编写函数,按照如下公式计算圆周率π的值(精确到1e-5)

\frac{\pi }{2}=(\frac{2}{1}\frac{2}{3})*(\frac{4}{3}\frac{4}{5})*(\frac{6}{5}\frac{6}{7})*....**(\frac{2n}{2n-1}\frac{2n}{2n+1})

#include <stdio.h>

double pai() {
	double last=0;
	double flag=1;
	int n=1;
	while(flag-last>=1e-5) {
		last=flag;
		flag*=1.0*(2*n)*(2*n)/((2*n-1)*(2*n+1));
		n++;
	}
	return 2*last;
}

int main() {
	printf("%f",pai());
}

2.编写函数int delarr(int a[], int n),删除整型数组a中所有偶数,要求:a数组中剩余元素保存次序顺序存储,函数值返回剩余偶数个数,不能定义额外的新数组

#include <stdio.h>

int delarr(int a[],int n){
	for(int i=0;i<n;i++){
		if(a[i]%2==0){
			for(int j=i;j<n-1;j++)
				a[j]=a[j+1];
			i--;
			n--;
		}
	}
	return n;
}

3.给一个正整数n,将其拆分为n个1。可行的拆分过程为:(设定一个初值为0的累加器)。首先,将n拆分为2个数,两个数之和为n,二者之差的绝对值加入累加器中:再对拆分得到的2个数分别进行上述“拆分+累加”操作,直到所有数均拆分为1为止。编写递归程序,计算所有可行拆分过程所对应的的累加器的最小值。提示:递归公式如下

f(n)=\left\{\begin{matrix} 0,n=1 & \\ min_{i=1,...,n/2} {(n-i)-i+f(i)+f(n-i)}& \end{matrix}\right.

#include <stdio.h>

int func(int n) {
	if(n==1)
		return 0;
	int min=func(n-1)+n-2;
	for(int i=2; i<=n/2; i++) {
		int temp=n-i-i+func(i)+func(n-i);
		if(min>temp)
			min=temp;
	}
	return min;
}

4.职工的信息卡至少包括工号、姓名出生年月等信息。限定:工号为整形且不超过整形的取值范围。
1)定义存储职工信息的单向链表的节点类型;
2)假定管理职工信息的单向链表已经从小到大排序,编写函数,由键盘键入1个职工的工号,删除该职工的全部信息
3)编写函数,将职工信息的单向链表中所有出生月份大于y的职工的完整信息,存储到文件out.txt中
 

#include <stdio.h>
#include <stdlib.h>

typedef struct Date {
	int y,m;
} Date;

typedef struct node {
	int num;
	char name[20];
	struct Date birthdate;
	struct node * next;
} node;

struct node *del(struct node *head) {
	int search;
	scanf("%d",&search);
	if(head->num==search)
		return head->next;
	struct node *p=head->next,*q=head;
	while(p!=NULL&&search!=p->num) {
		q=p;
		p=p->next;
	}
	q->next=p->next;
	return head;
}

void save(struct node *head,int y) {
	FILE *file;
	if((file=fopen("in.txt","w"))==NULL) {
		printf("open error");
		exit(0);
	}
	while(head!=NULL) {
		if(head->birthdate.y>y)
			fprintf(file,"%d%10s%d%d\n",head->num,head->name,head->birthdate.y,head->birthdate.m);
		head=head->next;
	}
	fclose(file);
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值