[ VB2010 ] --用遞迴方法 輸入任意兩個數字,以遞迴方式求出最大公因數。
Public Class Form1
Private Function gcd(ByVal b As Integer, ByVal R As Integer) '/*遞迴程式部分*/
Dim a As Integer
If (b > 0) Then
a = R
R = b
b = a Mod R
Return gcd(b, R)
Else
Return R
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, R, b As Integer
num1 = Val(TextBox2.Text)
num2 = Val(TextBox3.Text)
b = num1 Mod num2
R = num2
R = gcd(b, R)
TextBox4.Text = gcd(b, R).ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class
算數運算子:
運算子 | 說明 | 範例 |
+ | 加 | 1 + 1 = 2 |
- | 減 | 2 - 1 = 1 |
* | 乘 | 2 * 3 = 6 |
/ | 除 | 13 / 8 = 1.625 |
\ | 除 | ( 只取整數部分) 13 \ 8 = 1 |
Mod | 取餘數 | 13 Mod 8 = 5 、26 Mod 13 = 0 |
^ | 次方 | 2 ^ 3 = 8 |
設定運算子:
運算子 | 說明 (作用) | 範例(number = 10) |
= | 等於 (設值) | number = 10 |
+ = | 加之後設定 | number += 2(相當於number = number + 2) |
- = | 減之後設定 | number -= 2(相當於number = number - 2) |
* = | 乘之後設定 | number *= 2(相當於 number = number * 2) |
/ = | 除之後設定 | number /= 2(相當於number = number / 2) |
\ = | 除之後設定 | number \= 2(相當於 number = number \2)(只取整數部分) |
^ = | 次方之後設定 | number ^ = 2(相當於 number = number ^ 2) |
&= | 字串相加之後設定 | number &= 2(相當於number = number &2) |
沒有留言:
張貼留言