取得一个数组的某一位数


点击展开代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main() {
int a, g, s ,b ,q;
cout << "请输入一个4位数" << endl;
cin >> a;
g = a % 10;
s = (a/10)%10;
b = (a/100)%10;
q = (a/1000);
cout << "千位是" << q << endl;
cout << "百位是" << b << endl;
cout << "十位是" << s << endl;
cout << "个位是" << g << endl;
return 0;
}

保留三位小数


点击展开代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
float a, b, c;
cin >> a >> b;
c = a/b;
cout << fixed << setprecision(3);//
cout << c << endl;
return 0;
}

超级nb的记忆递推法


点击展开代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>  // 引入输入输出流库,用于cin/cout
#include<vector> // 引入向量容器库,用于使用vector
using namespace std; // 使用标准命名空间,避免std::前缀

// 使用记忆化递归计算斐波那契数列
vector<int> memo; // 声明全局记忆化数组,用于存储已计算的结果

// 斐波那契函数定义
int fib(int n) { // 定义递归函数fib,参数n表示要计算的斐波那契数列项数
// 处理非法输入情况
if (n <= 0) { // 如果n小于等于0,说明输入不合法
cout << "请输入正整数" << endl; // 提示用户输入正整数
return -1; // 返回-1表示错误
}

// 处理斐波那契数列的基础情况
if (n == 1) return 0; // 斐波那契数列第1项定义为0
if (n == 2) return 1; // 斐波那契数列第2项定义为1

// 记忆化检查:如果已经计算过该项,直接返回存储的结果
if (memo[n] != -1) return memo[n]; // -1表示未计算,非-1表示已计算

// 递归计算:将问题分解为两个子问题
memo[n] = fib(n-1) + fib(n-2); // 计算fib(n) = fib(n-1) + fib(n-2),并存储结果

return memo[n]; // 返回计算得到的结果
}

// 主函数
int main() {
int n; // 声明变量n,用于存储用户输入的项数

// 获取用户输入
cout << "请输入要计算的斐波那契数列项数: "; // 提示用户输入
cin >> n; // 读取用户输入的整数到变量n中

// 初始化记忆化数组
memo.resize(n+1, -1); // 调整memo大小为n+1(索引0到n),所有元素初始化为-1

// 计算并输出结果
cout << "斐波那契数列第" << n << "项是: " << fib(n) << endl; // 调用fib函数并输出结果

return 0; // 程序正常结束,返回0
}

cpp中 关于vector数组与原生数组的一些区别


点击展开

问题背景

小猪比较体重:有几只小猪要比较体重

点击展开第一版代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//第一版
#include<iostream>
#include<vector>
using namespace std;
vector<int> pig;
int big = 0;
int main(){ cout << "请输入小猪的体重" << endl;
int num;
while(cin >> num){
pig.push_back(num);
// 检查下一个字符是否是回车
if (cin.peek() == '\n') { break; }
}
int a;
a = sizeof(pig)/sizeof(pig[0]);
for (int i = 0;i < a;i++){
if(pig[i] > big)big = pig[i]; }
cout << big << endl; }
点击展开第二版代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//第二版
#include<iostream>
#include<vector>
using namespace std;
vector<int> pig;
int big = 0;
int main(){
cout << "请输入小猪的体重" << endl;
int num;
while(cin >> num){
pig.push_back(num);

// 检查下一个字符是否是回车
if (cin.peek() == '\n') {
break;
}
}
int a = pig.size();
for (int i = 0;i < a;i++){
if(pig[i] > big)big = pig[i];
}
cout << big << endl;
}
在代码中,原本写了:
1
2
vector<int> pig;
a = sizeof(pig)/sizeof(pig[0]);
期望得到 `vector` 的元素个数,但结果不对。

问题原因

  • pig 是一个 **std::vector**,不是原生数组。
  • sizeof(pig) 返回的是 vector 对象本身的大小(通常 24 或 32 字节,取决于实现)。
  • sizeof(pig[0]) 返回的是一个 int 的大小(通常 4 字节)。
  • 所以 sizeof(pig)/sizeof(pig[0]) 得到的只是一个固定的小整数(比如 6 或 8),并不是元素个数

正确做法

使用 vector 的成员函数 .size() 获取元素个数:

1
int a = pig.size();

改进后的代码片段

1
2
3
4
int a = pig.size();
for (int i = 0; i < a; i++) {
if (pig[i] > big) big = pig[i];
}

利用 STL 算法 std::max_element(更简洁):

1
2
3
4
5
6
#include <algorithm>

if (!pig.empty()) {
int big = *max_element(pig.begin(), pig.end());//这个地方是直接遍历数组pig的全部数据
cout << big << endl;
}

一种奇奇妙妙去重的方法


点击展开代码(cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>  // 包含所有标准C++库头文件
using namespace std; // 使用标准命名空间,避免std::前缀

int main() {
int n; // 声明变量n,用于存储数组元素个数
cin >> n; // 从标准输入读取元素个数n

vector<int> arr(n); // 创建大小为n的整型向量arr,用于存储输入数据

// 循环读取n个整数到arr数组中
for(int i = 0; i < n; i++) {
cin >> arr[i]; // 读取第i个整数并存储到arr[i]
}

// 对arr向量进行升序排序
// sort函数对[arr.begin(), arr.end())范围内的元素进行排序
sort(arr.begin(), arr.end());

// 创建arr1向量,用于存储去重后的结果
vector<int> arr1;

// 遍历排序后的数组,进行去重操作
for(int i = 0; i < n; i++) {
// 去重条件判断:
// 1. 如果是第一个元素(i==0),直接添加(前面没有元素可比较)
// 2. 或者当前元素不等于前一个元素(arr[i] != arr[i-1]),说明不是重复元素
if(i == 0 || arr[i] != arr[i-1]) {
arr1.push_back(arr[i]); // 将非重复元素添加到arr1末尾
}
}

// 使用范围for循环输出去重后的结果
// 遍历arr1中的每个元素,自动类型推导为int
for(int num : arr1) {
cout << num << " "; // 输出当前元素值,后面加空格分隔
}

return 0; // 程序正常结束,返回0表示成功
}

vector的动态数组

矩阵反转
/*
输入:
2 3
1 2 3
4 5 6
输出
1 4
2 5
3 6*/


点击展开知识点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<vector>
using namespace std;
int main(){
int x , y;
cin >> x >> y;
vector<vector<int>> a(x, vector<int> (y, 0));//一个名为a的动态数组
for(int i =0; i < x; i++ ){
for(int j = 0 ; j < y;j++){
cin >> a[i][j] ;
}
}
for(int j =0; j < y; j++ ){
for(int i =0; i < x; i++ ){
cout << a[i][j] << " ";
}
cout << endl;
}

return 0;
}

数据转字符串转数字

输入一个正整数n,统计2到n之间,个位数字为3的数字有几个。比如:n = 30,2到30之间含有3的有3个:3, 13, 23。

输入格式:
输入一个正整数n(2 ≤ n ≤ 1000)

输出格式:
输出为一个正整数,表示2到n之间个位数字为3的数字有几个。
eg:30 => 3 (3,13,23)


点击展开知识点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
int main(){
ll n;
cin>>n;
string chuan=to_string(n);
ll zhuan[chuan.length()];
ll geshu=chuan.length();

for(int i = 0; i < chuan.length(); i++) {
zhuan[i] = chuan[i] - '0';
}
ll s=0;
switch (geshu)
{
case 1:
if((n-1)/3==1) s=1;
break;
case 2:
if((zhuan[1]-1)/3==1) s+=1;
s+=zhuan[0];
break;
case 3:
if((zhuan[2]-1)/3==1) s+=1;
s+=zhuan[1];
s+=zhuan[0]*10;
break;
case 4:
s=100;
break;
}


cout <<s;
}


C++ 字符串操作常见错误与修正

写代码的时候错误用了<<将变量导入字符串。


点击展开知识点
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//错误代码
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string chuan;
cin>>n;
cout<<"1";
for(int i=2;i<n;i++){
if(n%i==0)chuan<<i<<" ";
}
if(chuan.empty())cout <<"none.";
else {
chuan.pop_back();
cout<<chuan;
}
return 0;

主要问题分析

字符串拼接错误

错误代码:

1
chuan << i << " ";  // 错误:<< 不能用于字符串拼接

正确方法:

  • 使用 += 运算符
  • 使用 append() 方法
  • 使用 to_string() 转换数字

逻辑错误

  • 不应在循环前输出固定值 “1”
  • 应正确处理边界条件

变量初始化问题

错误代码:

1
string chuan;  // 未初始化

正确方法:

1
string chuan = "";  // 显式初始化

字符串操作方法说明

字符串拼接

1
2
3
4
5
6
7
8
9
10
// 方法1:使用 +=
chuan += to_string(i) + " ";

// 方法2:使用 append()
chuan.append(to_string(i)).append(" ");

// 方法3:使用 stringstream
stringstream ss;
ss << i << " ";
chuan += ss.str();

删除最后一个字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 方法1:pop_back()(推荐)
if(!chuan.empty()) {
chuan.pop_back();
}

// 方法2:erase()
if(!chuan.empty()) {
chuan.erase(chuan.length() - 1);
}

// 方法3:substr()
if(!chuan.length() > 0) {
chuan = chuan.substr(0, chuan.length() - 1);
}

修正后的完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

string chuan = ""; // 修正:显式初始化字符串
bool hasFactors = false; // 新增:标记是否有因数

// 修正:移除错误的 cout<<"1"
for(int i = 2; i < n; i++) {
if(n % i == 0) {
// 修正:使用正确的字符串拼接方法
chuan += to_string(i) + " ";
hasFactors = true;
}
}

if(!hasFactors) {
cout << "none.";
} else {
// 修正:安全地删除最后一个空格
if(!chuan.empty()) {
chuan.pop_back();
}
cout << chuan;
}

return 0;
}

替代方案(使用vector,更推荐):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

vector<int> factors;

for(int i = 2; i < n; i++) {
if(n % i == 0) {
factors.push_back(i);
}
}

if(factors.empty()) {
cout << "none.";
} else {
for(int i = 0; i < factors.size(); i++) {
if(i > 0) cout << " ";
cout << factors[i];
}
}

return 0;
}