实现将一个正整数随机拆分为三个正整数-- 【叶子】

本文介绍了一种使用SQL将一个正整数随机拆分成三个正整数的方法,并提供了具体的SQL代码实现。通过该方法可以观察到每次运行时,原始整数被随机地分配为三个不同的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、sql中实现将一个正整数随机拆分为三个正整数

1sql代码

declare @i int

set @i =100;--要拆分的正整数

declare @j int

set @j =round(rand()*@i+1.0,0)

declare @k int

select @k=round(rand()*(@i-@j)+1.0,0)

declare @table table(id int)

if(@j>0 and @k>0 and @i-@j-@k>0)

begin

insert into @table

select @j union all

select @k union all

select @i-@j-@k

select * from @table order by newid();

end

go 100

2、运行结果

/* 运行结果(部分)

id

-----------

77

6

17

(3 row(s) affected)

id

-----------

7

31

62

(3 row(s) affected)

id

-----------

29

66

5

(3 row(s) affected)

id

-----------

4

67

29

(3 row(s) affected)

id

-----------

37

32

31

(3 row(s) affected)

id

-----------

39

39

22

(3 row(s) affected)

id

-----------

64

22

14

(3 row(s) affected)

id

-----------

9

13

78

(3 row(s) affected)

id

-----------

10

62

28

(3 row(s) affected)

id

-----------

33

10

57

(3 row(s) affected)

*/

At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information. 在大学的入口处,有一个巨大的矩形广告牌,尺寸为h*w (h为其高度,w为其宽度)。公告板是张贴所有可能的公告的地方:最近的编程比赛,餐厅菜单的变化以及其他重要信息。 On September 1, the billboard was empty. One by one, the announcements started being put on the billboard. 9月1日,广告牌空无一人。公告开始一个一个地贴在广告牌上。 Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi. 每个公告都是单位高度的条纹纸。更具体地说,第i个公告是一个大小为1 * wi的矩形。 When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one. 当有人在广告牌上张贴新的公告时,她总是选择最可能的位置来张贴公告。在所有可能的最上面的位置中,她总是选择最左边的位置。 If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university). 如果一个新的公告没有有效的位置,它就不会被放在广告牌上(这就是为什么一些编程比赛没有来自这所大学的参与者)。 Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed. 给定广告牌和公告的大小,您的任务是找到放置公告的行数。 Input 输入 There are multiple cases (no more than 40 cases). 有多例(不超过40例)。 The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements. 输入文件的第一行包含三个整数,h,w和n (1 <= h,w <= 10^9;1 <= n <= 200,000) -广告牌的尺寸和公告的数量。 Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement. 接下来的n行每一行都包含一个整数wi (1 <= wi <= 10^9)——第i条公告的宽度。 Output 输出 For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement. 对于每个公告(按照输入文件中给出的顺序),输出一个数字——该公告所在行的编号。行从1到h编号,从第一行开始。如果一个公告不能放在广告牌上,则为该公告输出“-1”。 Sample Input 样例输入 3 5 5 2 4 3 3 3 Sample Output 样例输出 1 2 1 3 -1 使用二分算法并且不能超时
最新发布
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值