zoukankan      html  css  js  c++  java
  • 第二次作业


    #include<stdio.h> void swap(int *p1, int *p2) { int t=*p1; *p1=*p2; *p2=t; return; } int main() { int swap(int *p1,int *p2); int a, b,*x,*y; scanf("%d %d ",&a,&b); x=&a,y=&b; if(a<b)swap(x,y); printf("%d %d ",a,b); return 0; }
    55 44
    5
    55 44
    
    --------------------------------
    Process exited after 7.035 seconds with return value 0
    请按任意键继续. . . 

    结论:不知道为什么,要输入两行数才显示答案。

    要定义N个数才把值指向指针,感觉自己弄的太复杂,但是是我极限了。

    作业二、void swap(int *p,int *q){int *m;*m=*p;*p=*q;*q=*m;}为何无法实现交换?

    #include<stdio.h>
    void swap(int *p1, int *p2)
    {
        int *t;
        *t=*p1;
        *p1=*p2;
        *p2=*t;
        return;
    }
    int main()
    {
        int swap(int *p1,int *p2);
        int a, b,*x,*y;
        scanf("%d %d
    ",&a,&b);
        x=&a,y=&b;
        if(a<b)swap(x,y);
        printf("%d %d
    ",a,b);
        return 0;
    }
    55 66
    2
    
    --------------------------------
    Process exited after 17.81 seconds with return value 3221225477
    请按任意键继续. . .
    

    结论:答案输不出来,指针是随机地址定义的,两个需要交换的地址可能重复了。

    作业三、void swap(int *p,int *q){int *m;m=p;p=q;q=m;}为何无法实现交换?

    #include<stdio.h>
    void swap(int *p1, int *p2)
    {
        int *t;
    	t=p1;
        p1=p2;
        p2=t;
        return;
    }
    int main()
    {
        int swap(int *p1,int *p2);
        int a, b,*x,*y;
        scanf("%d %d
    ",&a,&b);
        x=&a,y=&b;
        if(a<b)swap(x,y);
        printf("%d %d
    ",a,b);
        return 0;
    }
    44 55
    2
    44 55
    
    --------------------------------
    Process exited after 9.999 seconds with return value 0
    请按任意键继续. . .
    

    结论:交换不了,因为两个定义以整形交换的而没有交换地址,所以不能交换值。

    作业四、构建comp函数,实现两个整数比较,比较大的值通过返回值返回给main。要求comp的参数和返回值都是指针类型数据。  

    #include<stdio.h>
    int comp_ab(int a, int b)
    {
         if (a>b) return a; else return b;
     }  
    int main()
    { 
         int a,b,x;
         printf("input a b
    "); 
         scanf("%d %d",&a, &b);
         x = comp_ab(a,b);
         printf("max is %d
    ",x); 
         return 0;
     }
    
    input a b
    4 5
    max is 5
    
    --------------------------------
    Process exited after 3.921 seconds with return value 0
    请按任意键继续. . .
    

    结论:用指针更能省交换地址的时间,所以只指针合理应用更能让我们使程序简洁、高效、紧凑。

      

  • 相关阅读:
    k8s-istio记录
    k8s
    单词 -(动物)
    RxJs
    .netcore 3.1 unbuntu
    单词规整
    AutoMapper
    时间
    ye
    特殊权限
  • 原文地址:https://www.cnblogs.com/wengweng/p/6601097.html
Copyright © 2011-2022 走看看