您的位置:首页 > 其它

北航2010年机考题

2016-06-25 13:53 441 查看
1.利用泰勒公式求cos(x)=1-x2/2!+x4/4!-……

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

double fun(int x)
{
double sum = 1;
for(int i=1;i<=x;i++){
sum *= i;
}
return sum;
}

double cos(int x){
double sum = 1;
double temp;
int i = 2;
int flag = -1;
do{
temp = flag*pow(x,i)/fun(i);
sum += temp;
i = i+2;
flag = -flag;
}while(abs(temp)>1e-6);
return sum;

}

int main()
{
int x;
double sum=0;
scanf("%d",&x);
sum = cos(x);
printf("cos(%d)=%f\n",x,sum);
return 0;
}


2.归并两个有序字符串,要求输出不能有重复字符
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

char *Merge(char *a,char *b)
{
char c[200];
int i=0,j=0,k=0;

while(i<strlen(a)&&j<strlen(b)){
if(a[i]>b[j]){
c[k++] = b[j];
j++;
}
else if(a[i]<b[j]){
c[k++] = a[i];
i++;
}
else if(a[i] == b[j]){
c[k++] = a[i];
i++;
j++;
}
}
while(i<strlen(a)){
c[k++] = a[i++];
}
while(j<strlen(b)){
c[k++] = b[j++];
}
c[k] = '\0';
return c;
}

int main()
{
char a[100];
char b[100];
char *c;
int i=0;
scanf("%s",a);
scanf("%s",b);
c = Merge(a,b);
printf("%s",c);
}

3.两个整数数组(无序,可有重复元素),判断两个整数数组是否完全相同(重复元素的话,重复次数也要相同)

有两种思路,第一种是对两个数组进行排序,而后比较两个数组是否相同;第二种思路,直接比较两个数组。
以下代码是第二种思路:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i,ii,j,k,l;
scanf("%d",&n);
int* array0=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",array0+i);
int* array1=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",array1+i);
for(i=0;i<n;i++)
{
j=l=0;
for(ii=0;ii<n;ii++)
if(array0[i]==array0[ii])
j++;
for(k=0;k<n;k++)
if(array1[k]==array0[i])
l++;
if(j!=l)
{
printf("not equal!\n");
exit(0);
}
}
printf("equal! \n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: