笔试准备之——C经典算法18例之三

来源:互联网 发布:乌镇人工智能报告 编辑:程序博客网 时间:2024/06/05 17:03
/****************************************************************
13、在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始
位置(第几个),不存在则返回-1

******************************************************************/

#include<stdio.h>#include<stdlib.h>#include<string.h>int search(char s1[],char s2[]){int i=0,j;int len=strlen(s2);while(s1[i]){for(j=0;j<len;j++)if(s1[i+j]!=s2[j]) break;if(j>=len)return i+1;else i++;}return -1;}void main(){char s1[100];char s2[20];printf("enter a long string:\n");gets(s1);printf("enter a short string:\n");gets(s2);printf("result is: %d\n",search(s1,s2));system("pause");}


/****************************************************************
14、用指针变量输出结构体数组元素

******************************************************************/

#include<stdio.h>#include<stdlib.h>struct student{int num;char *name;char sex;int age;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};void main(){int i;struct student *ps;printf("Num \tName\t\t\tSex\tAge\t\n");    /*用指针变量输出结构体数组元素。*/for(ps=stu;ps<stu+5;ps++)printf("%d\t%-8s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);printf("\n");/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);system("pause");}

/****************************************************************
15、建立一个有三个结点的简单链表:
******************************************************************/

#include<stdio.h>#include<stdlib.h>struct student{ int num;char *name;int age ;struct student *next;};void main(){struct student a,b,c,*head,*p;a.num=1001; a.name="lihua"; a.age=18; // 对结点成员进行赋值  b.num=1002; b.name="liuxing"; b.age=19;c.num=1003; c.name="huangke"; c.age=18;head=&a;     //  建立链表,a为头结点  a.next=&b;b.next=&c;c.next=NULL;// 输出链表p=head;       do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);system("pause");}

/****************************************************************
16、输入一个字符串,判断其是否为回文。回文字符串是指从左到右读
和从右到左读完全相同的字符串
******************************************************************/

#include<stdio.h>#include<stdlib.h>#include <string.h>void main(){char s[100];int i,j,n;printf("输入字符串:\n");gets(s); n=strlen(s);//判断是否两边相等for(i=0,j=n-1;i<j;i++,j--)if(s[i]!=s[j])   break;if(i>=j) printf("是回文串\n");else     printf("不是回文串\n");system("pause");}

/****************************************************************
17、冒泡排序,从小到大,排序后结果输出到屏幕及文件test17.out
******************************************************************/

#include<stdio.h>#include<stdlib.h>void fun(int a[],int n){int i,j,t;for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(a[j]>a[j+1]) {t=a[j];a[j]=a[j+1];a[j+1]=t;}}void main(){int a[10],n=10,i;FILE *fp;printf("enter 10 number:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);if((fp=fopen("test17.out","w"))==NULL)printf("open file myf2.out failed!\n");printf("old array:\n");for(i=0;i<10;i++){printf("%-4d",a[i]);    fprintf(fp,"%-4d",a[i]);}fun(a,10);printf("\n sort from small to big:\n");for(i=0;i<10;i++){printf("%-4d",a[i]);    fprintf(fp,"%-4d",a[i]);}printf("\n");fclose(fp);system("pause");}

/****************************************************************
18、编写函数countpi,利用公式
 π/2~=1+(1/3)+(1/3)*(2/5)+(1/3)*(2/5)*(3/7)+(1/3)*(2/5)*(3/7)*(4/9)+...
计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,
请完善函数。将结果显示在屏幕上并输出到文件test18.out中。
******************************************************************/

#include<stdio.h>#include<stdlib.h>double countpi(double eps)    //eps为允许误差{int m=1;double temp=1.0,s=0;while(temp>=eps){ s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}void main(){FILE *fp;double eps=1e-5,pi;if((fp=fopen("test18.out","w"))==NULL)  { printf("cannot open the file\n");exit(0);                       }pi= countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);system("pause");}





0 0
原创粉丝点击