循环结构程序(问题详解).docVIP

  • 3
  • 0
  • 约7.61千字
  • 约 8页
  • 2019-02-01 发布于安徽
  • 举报
实用标准文案 精彩文档 循环结构程序 1.利用随机函数RAND和函数floor,产生30个1到20之间的随机整数,使用WHILE语句显示这30个随机数。 declare @i int,@x int select @i=1 while @i=30 begin set @x=FLOOR(RAND()*(20-1+1)+1) set @i=@i+1 print @x end 2.设纸的厚度为0.5毫米,将纸对折,再对折…,求至少对折多少次,纸张的厚度能达到珠穆朗玛峰的高度8848米。 方法一: Declare @h decimal(10,1),@n int select @h=0.5,@n=0 while @h8844000 --运算部分 begin set @h=@h*2 set @n=@n+1 end select 对折,@n,次后可以超过珠峰的高度‘ -输出部分 方法二: Declare @h decimal(10,1),@n int select @h=0.5,@n=0 while @h8844000 --运算部分 begin set @n=@n+1 set @h=0.5*power(2,@n) end --select 对折,@n,次后可以超过珠峰的高度 print 对折+ltrim(str(@n))+次后可以超过珠峰的高度 3.求自然数1~100之间奇数之和 declare @i int,@s bigint select @i=1,@s=0 while @i=100 begin set @s=@s+@i set @i=@i+2 end select @s=,@s 4.编写程序,输出由1、2、3、4、5、6这六个数字组成的所有可能的两位数,并统计它们的个数。 declare @x int,@i int,@j int,@n int set @x=123456 set @i=1 set @n=0 while @i=6 begin set @j=1 while @j=6 begin print str(@i,1)+str(@j,1) set @n=@n+1 set @j=@j+1 end set @i=@i+1 end print @n=+str(@n,3) 5.编写程序,输入一个数,判定该数是否为素数,如果是素数,则输出“该数是素数”的信息,否则输出“该数不是素数”的信息。 方法一:用循环变量等于要判断的数减一的方法来确定。 declare @x int,@i int set @i=2 set @x=6 while @i=@x-1 begin if @x%@i=0 break else set @i=@i+1 end if @i=@x print str(@x)+‘是素数 else print str(@x)+‘不是素数 方法二:使用做记号的方法。 declare @x int,@i int,@jh bit set @jh=0 --使用变量做记号 set @i=2 set @x=5 while @i=@x-1 begin if @x%@i=0 begin set @jh=1 break end else set @i=@i+1 end if @jh=0 print str(@x)+‘是素数 else print str(@x)+‘不是素数 6.编程计算从1到30之间的所有7的倍数之和 declare @i int,@s bigint select @i=1,@s=0 while @i=30 begin if @i%7=0 set @s=@s+@i

文档评论(0)

1亿VIP精品文档

相关文档