随机产生n个(20≤n≤100)两位整数,找出其中的奇数、偶数和素数,并将奇数按升序排序,将偶数按降序排序,将素数求和。求代码和流程图
function isprime(x as integer)
if x = 2 then return false
for i = 2 to x - 1
if x mod 2 = 0 then return false
next
return true
end function
dim r as new random()
dim list as new list(of integer)
dim i as integer
for i = 1 to n
list.add(r.nextint(20, 100))
next
'奇数
for each x in list.where(function(x) x mod 2 = 1).orderby(function(x) x)
console.writeline(x)
next
'偶数
for each x in list.where(function(x) x mod 2 = 0).orderbydescending(function(x) x)
console.writeline(x)
next
'
素数求和
dim sum as integer = list.where(function(x) isprime(x)).sum()
console.writeline(sum)