大白书4-4.1

4.4.1 xiao小问题集锦  solve() 和 f() g()  h()
#include <iostream>
#include <cassert> //算法竞赛入门经典4-4
#include <cstdio>
#include <ctime>
//#include <size>
typedef long long LL;
using namespace std;
int c =2;
int solve(double a,double b,double c,double d,double e,double f){
    double x,y;int flag=0;
    if(a*e != b*d)
    {   
        x = (c*e-b*f)/(a*e-b*d);
        y = (a*f-c*d)/(a*e-b*d);
        flag = 1;
        printf("one answer\n");
    }
    else if(c*e == b*f){flag=2;printf("unlimited answer\n");}
    else {flag =0; printf("no answer\n");}
    assert(flag<=1);
    return flag;
}
int* hehe(){
    int b=0;
    return &b;
}
int f(){
    return c++;
}
int g(){
    return c=c/2;
}
int h(){
    return c=c-1;
}
int main(){
    int a,b;
    a=f();b=f();
    printf("a=%d b=%d\n",a,b);
    a =(f()+g())+h(); b=f()+g()+h();
    printf("a=%d b=%d\n",a,b);
    
    int* aa;
    aa=hehe();
    cout<<*aa<<endl;
    return 0;
}
View Code


/*一个函数结束后,该函数内所有的自动变量
所占用的内存空间都被释放,释放的含义是:
通知系统,这些内存已经是自由的了,可以被
其他代码改写了。没有被改写前,这些内存中还
是原来的值,至于什么时候改写,那谁也不知道。
所以返回它们的地址是没有任何意义的。
记住:”不要返回一个局部非静态变量的地址“
,这是C/C++的金科玉律。*/

#include<stdio.h>
int *w(int a)
{
    int b=a+1;
    return &b;
}
int main()
{
    int a=2,*p;
    p=w(a);
    printf("%d\n",*p);
    return 0;
}

  

posted @ 2018-07-09 15:12  163467  阅读(123)  评论(0编辑  收藏  举报