求一fortran程序,取出n个连续自然数的所有n级排列.
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/10 22:36:55
求一fortran程序,取出n个连续自然数的所有n级排列.
例如 n=4的:要存到文件里面的是所有1234的排列:1234,1243,1324,1342,····4321 这样共n!=24个排列的数据.
要求n可以取任意正整数.
例如 n=4的:要存到文件里面的是所有1234的排列:1234,1243,1324,1342,····4321 这样共n!=24个排列的数据.
要求n可以取任意正整数.
program main
character(20) arr(20000),str
Integer n,ss(20),k,m
n = 4
k = 0
m = JC(n)
Do While (k < m)
str = ""
Call GetNum(str, n) !获得随机数
!判断随机数是否已经出现过
ii = 1
do i = 1 , k
If (trim(arr(i))==trim(str)) Then
ii = 0
cycle
End If
enddo
!如果随机数未出现过则储存在arr中
If (ii == 1) Then
k = k + 1
arr(k) = str
!输出结果 也可输出到文件中
write(*,*) trim(str)
End If
end do
End program
Subroutine GetNum(str, n)
!获得一个n位随机数,该随机数内各位数字都不重复
character(20) str
character(1) s1
Integer ss(20),n,a,b
real x
str = ""
ss=0
i = 0
Do While (Len(trim(str)) < n)
call random(x)
a = Int(x * n + 1)
b = 1
do j = 1, i
If (a == ss(j)) Then
b = 0
cycle
End If
end do
If (b == 1) Then
i = i + 1
ss(i) = a
write(s1,'(i1)') a
str = trim(str) // s1
End If
end do
End Subroutine
integer Function JC(n)
!计算n的阶乘
integer n
JC = 1
do i = 1 , n
JC = JC * i
end do
End Function
我的源代码是vba的,结果可以直接输出在excel中,花了好久改成fortran.vba的也一起给你吧:
Private Sub nj()
Dim arr(10000) As String, n As Integer, str As String, ss(10) As Integer
Sheet1.Cells.Clear
n = 5
k = 0
m = JC(n)
Do While k < m
str = ""
Call GetNum(str, n)
ii = 1
For i = 1 To k
If arr(i) = str Then
ii = 0
Exit For
End If
Next
If ii = 1 Then
k = k + 1
arr(k) = str
Cells(k, 1) = str
End If
DoEvents
Loop
MsgBox ""
End Sub
Private Sub GetNum(str, n)
Dim ss() As Integer
str = ""
ReDim ss(n)
i = 0
Do While Len(str) < n
a = Int(Rnd * n + 1)
b = 1
For j = 1 To i
If a = ss(j) Then
b = 0
Exit For
End If
Next
If b = 1 Then
i = i + 1
ss(i) = a
str = str & ss(i)
'MsgBox ss(i) & "," & str
End If
DoEvents
Loop
End Sub
Private Function JC(n)
JC = 1
For i = 1 To n
JC = JC * i
Next
End Function
再问: 暂时没时间细看,但是 ··· 随机数? 我也想过用随机方法把所有不重复的都试出来? 运算量是不大了点?
再答: 速度很快的,你运行一下试试就知道。
下面有一个人分享的算法没用随机数,但计算量貌似惊人了。参考一下吧(里面也有我的回答):
http://zhidao.baidu.com/question/872680146775499292.html?oldq=1
character(20) arr(20000),str
Integer n,ss(20),k,m
n = 4
k = 0
m = JC(n)
Do While (k < m)
str = ""
Call GetNum(str, n) !获得随机数
!判断随机数是否已经出现过
ii = 1
do i = 1 , k
If (trim(arr(i))==trim(str)) Then
ii = 0
cycle
End If
enddo
!如果随机数未出现过则储存在arr中
If (ii == 1) Then
k = k + 1
arr(k) = str
!输出结果 也可输出到文件中
write(*,*) trim(str)
End If
end do
End program
Subroutine GetNum(str, n)
!获得一个n位随机数,该随机数内各位数字都不重复
character(20) str
character(1) s1
Integer ss(20),n,a,b
real x
str = ""
ss=0
i = 0
Do While (Len(trim(str)) < n)
call random(x)
a = Int(x * n + 1)
b = 1
do j = 1, i
If (a == ss(j)) Then
b = 0
cycle
End If
end do
If (b == 1) Then
i = i + 1
ss(i) = a
write(s1,'(i1)') a
str = trim(str) // s1
End If
end do
End Subroutine
integer Function JC(n)
!计算n的阶乘
integer n
JC = 1
do i = 1 , n
JC = JC * i
end do
End Function
我的源代码是vba的,结果可以直接输出在excel中,花了好久改成fortran.vba的也一起给你吧:
Private Sub nj()
Dim arr(10000) As String, n As Integer, str As String, ss(10) As Integer
Sheet1.Cells.Clear
n = 5
k = 0
m = JC(n)
Do While k < m
str = ""
Call GetNum(str, n)
ii = 1
For i = 1 To k
If arr(i) = str Then
ii = 0
Exit For
End If
Next
If ii = 1 Then
k = k + 1
arr(k) = str
Cells(k, 1) = str
End If
DoEvents
Loop
MsgBox ""
End Sub
Private Sub GetNum(str, n)
Dim ss() As Integer
str = ""
ReDim ss(n)
i = 0
Do While Len(str) < n
a = Int(Rnd * n + 1)
b = 1
For j = 1 To i
If a = ss(j) Then
b = 0
Exit For
End If
Next
If b = 1 Then
i = i + 1
ss(i) = a
str = str & ss(i)
'MsgBox ss(i) & "," & str
End If
DoEvents
Loop
End Sub
Private Function JC(n)
JC = 1
For i = 1 To n
JC = JC * i
Next
End Function
再问: 暂时没时间细看,但是 ··· 随机数? 我也想过用随机方法把所有不重复的都试出来? 运算量是不大了点?
再答: 速度很快的,你运行一下试试就知道。
下面有一个人分享的算法没用随机数,但计算量貌似惊人了。参考一下吧(里面也有我的回答):
http://zhidao.baidu.com/question/872680146775499292.html?oldq=1
求一fortran程序,取出n个连续自然数的所有n级排列.
编写程序验证:任何一个自然数n立方都等于n个连续奇数之和.要求对每个输入的自然数计算并输出相应连续奇数
求100以内的自然数N 使得从一开始的连续N个自然数的立方之和大于3000000
求最大的自然数n,使得从1到连续n个自然数的立方和小于50000
N满足:N恰有144个不同约数; 在N的所有约数中有10个连续的非零自然数.
用matlab编写一个程序,要求任给一个自然数n,输出1到n之间所有可以用若干连续自然数之和表示的数,并输出其所有表示方
连续N个自然数的平方的和等于多少
将自然数2010拆分成N个连续自然数的和,那么,N=?
C语言程序设:输入两个正整数m和n,求它们所有的公约数,从大到小排列
如果5个连续自然数的和是90,中间数是n,求n的值.(列方程.)
求五十以内最小的自然数N使得从一零二开始的连续N 个偶数之和大于两千
c++,验证任何一个自然数n的立方都可以写成n个连续奇数之和,求修改