L1-080 乘法口诀数列
本题要求你从任意给定的两个 1 位数字 a1 和 a2 开始,用乘法口诀生成一个数列 {a**n},规则为从 a1 开始顺次进行,每次将当前数字与后面一个数字相乘,将结果贴在数列末尾。如果结果不是 1 位数,则其每一位都应成为数列的一项。
输入格式:
输入在一行中给出 3 个整数,依次为 a1、a2 和 n,满足 0≤a1,a2≤9,0<n≤103。
输出格式:
在一行中输出数列的前 n 项。数字间以 1 个空格分隔,行首尾不得有多余空格。
输入样例:
2 3 10
输出样例:
2 3 6 1 8 6 8 4 8 4
样例解释:
数列前 2 项为 2 和 3。从 2 开始,因为 2×3=6,所以第 3 项是 6。因为 3×6=18,所以第 4、5 项分别是 1、8。依次类推…… 最后因为第 6 项有 6×8=48,对应第 10、11 项应该是 4、8。而因为只要求输出前 10 项,所以在输出 4 后结束。
C++程序:
vector解法
#include <map>
#include <set>
#include <regex>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { {1,0} ,{-1,0},{0,1},{0,-1} };
int main(){
vector<int> ans;
int a1, a2, n;
cin >> a1 >> a2 >> n;
ans.push_back(a1);
ans.push_back(a2);
int pos = 0;//记录当前下标
while (ans.size() < n) {
int t = ans[pos++] * ans[pos];
//小于10直接放入,大于10拆分
if (t < 10) {
ans.push_back(t);
}
else {
int second = t % 10;
t /= 10;
int first = t % 10;
ans.push_back(first);
ans.push_back(second);
}
}
for (int i = 0; i < n; i++) {
if (i != 0) {
cout << " ";
}
cout << ans[i];
}
return 0;
}
/*
*/
string解法
#include <map>
#include <set>
#include <regex>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { {1,0} ,{-1,0},{0,1},{0,-1} };
int main(){
string ans;
int a1, a2, n;
cin >> a1 >> a2 >> n;
ans += to_string(a1);
ans += to_string(a2);
int pos = 0;//记录当前下标
while (ans.size() < n) {
int t = (ans[pos++] - '0') * (ans[pos] - '0');
ans += to_string(t);
}
for (int i = 0; i < n; i++) {
if (i != 0) {
cout << " ";
}
cout << ans[i];
}
return 0;
}
/*
*/