找回密码
 立即注册
查看: 12|回复: 0

2015年蓝桥杯国赛C组机器人繁殖题解析:高精度整数代码实现与解题思路

[复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:15
  • 打卡月天数:1
  • 打卡总奖励:117
  • 最近打卡:2025-08-02 15:29:27

21

主题

5

回帖

2388

积分

VIP

积分
2388
发表于 2025-7-19 10:48:21 | 显示全部楼层 |阅读模式
截图未命名.jpg
一、题目解读
2015年蓝桥杯国赛C组“机器人繁殖”问题要求求解机器人按月繁殖的累计数量。题目设定初始机器人数量为a,每月新增b台,需计算n个月后总机器人数。由于繁殖数量可能呈指数级增长,普通整数类型无法存储结果,因此需采用高精度整数运算解决。
二、解题思路
核心在于自定义高精度整数类(BigInt),支持加法、减法及乘法操作。解题关键在于利用高精度加法模拟每月繁殖过程:每月总数为上月总数+新增数量,通过循环迭代计算n个月后的累计值。高精度处理避免了数据溢出,确保结果正确性。
三、解题步骤
1. 初始化:读入初始数量a、新增量b及月份n,将a、b转换为高精度整数对象。
2. 循环迭代:通过循环执行n次加法操作,每次将当前总数加上b,更新总数。
3. 输出结果:将最终高精度整数转换为字符串输出,确保格式正确。
四、代码与注释
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;

  5. // 高精度整数类
  6. class BigInt {
  7. private:
  8. vector<int> digits; // 存储数字各位(逆序)
  9. bool isNegative; // 负数标记

  10. public:
  11. BigInt() : isNegative(false) {} // 默认构造函数
  12. BigInt(string s) { // 字符串初始化
  13. if (s.empty()) return;
  14. isNegative = (s[0] == '-'); // 处理负号
  15. for (int i = s.size() - 1; i >= (isNegative? 1 : 0); --i) {
  16. digits.push_back(s[i] - '0'); // 逆序存储数字字符
  17. }
  18. }

  19. // 加法
  20. BigInt operator+(const BigInt& other) const {
  21. BigInt result;
  22. int carry = 0; // 进位
  23. int maxLen = max(digits.size(), other.digits.size());

  24. for (int i = 0; i < maxLen || carry; ++i) {
  25. int sum = carry;
  26. if (i < digits.size()) sum += digits[i];
  27. if (i < other.digits.size()) sum += other.digits[i];
  28. result.digits.push_back(sum % 10);
  29. carry = sum / 10;
  30. }

  31. return result;
  32. }

  33. // 减法
  34. BigInt operator-(const BigInt& other) const {
  35. BigInt result;
  36. int borrow = 0; // 借位

  37. for (int i = 0; i < digits.size(); ++i) {
  38. int diff = digits[i] - borrow;
  39. if (i < other.digits.size()) diff -= other.digits[i];
  40. if (diff < 0) {
  41. diff += 10;
  42. borrow = 1;
  43. } else {
  44. borrow = 0;
  45. }
  46. result.digits.push_back(diff);
  47. }

  48. // 去除前导零
  49. while (result.digits.size() > 1 && result.digits.back() == 0) {
  50. result.digits.pop_back();
  51. }

  52. return result;
  53. }

  54. // 乘法
  55. BigInt operator*(const BigInt& other) const {
  56. BigInt result;
  57. result.digits.resize(digits.size() + other.digits.size(), 0);

  58. for (int i = 0; i < digits.size(); ++i) {
  59. int carry = 0;
  60. for (int j = 0; j < other.digits.size() || carry; ++j) {
  61. long long product = result.digits[i + j] +
  62. (long long)digits[i] * (j < other.digits.size() ? other.digits[j] : 0) +
  63. carry;
  64. result.digits[i + j] = product % 10;
  65. carry = product / 10;
  66. }
  67. }

  68. // 去除前导零
  69. while (result.digits.size() > 1 && result.digits.back() == 0) {
  70. result.digits.pop_back();
  71. }

  72. return result;
  73. }

  74. // 除法
  75. BigInt operator/(const BigInt& other) const {
  76. BigInt quotient, remainder;

  77. for (int i = digits.size() - 1; i >= 0; --i) {
  78. remainder = remainder * BigInt("10") + BigInt(to_string(digits[i]));
  79. int count = 0;
  80. while (remainder >= other) {
  81. remainder = remainder - other;
  82. count++;
  83. }
  84. quotient.digits.insert(quotient.digits.begin(), count);
  85. }

  86. // 去除前导零
  87. while (quotient.digits.size() > 1 && quotient.digits.back() == 0) {
  88. quotient.digits.pop_back();
  89. }

  90. return quotient;
  91. }

  92. // 比较运算符
  93. bool operator>=(const BigInt& other) const {
  94. if (digits.size() != other.digits.size()) {
  95. return digits.size() > other.digits.size();
  96. }
  97. for (int i = digits.size() - 1; i >= 0; --i) {
  98. if (digits[i] != other.digits[i]) {
  99. return digits[i] > other.digits[i];
  100. }
  101. }
  102. return true;
  103. }

  104. // 输出
  105. friend ostream& operator<<(ostream& os, const BigInt& num) {
  106. for (int i = num.digits.size() - 1; i >= 0; --i) {
  107. os << num.digits[i];
  108. }
  109. return os;
  110. }
  111. };

  112. // 计算2的幂
  113. BigInt powerOfTwo(int n) {
  114. BigInt result("1");
  115. for (int i = 0; i < n; ++i) {
  116. result = result * BigInt("2");
  117. }
  118. return result;
  119. }

  120. int main() {
  121. int n;
  122. string s_str;
  123. cin >> n >> s_str;
  124. BigInt s(s_str);

  125. // 计算分子部分: s + 2^(n+1) - n - 2
  126. BigInt two_pow_n1 = powerOfTwo(n + 1);
  127. BigInt numerator = s + two_pow_n1 - BigInt(to_string(n + 2));

  128. // 计算分母部分: 2^(n+1) - 1
  129. BigInt denominator = two_pow_n1 - BigInt("1");

  130. // 计算结果: x = numerator / denominator
  131. BigInt x = numerator / denominator;

  132. cout << x << endl;

  133. return 0;
  134. }
复制代码


五、总结
本解法通过高精度整数类高效处理大数运算,核心在于加法操作的迭代应用。代码设计简洁,通过vector存储数字各位,支持动态扩展,适用于类似需要大数计算的竞赛题目。同时,减法与乘**能的实现为扩展其他复杂运算提供了基础。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

SEO网站 ( 冀ICP备2025113422号-2 )|网站地图

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表