乘法口诀数列
输出格式:
在一行中输出数列的前 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 后结束。
// 数据结构13.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int first;//第一个数
int second;//第二个数
int num;//项数
int show1;//两数乘积所得的数
cin >> first >> second >> num;
cout << first << " " << second << " ";
int show[1001];//存储答案
show[0] = first;
show[1] = second;
int ah = 2;//用来计数
for (int i = 2;i<num;i++ )
{
show1 = first * second;
if (show1 > 9)
{
show[ah] = show1 / 10;
show[ah + 1] = show1 % 10;
first = show[i - 1];
second = show[i];
if(ah==num-1)
{cout<<show[ah];
break;}
else if(ah<num-2&&ah>=2)
{cout<<show[ah]<<" "<<show[ah+1]<<" ";}
else if(ah==num-2)
{cout<<show[ah]<<" "<<show[ah+1];
break;}
ah = ah + 2;
}
else if (show1 <= 9&&show1>=0)
{
show[ah] = show1;
first = show[i-1];
second = show[i];
if (ah < num - 1&&ah>=2)
{
cout << show[ah] << " ";
}
else if(ah==num-1)
{cout<<show[ah];
break;}
ah++;
}
}
}