2021团体程序设计天梯赛 L1-8 乘法口诀数列
思路:
简单递推
Tip:
注意结果为0的情况
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; int ans[maxn]; int main() { int a, b, n; cin >> a >> b >> n; ans[1] = a; ans[2] = b; int lastt = 3; for (int i = 1; i <= n; i++) { if (lastt > n) break; int c = ans[i] * ans[i + 1]; if (c == 0) { ans[lastt++] = 0; continue; } stack<int> s; while (c) { s.push(c % 10); c /= 10; } while (!s.empty()) { ans[lastt++] = s.top(); s.pop(); } } bool first = true; for (int i = 1; i <= n; i++) { if (first) { first = false; cout << ans[i]; } else cout << " " << ans[i]; } return 0; }