- 握手问题
#include <bits/stdc++.h>
using namespace std;/*** 计算组合数公式 C(n,2)*/
int combination(int n) {return n * (n - 1) / 2;
}int main() {int a = 50, b = 7;cout << (a*(a-1)/2 - b*(b-1)/2) << endl;return 0;
}
知识点:
- 组合数学公式 C(n,2) = n*(n-1)/2
- 集合的补集
解题思路:
总握手次数 = 所有人两两握手次数 - 特殊群体内部未发生的握手次数
计算步骤:
- 计算50人总握手次数:C(50,2) = 1225
- 计算7人群体未握手次数:C(7,2) = 21
- 最终结果:1225 - 21 = 1204
2.小球反弹
#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a, int b){return b == 0 ? a : gcd(b,a%b);
}
int main(){int x = 343720;int y = 233333;int dx = 15;int dy = 17;int p = y * dx;int q = x * dy;int g = gcd(p,q);p /= g;q /= g;int t = 2*x*p / dx;double s = 1.0 * sqrt(dx*dx+dy*dy)*t;printf("%.2f",s);return 0;
}
高中的向量分解,代数式代出距离公式直接出结果
- 好数
#include <bits/stdc++.h>
using namespace std;bool check(int n) {int p = 1;while(n) {int d = n%10;if(p%2 != d%2) return false;n /= 10;p++;}return true;
}int main() {int n, cnt=0;cin >> n;for(int i=1; i<=n; i++) if(check(i)) cnt++;cout << cnt;return 0;
}
知识点:
- 数位分解操作
- 奇偶判断
- 循环遍历
解题思路:
- 从个位开始逐位检查(奇数位从1开始计数)
- 判断规则:
- 奇数位(第1、3、5...位):数字必须为奇数
- 偶数位(第2、4、6...位):数字必须为偶数
- 遍历1到N的所有数字进行验证
4.R格式
#include<bits/stdc++.h>
using namespace std;int main() {string s;int cnt = 0;vector<int> a(11000);int d = 0;int n;cin >> n >> s;// 提取数字并记录小数点位置for (int i = s.size() - 1; i >= 0; i--) {if (s[i]!= '.') {a[cnt++] = s[i] - '0';}else {d = cnt;}}// 模拟乘以2^n的过程for (int i = 1; i <= n; i++) {for (int j = 0; j < cnt; j++) {a[j] *= 2;}for (int j = 0; j < cnt; j++) {if (a[j] >= 10) {a[j + 1] += a[j] / 10;a[j] %= 10;}}if (a[cnt]) {cnt++;}}// 四舍五入处理if (a[d - 1] >= 5) {a[d]++;for (int i = d; i < cnt; i++) {if (a[i] >= 10) {a[i + 1] += a[i] / 10;a[i] %= 10;}}if (a[cnt]) {cnt++;}}// 输出结果for (int i = cnt - 1; i >= d; i--) {cout << a[i];}return 0;
}
实现步骤:
-
输入处理:
- 直接读取原始字符串
- 逆向存储数字到数组(个位在a[0])
- 记录小数点位置d
-
乘法过程:
for 循环n次:
每位数字×2
从低位到高位处理进位 -
四舍五入:
- 根据原小数位d判断舍入位
- 从舍入位向前进位
-
结果输出:
- 从最高位到小数点位置逆序输出
知识点:
- 高精度字符串运算
- 小数点位置管理
- 四舍五入算法
- 字符串操作技巧