给定非负整数序列的前两项 a1,a2 ,请按如下规则产生符合要求整数序列:若当前序列的最后 2 项的乘积小于 10000 以内的数,则把乘积直接作为后继项;若当前序列的最后两项之积大于等于 10000 ,则把该数字分别对 10000 求余和求商(商向下取整),把余和商依次作为该序列新插入的两项。请输出所产生的前 10 项。用c++完成
时间: 2023-06-09 17:04:54 浏览: 69
C++版本的最大覆盖问题的算法设计
3星 · 编辑精心推荐
#include <stdio.h>
int main()
{
int a1, a2;
printf("请输入前两项:");
scanf("%d%d", &a1, &a2);
printf("%d %d ", a1, a2);
int product = a1 * a2;
while (product >= 10000)
{
int quotient = product / 10000;
int remainder = product % 10000;
printf("%d %d ", quotient, remainder);
a1 = a2;
a2 = quotient;
product = a1 * a2;
}
printf("%d", product);
for (int i = 4; i < 11; i++)
{
int temp = a2;
a2 = product;
product = temp * a2;
if (product >= 10000)
{
int quotient = product / 10000;
int remainder = product % 10000;
printf(" %d %d", quotient, remainder);
a1 = a2;
a2 = quotient;
product = a1 * a2;
}
else
{
printf(" %d", product);
a1 = a2;
a2 = product;
}
}
return 0;
}
阅读全文