回顾
- 猴子爬杆
猴子每天爬3米,再向下退1米,到第十天后刚好到达杆顶,问杆子高度
#include<iostream>
using namespace std;
int main(){
int day=1,high=0;
while(day<10){
high=high+2;
day++;
}
high=high+3;
std::cout<<high;
return 0;
}
- 水仙花数
遍历从100到1000的水仙花数
水仙花数:个位3+十位3+百位3=数值
#include<iostream>
using namespace std;
int main(){
int ge,shi,bai;
for(int n=100;n<=1000;n++){
ge=n%10;
shi=n%100/10;
bai=n/100;
if(ge*ge*ge+shi*shi*shi+bai*bai*bai==n)
std::cout<<n<<"是水仙花数";
}
return 0;
}
- 菱形图
#include <iostream>
int main() {
int n = 7; // 菱形的高度,必须为奇数
int mid = n / 2; // 中间行的索引
// 打印上半部分(包括中间行)
for (int i = 0; i <= mid; ++i) {
// 打印空格
for (int j = 0; j < mid - i; ++j) {
std::cout << " ";
}
// 打印星号
for (int j = 0; j < 2 * i + 1; ++j) {
std::cout << "*";
}
std::cout << std::endl;
}
// 打印下半部分
for (int i = mid - 1; i >= 0; --i) {
// 打印空格
for (int j = 0; j < mid - i; ++j) {
std::cout << " ";
}
// 打印星号
for (int j = 0; j < 2 * i + 1; ++j) {
std::cout << "*";
}
std::cout << std::endl;
}
return 0;
}
数字运算
#include <iostream>
#include <cmath>
int main() {
double x = 25.0;
std::cout << "sqrt(x) = " << sqrt(x) << std::endl;
std::cout << "pow(2.0, 3.0) = " << pow(2.0, 3.0) << std::endl;
// abs for int, fabs for double
std::cout << "abs(-10) = " << abs(-10) << std::endl;
std::cout << "fabs(-10.5) = " << fabs(-10.5) << std::endl;
std::cout << "sin(3.14159 / 2) = " << sin(3.14159 / 2) << std::endl;
return 0;
}
数组(Array)
存储多个相同类型的变量。
int arr[5];
int arr[5] = {1, 2, 3, 4, 5};
int arr[5] = {};
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
通过索引访问和修改数组元素:
arr[0] = 10;
int value = arr[1];
运用for循环
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << std::endl;
}
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// 修改数组元素
arr[0] = 10;
// 输出数组元素
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << std::endl;
}
return 0;
}
多维数组
用于表示多维数据的数组,如二维数组可以表示一个矩阵。
type arrayName[rows][columns];
#include <iostream>
int main() {
// 声明和初始化一个2x3的二维数组
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// 遍历并输出二维数组
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
函数(Functions)
实现特定任务的代码块。
int add(int a, int b); // 函数声明
int add(int a, int b) { // 函数定义
return a + b;
}
函数的调用:调用函数并使用其返回值:
int result = add(3, 4);
#include <iostream>
// 函数声明
int add(int a, int b);
int main() {
int sum = add(5, 7); // 函数调用
std::cout << "Sum: " << sum << std::endl;
return 0;
}
// 函数定义
int add(int a, int b) {
return a + b;
}
#include <iostream>
using namespace std;
// 函数声明
int max(int num1, int num2);
int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
int ret;
// 调用函数来获取最大值
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// 函数返回两个数中较大的那个数
int max(int num1, int num2)
{
// 局部变量声明
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
#include <iostream>
// 函数声明
int add(int a, int b);
int subtract(int a, int b);
int main() {
int n = 7;
// 调用数学函数
int sum = add(5, 3);
int difference = subtract(5, 3);
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
return 0;
}
// 数学函数:加法
int add(int a, int b) {
return a + b;
}
// 数学函数:减法
int subtract(int a, int b) {
return a - b;
}
字符串
字符串是一种字符序列,用于表示文本数据。
#include <iostream>
#include <string>
int main() {
// 使用字符数组声明和初始化字符串
char charArray[] = "Hello, World!";
// 使用std::string声明和初始化字符串
std::string str = "Hello, C++!";
std::cout << charArray << std::endl;
std::cout << str << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "World";
// 拼接字符串
std::string str3 = str1 + ", " + str2 + "!";
std::cout << "Concatenated string: " << str3 << std::endl;
// 比较字符串
if (str1 == str2) {
std::cout << "The strings are equal." << std::endl;
} else {
std::cout << "The strings are not equal." << std::endl;
}
// 查找子字符串
std::size_t found = str3.find("World");
if (found != std::string::npos) {
std::cout << "'World' found at position: " << found << std::endl;
} else {
std::cout << "'World' not found." << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 获取字符串长度
std::cout << "Length of string: " << str.length() << std::endl;
// 替换子字符串
str.replace(7, 5, "C++");
std::cout << "After replacement: " << str << std::endl;
// 转换为大写
for (char& c : str) {
c = toupper(c);
}
std::cout << "Uppercase string: " << str << std::endl;
return 0;
}