Run ID:102508

提交时间:2024-12-21 18:58:51

#include <iostream> using namespace std; // 辗转相除法求最大公约数 int gcd(int a, int b) { while (b!= 0) { int temp = b; b = a % b; a = temp; } return a; } // 求两个数的最小公倍数 int lcm(int a, int b) { return a * b / gcd(a, b); } int main() { int cycle = lcm(20, 30); // 计算报数周期,即20和30的最小公倍数 int full_cycles = 1000 / cycle; // 完整的周期数 int remaining = 1000 % cycle; // 剩余的数的个数 int same_count_in_cycle = 0; for (int i = 1; i <= cycle; ++i) { int num_a = (i - 1) % 20 + 1; int num_b = (i - 1) % 30 + 1; if (num_a == num_b) { same_count_in_cycle++; } } int total_same_count = full_cycles * same_count_in_cycle; // 处理剩余的数中相同的个数 for (int i = 1; i <= remaining; ++i) { int num_a = (i - 1) % 20 + 1; int num_b = (i - 1) % 30 + 1; if (num_a == num_b) { total_same_count++; } } cout << total_same_count << endl; return 0; }