c++ for循环有意思的代码
时间: 2024-03-24 07:32:10 浏览: 46
有趣的循环 代码
以下是一些有趣的C++ for循环代码:
1. 倒序输出数组元素:
```
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 4; i >= 0; i--) {
cout << arr[i] << " ";
}
```
2. 输出九九乘法表:
```
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "x" << i << "=" << i*j << " ";
}
cout << endl;
}
```
3. 输出斐波那契数列前n项:
```
int n = 10;
int a = 0, b = 1, c;
for (int i = 1; i <= n; i++) {
cout << a << " ";
c = a + b;
a = b;
b = c;
}
```
阅读全文