C++ 教程 在线

2632cpp-while-loop

当 while() 中的条件值为 0 时,循环就结束了。

开始 y = 10,每循环一次 y 的值就减 1(y-- 会导致 y 减 1),当 y 值一直减到 0 时,退出 while 循环,但是还要继续做 y --操作,所以 y 最后的值就是 -1。

#include <stdio.h>
int main()
{ 
    int y = 10;
    int count = 0;
    while (y--) {
        ++count;
        printf("第%d次:y=%d\n", count, y);
    }
    printf("最后y的值:%d\n", y); 
    return 0;
}

2631cpp-pointer-operators

调用时变量前加 "&" -------返回该变量的地址

声明时变量前加 "基本类型 *" -------该指针变量表示另一个普通变量的地址   eg:int * 或 char *

调用时变量前加 "*"-------表示取该地址的内容

声明时变量前加 "基本类型 **"-------该二级指针变量表示另一个一级"基本类型 *"指针变量地址   

2089cpp-examples-monkey-eating-peach

使用递归可以很好的解决此问题....

#include <iostream>
using namespace std;
int returnPeach(int day, int peach)
{
    if(day > 1)
    {
        peach = peach * 2 + 2;
                day -= 1;
        return returnPeach(day, peach);
    }
    else
    {
        return peach;
    }
}
int main(void)
{
    int peach = returnPeach(10,1);
    cout<<"第一天共摘桃子的数量为:"<<peach<<endl;
    system("pause");
    return 0;
}

2088c-function-localtime

time_t rawtime; // 定义时间变量值 rawtime
struct tm *timeinfo; // 定义 tm 结构指针
time ( &rawtime ); // 获取当前工作时间值,并赋值给 rawtime
timeinfo = localtime ( &rawtime ); // localtime() 将参数 rawtime 所指的 time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构 timeinfo 返回。

2025cpp-examples-monkey-eating-peach

参考方法:

#include <iostream>
int main() {
  for (int x = 1, n = 9; n > 0; n--) {
    std::cout << "第" << n << "天吃之前有" << (x + 1) * 2 << "个桃子"
              << std::endl;
    std::cout << "第" << n << "天吃完后有" << x << "个桃子" << std::endl;
    x = (x + 1) * 2;
  }
  return 0;
}