L1-080 乘法口诀数列

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;
}
/*

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值