C++实验一

第一题

//main.c

#include

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

//func.c

#include

   void func(int *a, int *b)

   {

       int temp=*a;

       *a=*b;

       *b=temp;

   }

//func.h

void func(int *a, int *b);

第二题

//main.c

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

    //2

       printf("ä?ÌY给?处ä|理¤¨ª后¨®Ì?参?数ºy是º?%d,%d\n",a,b);

//func.c

#include

   void func(int *a, int *b)

   {

       int temp=*a;

       *a=*b;

       *b=temp;

   }

//func.h

void func(int *a, int *b);

第三题

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

    //2

       printf("ä?ÌY给?处ä|理¤¨ª后¨®Ì?参?数ºy是º?%d,%d\n",a,b);

    //3

       c=gcd(a,b);

       printf("a和¨ªb的Ì?Á?䨮公?约?数ºy为a:êo%d\n", c);

//gcd.c

#include

int gcd(int a,int b)

{

    while ((a-b)!= 0)

    {

        if (a > b)a =a- b;

        else b =b- a;

    }

    return a;

}

//gcd.h

int gcd(int a,int b);

第四题

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

    //2

       printf("ä?ÌY给?处ä|理¤¨ª后¨®Ì?参?数ºy是º?%d,%d\n",a,b);

    //3

       c=gcd(a,b);

       printf("a和¨ªb的Ì?Á?䨮公?约?数ºy为a:êo%d\n", c);

    //4

      

       printf("输º?入¨?你?要°a查¨¦找¨°的Ì?数ºy下?标À¨º为a:");

        scanf_s("%d", &d);

        arr(d);

//arr.c

#include

int arr(int x)

{

    int a[]={2,3,4,5,6,7,8,9,10};

    int i=0;

    for(i;i<9;i++){

        if(a[i]== x )

        {

        printf("你?要°a查¨¦找¨°的Ì?数ºy为a:êo%d\n",i);

        return 0;

        }

       

    }

    printf("数ºy组Á¨¦中D没?有®Dä?数ºy");

}

//arr.h

int arr(int x);

第五题

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

    //2

       printf("ä?ÌY给?处ä|理¤¨ª后¨®Ì?参?数ºy是º?%d,%d\n",a,b);

    //3

       c=gcd(a,b);

       printf("a和¨ªb的Ì?Á?䨮公?约?数ºy为a:êo%d\n", c);

    //4

      

       printf("输º?入¨?你?要°a查¨¦找¨°的Ì?数ºy下?标À¨º为a:");

        scanf_s("%d", &d);

        arr(d);

       

    //5

        //int a[]={2,3,4,5,6,7,8,9};

        //int m,n;

        printf("输º?入¨?f你?要°a查¨¦找¨°的Ì?数ºy下?标À¨º为a:");

        scanf_s("%d",&m);

        n=biSearch(10,m);

        if (n==-1)

             printf("没?找¨°到Ì?");

        else

             printf("%dÌ?下?标À¨º是º?%d\n",m,n);

//biSearch.c

    int biSearch(int len,int x)

{

    int a[10]={2,3,4,5,6,7,8,9,10};

    int i;

for (i=0;i

{

if (x==a[i])

return i;

}

return -1;

}

//biSearch.h    

int biSearch(int len,int x);

第六题

int main(void)

{   int a=5,b=6;

     int c;

     //int h[9]={1,2,3,4,5,6,7,8,9};

     int d;

     //int a[]={0,1,2,3,4,5,6,7,8,9}; 

     int m,n,q;

     unsigned long w;

     //1

       printf("即¡ä将?调̡®?func!\n");

       printf("ä?ÌY给?处ä|理¤¨ª前¡ãÌ?参?数ºy是º?%d,%d\n",a,b);

       func(&a,&b);

       printf("退ª?出?func!");

    //2

       printf("ä?ÌY给?处ä|理¤¨ª后¨®Ì?参?数ºy是º?%d,%d\n",a,b);

    //3

       c=gcd(a,b);

       printf("a和¨ªb的Ì?Á?䨮公?约?数ºy为a:êo%d\n", c);

    //4

      

       printf("输º?入¨?你?要°a查¨¦找¨°的Ì?数ºy下?标À¨º为a:");

        scanf_s("%d", &d);

        arr(d);

       

    //5

        //int a[]={2,3,4,5,6,7,8,9};

        //int m,n;

        printf("输º?入¨?f你?要°a查¨¦找¨°的Ì?数ºy下?标À¨º为a:");

        scanf_s("%d",&m);

        n=biSearch(10,m);

        if (n==-1)

             printf("没?找¨°到Ì?");

        else

             printf("%dÌ?下?标À¨º是º?%d\n",m,n);

    //6

        printf("请?输º?入¨?楼£¤梯¬YÌ?阶¡Á数ºy:");

        scanf_s("%d",&q);

        w=cou(q);

        printf("®D%d种?爬¨¤楼£¤梯¬YÌ?方¤?法¤¡§\n",w);

//taijie.c

unsigned long cou (int n)

{

unsigned long f;

if(n==1)

f=1;

else

if(n==2)

f=2;

else

f=cou(n-1)+cou(n-2);

return(f);

}

// taijie.h

unsigned long cou (int n);

C++实验一_第1张图片

你可能感兴趣的:(算法,数据结构,图论)