Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
121888 | 朱铭轩 | 22第n项的数 | C++ | Accepted | 1 MS | 284 KB | 1814 | 2025-06-08 20:12:30 |
#include <iostream> #include <string> using namespace std; // 字符串表示的大数与整数相乘的函数 string multiply(string num, int k) { if (k == 0) { return "0"; } if (k == 1) { return num; } string res = ""; int carry = 0; // 从最低位到最高位遍历字符串 for (int i = num.size() - 1; i >= 0; i--) { int digit = num[i] - '0'; int product = digit * k + carry; carry = product / 10; int res_digit = product % 10; res = char('0' + res_digit) + res; // 将结果数字添加到字符串前面 } // 处理剩余的进位 while (carry > 0) { res = char('0' + carry % 10) + res; carry /= 10; } return res; } int main() { int n; cin >> n; int a, b, c, d; cin >> a >> b >> c >> d; // 检查是否为等差数列:相邻项的差相等 if ((b - a == c - b) && (c - b == d - c)) { int diff = b - a; // 公差 long long first = a; // 首项 long long nth_term = first + static_cast<long long>(n - 1) * diff; cout << nth_term << endl; } // 检查是否为等比数列:相邻项的积满足 b*b == a*c 且 c*c == b*d else if ((b * b == a * c) && (c * c == b * d)) { int r = b / a; // 公比(整数除法,由题目保证为整数) string term = to_string(a); // 首项转为字符串 // 从第1项开始,乘以公比 (n-1) 次得到第n项 for (int i = 1; i < n; i++) { term = multiply(term, r); } cout << term << endl; } // 题目保证数列为等差或等比,此分支理论上不会执行 else { cout << "0" << endl; // 安全处理 } return 0; }