正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,现给定a和b,如何生成Q中的前几项
2023-11-27 07:21:36
思路分析:可以与归并排序联系起来,给定两个变量A、B,变量A轮着存放:a*1,a*2,a*3,……变量组B轮着存放:b*1,b*2,b*3,……有两个整数i、j,分别代表A、B第i、j次存放的值,每次取A、B中的较小值,并将较小值的次数加一,然后继续比较。
代码如下:
#include "stdafx.h"
#include<stdio.h>
void Generate(int a, int b, int N, int *Q)
{
int tmpA, tmpB;
int i = ;
int j = ;
for (int k = ; k < N; k++)
{
tmpA = a*i;
tmpB = b*j;
if (tmpA <= tmpB)
{
Q[k] = tmpA;
i++;
}
else
{
Q[k] = tmpB;
j++;
}
}
}
int main()
{
int a[];
int i;
Generate(, , , a);
for (i = ; i < sizeof(a) / sizeof(a[]); i++)
printf("%d ", a[i]);
printf("\n");
getchar();
return ;
}
效果如图:
最新文章
- yii2中自定义验证规则rules
- [LeetCode] Letter Combinations of a Phone Number
- 12-8 php基础
- 如何利用tomcat和cas实现单点登录(2):配置cas数据库验证和cas客户端配置
- SDUT 3344 数据结构实验之二叉树五:层序遍历
- project 2010 使用技巧
- Android编程: Activity生命周期和LogCat使用
- OSG 安装配置
- 【技术宅4】如何把M个苹果平均分给N个小朋友
- Ueditor开发经验
- mysql在linux上重启
- bestcoder Round#52 1001(最短路+状压dp)
- Openstack(企业私有云)万里长征第一步——安装
- Spark UI界面原理
- OC实现带弹跳动画按钮的界面控制器view
- 0412ooday01.txt=============对象和类(上)
- 牛牛与数组 (简单dp)
- error 2593 operator <;<; 不明确的可能的解决方法
- Android应用源码 概览
- Houdini技术体系 基础管线(四) :Houdini驱动的UE4植被系统 上篇
热门文章
- [YARN] 2.2 GB of 2.1 GB virtual memory used. Killing container.
- .Net Core邮件发送之MailKit
- Android定位&;地图&;导航——基于百度地图实现的定位功能
- python2中在sqlite3中插入中文
- 每天一个linux命令(3):ls命令
- DataGuard---->;物理StandBy的角色切换之switchover
- eclipse spring 配置文件xml校验时,xsd报错
- pycharm如何解决新建的文件没有后缀的问题
- CAS Maven
- xcode 报错 malloc: *** error for object 0x6c3c5a4: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug------d