#include"../exercise.h"// TODO: 在这里声明函数intadd(int a,int b);intmain(int argc,char**argv){ASSERT(add(123,456)==123+456,"add(123, 456) should be 123 + 456");auto x =1, y =2;
std::cout << x <<" + "<< y <<" = "<<add(x, y)<< std::endl;return0;}intadd(int a,int b){// TODO: 补全函数定义,但不要移动代码行return a+b;}
```
// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>#include"../exercise.h"voidfunc(int);// TODO: 为下列 ASSERT 填写正确的值intmain(int argc,char**argv){auto arg =99;ASSERT(arg ==99,"arg should be ?");
std::cout <<"befor func call: "<< arg << std::endl;func(arg);ASSERT(arg ==99,"arg should be ?");
std::cout <<"after func call: "<< arg << std::endl;return0;}// TODO: 为下列 ASSERT 填写正确的值voidfunc(int param){ASSERT(param ==99,"param should be ?");
std::cout <<"befor add: "<< param << std::endl;
param +=1;ASSERT(param ==100,"param should be ?");
std::cout <<"after add: "<< param << std::endl;}
```
#include"../exercise.h"// READ: `static` 关键字 <https://zh.cppreference.com/w/cpp/language/storage_duration>// THINK: 这个函数的两个 `static` 各自的作用是什么?staticintfunc(int param){staticint static_ = param;// std::cout << "static_ = " << static_ << std::endl;return static_++;}intmain(int argc,char**argv){// TODO: 将下列 `?` 替换为正确的数字ASSERT(func(5)==5,"static variable value incorrect");ASSERT(func(4)==6,"static variable value incorrect");ASSERT(func(3)==7,"static variable value incorrect");ASSERT(func(2)==8,"static variable value incorrect");ASSERT(func(1)==9,"static variable value incorrect");return0;}
```
#include"../exercise.h"//递归很可怕,内存消耗大,时间复杂度高,所以我们可以用循环来实现斐波那契数列constexprunsignedlonglongfibonacci(int i){if(i<=1)return i;unsignedlonglong a=0,b=1,c=0;for(int j=2;j<=i;j++){
c=a+b;
a=b;
b=c;}return b;}intmain(int argc,char**argv){constexprauto FIB20 =fibonacci(20);ASSERT(FIB20 ==6765,"fibonacci(20) should be 6765");
std::cout <<"fibonacci(20) = "<< FIB20 << std::endl;// TODO: 观察错误信息,修改一处,使代码编译运行constexprauto ANS_N =100;auto ANS =fibonacci(ANS_N);
std::cout <<"fibonacci("<< ANS_N <<") = "<< ANS << std::endl;return0;}
#include"../exercise.h"//递归很可怕,内存消耗大,时间复杂度高,所以我们可以用循环来实现斐波那契数列constexprunsignedlonglongfibonacci(int i){if(i<=1)return i;unsignedlonglong a=0,b=1,c=0;for(int j=2;j<=i;j++){
c=a+b;
a=b;
b=c;}return b;}intmain(int argc,char**argv){constexprauto FIB20 =fibonacci(20);ASSERT(FIB20 ==6765,"fibonacci(20) should be 6765");
std::cout <<"fibonacci(20) = "<< FIB20 << std::endl;// TODO: 观察错误信息,修改一处,使代码编译运行constexprauto ANS_N =100;auto ANS =fibonacci(ANS_N);
std::cout <<"fibonacci("<< ANS_N <<") = "<< ANS << std::endl;return0;}
#include"../exercise.h"// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算staticunsignedlonglongfibonacci(int i){// TODO: 为缓存设置正确的初始值staticunsignedlonglong cache[128]={0,1}, cached;// TODO: 设置正确的循环条件if(i<=1)return i;for(cached=2;cached<=i;++cached){
cache[cached]= cache[cached -1]+ cache[cached -2];}return cache[i];}// ---- 不要修改以下代码 ----intmain(int argc,char**argv){ASSERT(fibonacci(0)==0,"fibonacci(0) should be 0");ASSERT(fibonacci(1)==1,"fibonacci(1) should be 1");ASSERT(fibonacci(2)==1,"fibonacci(2) should be 1");ASSERT(fibonacci(3)==2,"fibonacci(3) should be 2");ASSERT(fibonacci(10)==55,"fibonacci(10) should be 55");auto fib100 =fibonacci(100);
std::cout <<"fibonacci(100) = "<< fib100 << std::endl;ASSERT(fib100 ==3736710778780434371,"fibonacci(100) should be 3736710778780434371");return0;}