取得一个数组的某一位数
点击展开代码(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> #include<vector> using namespace std;
vector<int> memo;
int fib(int n) { if (n <= 0) { cout << "请输入正整数" << endl; return -1; } if (n == 1) return 0; if (n == 2) return 1; if (memo[n] != -1) return memo[n]; memo[n] = fib(n-1) + fib(n-2); return memo[n]; }
int main() { int n; cout << "请输入要计算的斐波那契数列项数: "; cin >> n; memo.resize(n+1, -1); cout << "斐波那契数列第" << n << "项是: " << fib(n) << endl; return 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 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()); 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> using namespace std;
int main() { int n; cin >> n; vector<int> arr(n); for(int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); vector<int> arr1; for(int i = 0; i < n; i++) { if(i == 0 || arr[i] != arr[i-1]) { arr1.push_back(arr[i]); } } for(int num : arr1) { cout << num << " "; } return 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)); 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;
|
主要问题分析
字符串拼接错误
错误代码:
正确方法:
- 使用
+= 运算符
- 使用
append() 方法
- 使用
to_string() 转换数字
逻辑错误
- 不应在循环前输出固定值 “1”
- 应正确处理边界条件
变量初始化问题
错误代码:
正确方法:
字符串操作方法说明
字符串拼接
1 2 3 4 5 6 7 8 9 10
| chuan += to_string(i) + " ";
chuan.append(to_string(i)).append(" ");
stringstream ss; ss << i << " "; chuan += ss.str();
|
删除最后一个字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| if(!chuan.empty()) { chuan.pop_back(); }
if(!chuan.empty()) { chuan.erase(chuan.length() - 1); }
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; 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; }
|
Author:
Lyxy-szs
Permalink:
http://lyxy-szs.github.io/2025/09/29/C++¬e2/
Slogan:
永远相信美好的事情即将發生?