本题要求你从任意给定的两个 1 位数字 a1 和 a2 开始,用乘法口诀生成一个数列 {an},规则为从 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 后结束。
思路:两数相乘最大9*9=81,所以只需考虑大于九和小于等于九两种情况。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,k;
cin>>n>>m>>k;
int a[1008];
int mant=2;
int index1=2,index2=0;//index1用来记每次m的取数 index2用来计算个数
a[index2++]=n;
a[index2++]=m;
while(1){
int t=n*m;
if(t<10){
a[index2++]=t;
mant++;
}else{
a[index2++]=t/10;
a[index2++]=t%10;
mant+=2;
}
n=m;
m=a[index1++];
if(mant>=k){//由于存在t+=2这个所以当mant>k时也退出循环
break;
}
}
for(int i=0;i<k;i++){
cout<<a[i];
if(i!=k-1)cout<<" ";
}
return 0;
}