代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
VB.Net
】
NOI地震问题
作者:
美杜莎-
/ 发布于
2013/10/23
/
1325
Public Class NOI地震问题 Dim country As Single Dim ways As Single Dim lnk(0 To 200) As Link Dim ZF(0 To 1275, 0 To 50) As Link '走法,50个村庄最多有1275种走法,每种走法最多有50步 Dim temp(0 To 50) As Link '临时存放山路 Public Function JieCheng(ByVal number As Single) '求和函数,用来判断村庄最多能有几条路 Dim rezult As Integer rezult = 0 For i = 1 To number rezult = i + rezult Next JieCheng = rezult End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Enabled = False TextBox2.Enabled = False TextBox3.Enabled = False Button1.Enabled = False Button4.Enabled = False End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Static n As Single Select Case Button1.Text Case "开始" n = 0 country = Val(TextBox4.Text) ways = Val(TextBox5.Text) If ((ways) >= (country - 1)) And ((ways) <= (JieCheng(country - 1))) Then '山路数大于(村庄数-1),小于一到(村庄-1)的和,正常执行 TextBox4.Enabled = False TextBox5.Enabled = False TextBox1.Enabled = True TextBox2.Enabled = True TextBox3.Enabled = True Button1.Enabled = False ListBox1.Items.Add("开始") Button1.Text = "继续" Else If ((ways) >= (country - 1)) Then MsgBox("路的数量比(一到村庄-1)的和的数目还多,您输入的数据不符合要求!请重新输入") Else MsgBox("路的数量比(村庄-1)的数目还少,您输入的数据不符合要求!请重新输入") End If TextBox4.Clear() country = Nothing ways = Nothing TextBox5.Clear() Button1.Enabled = False TextBox4.Focus() Exit Sub End If Case "继续" '继续按钮 If TextBox1.Text = TextBox2.Text Then '判断输入的两个村庄是否相同 MsgBox("输入的村庄A不能与村庄B相同,请从新输入!") TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox1.Focus() Exit Sub End If n = n + 1 If n <= ways Then With lnk(n) '为lnk(n)赋值 .F = Val(TextBox1.Text) .T = Val(TextBox2.Text) .level = Val(TextBox3.Text) End With If n <> 1 Then For i = 1 To (n - 1) If (lnk(i).F = lnk(n).F And lnk(i).T = lnk(n).T) Or (lnk(i).T = lnk(n).F And lnk(i).F = lnk(n).T) Then '判断在之前几次输入的项目中有没有重复 MsgBox("输入的项目重复,请从新输入!") TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox1.Focus() n = n - 1 Exit Sub End If Next End If ListBox1.Items.Add("第" & n & "次输入:") ListBox1.Items.Add("村庄" & Val(TextBox1.Text) & "到村庄" & Val(TextBox2.Text) & "的震级为" & Val(TextBox3.Text)) TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() If n = ways Then ListBox1.Items.Add("输入完毕!") Button1.Enabled = False Button1.Text = "开始" Button4.Enabled = True TextBox1.Enabled = False TextBox2.Enabled = False TextBox3.Enabled = False End If End If End Select TextBox1.Focus() End Sub Private Sub TextBox4_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.LostFocus If (Val(TextBox4.Text) >= 2 And Val(TextBox4.Text) <= 50) Or (TextBox4.Text = "") Then If (TextBox4.Text <> "") And (TextBox5.Text <> "") Then Button1.Enabled = True Else Button1.Enabled = False End If Else MsgBox("输入的数据有误!!!请重新输入") TextBox4.Clear() End If End Sub Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged If (Val(TextBox5.Text) >= 1 And Val(TextBox5.Text) <= 200) Or (TextBox5.Text = "") Then If (TextBox4.Text <> "") And (TextBox5.Text <> "") Then Button1.Enabled = True Else Button1.Enabled = False End If Else MsgBox("输入的数据有误!!!请重新输入") TextBox5.Clear() End If End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If (Val(TextBox1.Text) >= 1 And Val(TextBox1.Text) <= country) Or (TextBox1.Text = "") Then If (TextBox1.Text <> "") And (TextBox2.Text <> "") And (TextBox3.Text <> "") Then Button1.Enabled = True Else Button1.Enabled = False End If Else MsgBox("输入的数据有误!!!请重新输入") TextBox1.Clear() End If End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged If (Val(TextBox2.Text) >= 1 And Val(TextBox2.Text) <= country) Or (TextBox2.Text = "") Then If (TextBox1.Text <> "") And (TextBox2.Text <> "") And (TextBox3.Text <> "") Then Button1.Enabled = True Else Button1.Enabled = False End If Else MsgBox("输入的数据有误!!!请重新输入") TextBox2.Clear() End If End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged If (Val(TextBox3.Text) >= 1 And Val(TextBox3.Text) <= 16) Or (TextBox3.Text = "") Then '震级在一到十六之间 If (TextBox1.Text <> "") And (TextBox2.Text <> "") And (TextBox3.Text <> "") Then Button1.Enabled = True Else Button1.Enabled = False End If Else MsgBox("输入的数据有误!!!震级不能超过16,请重新输入") TextBox3.Clear() End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click country = 0 ways = 0 ReDim lnk(0 To 200) ReDim ZF(0 To 1275, 0 To 50) ReDim temp(0 To 50) ListBox1.Items.Clear() TextBox1.Enabled = False TextBox2.Enabled = False TextBox3.Enabled = False TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() TextBox4.Enabled = True TextBox5.Enabled = True Button4.Enabled = False Button1.Text = "开始" End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MsgBox("【题目描述】" & vbCrLf & vbCrLf & _ "一座地震频发的山上有N个小村庄,编号分别为1 至N 。某些村庄之间有山路相连,每条山路都有一个防震等级。假如一条路的防震等级是k (k > 0),就表示如果发生k 级或者小于k 级的地震,这条山路是安然无恙的,但如果发生大于k 级的地震,这条路就会被毁坏,不能通过了。" & vbCrLf & vbCrLf & _ "现在邮递员小明要从村庄1 出发去村庄N ,他想知道最高发生几级地震的情况下,他仍然能顺利到达目的地。 " & vbCrLf & vbCrLf & _ "输入数据的第一行是用空格隔开的两个整数N 和M ,分别表示村庄数和山路数(2 ≤N ≤50, 1≤M ≤200)。按下来有M 行,每行包含三个整数u, v, w,表示村庄u 和村庄v 之间有一条防震等级为w 的路(1 ≤u, v ≤N, 1 ≤w ≤20)。路是双向的,在没有被毁坏的情况下,既可以从u 到达v,也可以从v 到达u。两个村庄之间最多有一条路。输入数据保证在没有发生的地震的时候可以从村庄1 到达村庄N 。" & vbCrLf & vbCrLf & _ "输出只有一行,包含一个整数,表示最高发生几级地震的情况下仍然能到达村庄N 。" & vbCrLf & vbCrLf & _ "【样例输入】" & vbCrLf & _ "4 5" & vbCrLf & _ "1 2 3 " & vbCrLf & _ "1 3 5 " & vbCrLf & _ "2 4 10 " & vbCrLf & _ "3 4 6 " & vbCrLf & _ "1 4 1 " & vbCrLf & _ "【样例输出】 " & vbCrLf & _ "5" & vbCrLf & _ "【样例解释】" & vbCrLf & _ "在发生5 级地震的情况下,小明可以通过1 →3 →4 这条路线到达村庄4。但如果发生6 级地震就不能到达了。") End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click '运算按钮 Dim number_1, cout_1 As Single number_1 = 0 cout_1 = 1 Do If (lnk(cout_1).F = 1) Or (lnk(cout_1).T = 1) Then '找到与村庄1连接的山路 temp(1) = lnk(cout_1) '存放山路到temp临时变量中 If (lnk(cout_1).F = country) Or (lnk(cout_1).T = country) Then '判断与村庄1连接的村庄A是不是村庄N ReDim temp(0 To 50) '清空曾存放在temp临时变量的数据 temp(1) = lnk(cout_1) '重新存放 添加走法() '是村庄N,添加到走法变量中 cout_1 = cout_1 + 1 '查找次数+1,退出本次查找 Else If lnk(cout_1).F = 1 Then '获得与村庄1连接的村庄A 查找(lnk(cout_1).T, 2) Else 查找(lnk(cout_1).F, 2) End If cout_1 = cout_1 + 1 '查找次数+1 End If number_1 = number_1 + 1 '找到的与村庄1连接的村庄+1 Else '本次没有找到 cout_1 = cout_1 + 1 End If Loop Until (cout_1 = 200) Or (lnk(cout_1).F = 0 And lnk(cout_1).T = 0) If number_1 = 0 Then MsgBox("找不到与村庄1连接的山路,无法到达村庄N的山路!大爷懒得列举了!") Else Dim j, k As Integer Dim Min(0 To 1275) As Single j = 1 ListBox1.Items.Add("") ListBox1.Items.Add("开始列举山路的走法") ListBox1.Items.Add("") Do ListBox1.Items.Add("第" & j & "种走法") k = 1 Do ListBox1.Items.Add("第" & k & "步" & "-> 从" & ZF(j, k).F & "到" & ZF(j, k).T & "震级为" & ZF(j, k).level) k = k + 1 Loop Until k = 50 Or (ZF(j, k).F = 0 And ZF(j, k).T = 0) If (ZF(j, k - 1).F = country) Or (ZF(j, k - 1).T = country) Then Min(j) = 最小(ZF, j, k - 1) Else Min(j) = 0 ListBox1.Items.Add("不幸的是这种走法到不了村庄N") End If ListBox1.Items.Add("最大能承受的震级是" & 最小(ZF, j, k - 1)) ListBox1.Items.Add("") j = j + 1 Loop Until (ZF(j, 1).F = 0 And ZF(j, 1).T = 0) ListBox1.Items.Add("列举完毕!") ListBox1.Items.Add("最终结果,能承受最大的震级是" & (最大(Min, j - 1))) MsgBox("完成遍历!", MsgBoxStyle.Information) End If End Sub Public Sub 查找(ByVal 村庄A As Single, ByVal 存放位置 As Single) '查找与村庄A连接的村庄 Dim number_x, cout_x As Single number_x = 0 cout_x = 1 Do If (lnk(cout_x).F = 村庄A) Or (lnk(cout_x).T = 村庄A) Then '找到与村庄A连接的山路 If 判断重复(lnk(cout_x)) = False Then '找到的与村庄A连接的山路没有重复 number_x = number_x + 1 '找到连接村庄A的山路+1 temp(存放位置) = lnk(cout_x) '把找到的连接村庄A的山路存放到temp临时变量中 If (lnk(cout_x).F = country) Or (lnk(cout_x).T = country) Then '判断与村庄A连接的村庄B是不是村庄N For i = 存放位置 To 50 '清空以前在cout_x之后存放的数据 temp(i).F = 0 temp(i).T = 0 temp(i).level = 0 Next temp(存放位置) = lnk(cout_x) '重新存放 添加走法() '是村庄N,添加到走法变量中 cout_x = cout_x + 1 '查找次数+1 Else If lnk(cout_x).F = 村庄A Then '不是村庄N:获得与村庄A连接的村庄B,并查找与村庄B的山路 查找(lnk(cout_x).T, 存放位置 + 1) Else 查找(lnk(cout_x).F, 存放位置 + 1) End If cout_x = cout_x + 1 '查找次数+1 End If Else '找到的与村庄A连接的山路重复了 cout_x = cout_x + 1 End If Else '本次没有找到 cout_x = cout_x + 1 End If Loop Until (cout_x = 200) Or (lnk(cout_x).F = 0 And lnk(cout_x).T = 0) If number_x = 0 Then '没有找到另外与村庄A连接的村庄,既是到了走法的尽头 添加走法() '添加到走法变量中 Exit Sub '退出本次查找,返回上一个村庄 Else End If End Sub Public Function 判断重复(ByVal 山路 As Link) As Boolean Dim cout As Single cout = 1 Dim Bool As Boolean '以用于退出循环 Do If (temp(cout).F = 山路.F And temp(cout).T = 山路.T) Or (temp(cout).F = 山路.T And temp(cout).T = 山路.F) Then Bool = True Else Bool = False cout = cout + 1 End If Loop Until (cout = 50) Or (temp(cout).F = 0 And temp(cout).T = 0) Or (Bool = True) 'temp临时变量的尽头或temp临时变量没有存放数据的位置 判断重复 = Bool End Function Public Sub 添加走法() Dim n As Single n = 1 Dim add As Boolean add = False '用来判断走法是否已经添加,以用于退出循环 Do If (ZF(n, 1).F = 0 And ZF(n, 1).T = 0) Then '判断第n行走法变量,是不是已经有了真实走法 Dim m As Single m = 1 Do ZF(n, m) = temp(m) m = m + 1 Loop Until (m = 50) Or (temp(m).F = 0 And temp(m).T = 0) '存放在temp临时变量存放到走法变量中 add = True Else n = n + 1 End If Loop Until n = 1275 Or add = True End Sub Public Function 最小(ByVal x(,) As Link, ByVal s As Single, ByVal n As Single) 'x() 为要比较的项目数组,s为第s种走法,n为项目数目 Dim a, b As Single a = 1 Do b = 0 For i = 1 To n If x(s, i).level >= x(s, a).level Then b = b + 1 End If Next If b <> n Then a = a + 1 End If Loop Until b = n 最小 = x(s, a).level End Function Public Function 最大(ByVal x() As Single, ByVal n As Single) 'x() 为要比较的项目数组,n为项目数目 Dim a, b As Single a = 1 Do b = 0 For i = 1 To n If x(i) <= x(a) Then b = b + 1 End If Next If b <> n Then a = a + 1 End If Loop Until b = n 最大 = x(a) End Function Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click country = 5 ways = 10 lnk(1).F = 1 lnk(1).T = 2 lnk(1).level = 3 lnk(2).F = 2 lnk(2).T = 3 lnk(2).level = 2 lnk(3).F = 3 lnk(3).T = 4 lnk(3).level = 4 lnk(4).F = 4 lnk(4).T = 5 lnk(4).level = 6 lnk(5).F = 5 lnk(5).T = 1 lnk(5).level = 1 lnk(6).F = 1 lnk(6).T = 3 lnk(6).level = 4 lnk(7).F = 3 lnk(7).T = 5 lnk(7).level = 5 lnk(8).F = 5 lnk(8).T = 2 lnk(8).level = 7 lnk(9).F = 2 lnk(9).T = 4 lnk(9).level = 6 lnk(10).F = 4 lnk(10).T = 1 lnk(10).level = 8 Button4.Enabled = True Button1.Enabled = False End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click country = 6 ways = 15 lnk(1).F = 1 lnk(1).T = 2 lnk(1).level = 6 lnk(2).F = 2 lnk(2).T = 3 lnk(2).level = 5 lnk(3).F = 3 lnk(3).T = 4 lnk(3).level = 4 lnk(4).F = 4 lnk(4).T = 5 lnk(4).level = 3 lnk(5).F = 5 lnk(5).T = 6 lnk(5).level = 2 lnk(6).F = 6 lnk(6).T = 1 lnk(6).level = 1 lnk(7).F = 1 lnk(7).T = 3 lnk(7).level = 4 lnk(8).F = 3 lnk(8).T = 5 lnk(8).level = 8 lnk(9).F = 5 lnk(9).T = 1 lnk(9).level = 6 lnk(10).F = 2 lnk(10).T = 4 lnk(10).level = 6 lnk(11).F = 4 lnk(11).T = 6 lnk(11).level = 10 lnk(12).F = 6 lnk(12).T = 2 lnk(12).level = 8 lnk(13).F = 1 lnk(13).T = 4 lnk(13).level = 5 lnk(14).F = 2 lnk(14).T = 5 lnk(14).level = 7 lnk(15).F = 3 lnk(15).T = 6 lnk(15).level = 9 Button4.Enabled = True Button1.Enabled = False End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim s As Integer s = 1 Dim v As String v = InputBox("要查找的震级", , "最大能承受的震级是") Do If ListBox1.Items.Item(s) = v Then ListBox1.SetSelected(s, True) End If s = s + 1 Loop Until ListBox1.Items.Count = s End Sub End Class Public Structure Link Dim F As Single Dim T As Single Dim level As Single End Structure
试试其它关键字
NOI
同语言下
.
根据User Agent来判定操作系统与浏览器类型
.
将PPT内容导出为JPG图片
.
Java屏幕截取
.
发送邮件组件
.
子窗体在父窗体指定的控件中显示
.
取文件的大小
.
从某个目录中筛选文件
.
VB脚本调用exe应用程序并传递参数
.
图像格式转换工具
.
洗牌算法
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
美杜莎-
贡献的其它代码
(
2
)
.
NOI地震问题
.
编写一个程序 把八个整数读入一个数组中 然后以相反的
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3