1.编写函数,数列的前n项和。例如当n等于5时s(5) = 1 - 1/2 + 1/3 - 1/4 + 1/5
#include <stdio.h>
float func(int n) {
float sum=0;
int sign=1;
for(int i=0; i<=n; i++) {
sum+=1.0*sign/i;
sign*=-1;
}
return sum;
}
2.编程序判断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;
}
3.编写函数比较两个字符串的大小,当s1>s2时,返回1,s1<s2时,返回-1,否则s1=s2时,返回0
#include <stdio.h>
int strcmp(char *str1,char *str2) {
int i=0;
while(str1[i]!='\0'&&str2[i]!='\0') {
if(str1[i]>str2[i])
return 1;
else if(str1[i]<str2[i])
return -1;
else
i++;
}
if(str1[i]=='\0'&&str2[i]=='\0')
return 0;
else if (str1[i]=='\0')
return -1;
else
return 1;
}
4.编写函数计算如下多项式的第n项值
#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 {
char ch;
struct node *next;
} node;
struct node* resort(struct node* head) {
struct node *arghead=(struct node *)malloc(sizeof(struct node));
struct node *dighead=(struct node *)malloc(sizeof(struct node));
arghead->next=NULL;
dighead->next==NULL;
struct node *rear1=arghead,*rear2=dighead,*p=head;
while(p!=NULL) {
struct node *temp=p->next;
if(p->ch>='a'&&p->ch<='z'||p->ch>='A'&&p->ch<='Z') {
p->next=rear1->next;
rear1->next=p;
rear1=p;
} else {
p->next=rear2->next;
rear2->next=p;
rear2=p;
}
p=temp;
}
rear2->next=arghead->next;
return dighead->next;
}