Group Programming Ladder Competition Exercise Set L1-080 Multiplication Table Sequence

This question requires you to start from any given two 1-digit numbers a1​ and a2​, and use the multiplication formula to generate a sequence {an​}. The rule is to proceed sequentially from a1​, each time adding the current number to the next number. Multiply and stick the result at the end of the sequence. If the result is not a 1-digit number, each of its digits should be an item in the sequence.

Input format:

The input gives 3 integers in one line, namely a1​, a2​ and n, satisfying 0≤a1​, a2​≤9, 0<n≤103.

Output format:

Print the first n terms of the sequence in one line. Numbers should be separated by 1 space, and there should be no extra spaces at the beginning or end of the line.

Input example:

2 3 10

Output sample:

2 3 6 1 8 6 8 4 8 4

Sample explanation:

The first two terms in the sequence are 2 and 3. Starting with 2, since 2×3=6, the 3rd term is 6. Because 3×6=18, the 4th and 5th terms are 1 and 8 respectively. And so on... Finally, because the 6th item has 6×8=48, the corresponding items 10 and 11 should be 4 and 8. And because only the first 10 items are required to be output, it ends after outputting 4.

Code length limit

16 KB

time limit

400 ms

memory limit

64 MB

Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    int n,s[n];
    cin>>s[0]>>s[1]>>n;
    int flag=2;//需要记录的元素对应下标,从下标2开始
    int index=0;//从第一个数字开始遍历数组
    int temp,i;//临时变量
    while(flag<n){
        temp=s[index]*s[index+1];//取前两位数字乘积赋给temp
        if(temp<10) s[flag++]=temp;//如果乘积小于10 直接给s数组
        else{
            s[flag++]=temp/10;//乘积超过10 则先在s数组中存放十位数字
            s[flag++]=temp%10;//再在s数组中存放个位数字
        }
        index++;//继续下个数字遍历
    }
    for(i=0;i<n-1;i++) cout<<s[i]<<" ";//这里i<n-1是因为行尾不能有多余空格
    cout<<s[n-1];
    return 0;
}

Guess you like

Origin blog.csdn.net/gaogao0305/article/details/130353999