Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2
Input #1, N1, N2
If N1 > N2 Then maxone = N1 Else maxone = N2 tmp = 0 ans = 0 i = 1 ch = False
Do num1 = Left(N1 Mod (10 ^ i), 1) num2 = Left(N2 Mod (10 ^ i), 1) If i > Len(N1) Then num1 = 0: If i > Len(N2) Then num2 = 0 If Len(maxone) < i Then Exit Do If (num1 + num2 + tmp) > 9 Then ans = ans + 1 sums = tmp + num1 + num2 tmp = sums \ 10 Else tmp = 0 End If i = i + 1 Loop
If LL >= i Then C = C + (A Mod 10) + (B Mod 10) A= A \ 10 B= B \ 10 Else If LL > LB Then C = C + (A Mod 10):A= A \ 10 If LL < LA Then C = C + (B Mod 10):B= B \ 10 End If
Dim A As Long Dim B As Long Dim C As Integer Dim LA As Byte Dim LB As Byte Dim LL As Byte dim LL2 As Byte Dim ans As Byte
Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Input #1, A, B Close #1
LA = Len(CStr(A)) LB = Len(CStr(B)) If LB > LA Then LL = LB: LL2 = LA Else LL = LA: LL2 = LB End If
For i = 1 To LL
If LL2 >= i Then C = C + (A Mod 10) + (B Mod 10) A = A \ 10 B = B \ 10 Else If LL > LB Then C = C + (A Mod 10): A = A \ 10 If LL < LA Then C = C + (B Mod 10): B = B \ 10 End If
If C >= 10 Then ans = ans + 1: C = 1
Next i
Open App.Path & "\out.txt" For Output As #2 Print #2, ans Close #2
佑好, If LL2 >= i Then C = C + (A Mod 10) + (B Mod 10) A = A \ 10 B = B \ 10 Else If LL > LB Then C = C + (A Mod 10): A = A \ 10 If LL < LA Then C = C + (B Mod 10): B = B \ 10 End If 這一段程式,沒有必要。 直接寫成--> C = C + (A Mod 10) + (B Mod 10) A = A \ 10 B = B \ 10 不用判斷,答案相同吧。
arro好, 程式還正確而已, If i > Len(NN1) Then num1 = 0: If i > Len(NN2) Then num2 = 0 If Len(maxone) < i Then Exit Do 這三個if都不好。 前兩個白寫,後一個應該寫在 do loop的條件上,前測、後測都行, 用until、while都行。 那個 i = i + 1 也沒必要。 再想想,再寫一次。
緣尉好, 程式寫的不錯,只是進入了一個小誤區,999+1,會如何呢? If m >= n Then s = m Else s = n 這一行的判斷是不需要的。 去掉s之後,那個 do loop的尾端改成 loop until n = 0 and m = 0 哦,還有一個錯,你將進位的變數,和記住進位的個數的變數,給搞混了。 需要再一個變數c If t + a + b >= 10 Then t = t + 1 將這一行改成 c = (c+a+b) \10 : t=t+c 不用判斷,反正有進位加1次,沒進位加0次。 緣尉你要將程式再組合一次,貼出來哦。
Dim s Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Line Input #1, n s = Split(n) Call A1 Close Close End End Sub
Sub A1()
Dim ans Do Until Len(s(0)) >= 5: s(0) = "0" & s(0): Loop Do Until Len(s(1)) >= 5: s(1) = "0" & s(1): Loop
Dim m1%, m2%, tmp% For i = 5 To 1 Step -1 m1 = Mid(s(0), i, 1) m2 = Mid(s(1), i, 1) If m1 + m2 + tmp > 9 Then tmp = 1: ans = ans + 1 Else tmp = 0 Next
Dim num1(10) As Integer, num2(10) As Integer
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N1, N2
If N1 > N2 Then maxone = N1 Else maxone = N2
tmp = 0
ans = 0
For i = 1 To 10
num1(i) = 0: num2(i) = 0
Next
For i = 1 To Len(N1)
num1(i) = Mid(N1, (Len(N1) + 1 - i), 1)
Next
For i = 1 To Len(N2)
num2(i) = Mid(N2, (Len(N2) + 1 - i), 1)
Next
For i = 1 To Len(maxone)
If (num1(i) + num2(i) + tmp) > 9 Then
ans = ans + 1
sums = tmp + num1(i) + num2(i)
tmp = Mid(sums, 1, 1)
End If
Next
Print #2, ans
Close #1
Close #2
End
End Sub
------------------
這次寫得有點亂
不過應該正確吧 =D
裡面的 mid 應該可以換成 right
arro好,
回覆刪除tmp = Mid(sums, 1, 1)
這行算進位的,如果改成用 tmp = sums \ 10更好些。
另外,題目並沒有說是幾位數,你當成最大十位數,會出錯吧。
還有,這些題目,其實是從各地找來的,並不一定很完整,你在重新出題在這兒的時候,可以將題目完善些,例如這題加入兩數的範圍,會好些。
所以,如果題目是真的不限輸入是幾位數的數字,程式要重新寫。
固定長度的迴圈,用for next
不固定長度的迴圈,用do loop,反正加到兩數都沒有了,才停。
再試試吧。
改用 DO LOOP 的方法
回覆刪除這種方法要注意的地方還蠻多的
原來 right 不能抓單一個數
所以只好用Left(N1 Mod (10 ^ i), 1) 這種奇特的方法
定為10位還是比較好做
不過,完美一點還是做無上限的好了
----------------------
Dim num1 As Integer, num2 As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N1, N2
If N1 > N2 Then maxone = N1 Else maxone = N2
tmp = 0
ans = 0
i = 1
ch = False
Do
num1 = Left(N1 Mod (10 ^ i), 1)
num2 = Left(N2 Mod (10 ^ i), 1)
If i > Len(N1) Then num1 = 0: If i > Len(N2) Then num2 = 0
If Len(maxone) < i Then Exit Do
If (num1 + num2 + tmp) > 9 Then
ans = ans + 1
sums = tmp + num1 + num2
tmp = sums \ 10
Else
tmp = 0
End If
i = i + 1
Loop
Print #2, ans
Close #1
Close #2
End
End Sub
arro好,
回覆刪除取最右邊的數字的方式,你的方式也是可以的,但是,還是不好。
先了解"字串"和"數字"的分別。
在這題中,你可以將N1,N2這兩個變數,轉成字串,然後再用right,left,mid,len這些字串函數去處理。
或是,第二個方式,N1,N2當成數字,用mod 10取個位數,再用N1=N1\10去掉一個數。
另外,還要注意字串長度比較長,數字的話,長度很短的,integer 到3萬多,long也只到幾十億而已。
還有,字串的+,是連接起來。不是數字上的+,這些小細節,在除錯的時候,很累人的。
字串跟數字真的蠻容易令人混淆的
回覆刪除數字 用 len 好像會少 1 的樣子
Dim A As Integer
回覆刪除Dim B As Integer
Dim C As Integer
Dim LA As Byte
Dim LB As Byte
Dim LL As Byte
Dim ans As Byte
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, A, B
Close #1
LA = Len(CStr(A))
LB = Len(CStr(B))
If LB > LA Then LL = LB Else LL = LA
For i = 1 To LL
C = C + Right(A, i) + Right(B, i)
If C >= 10 Then ans = ans + 1: C = 1
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, ans
Close #2
End
End Sub
數值好像會保留正負符號
LEN會多1
更正,我應該要用Mid
回覆刪除可是用Mid是從左邊開始取
我改用Mod取餘數
這麼一來
9555 555
555 9555
即可判斷
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim LA As Byte
Dim LB As Byte
Dim LL As Byte
Dim ans As Byte
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, A, B
Close #1
LA = Len(CStr(A))
LB = Len(CStr(B))
If LB > LA Then LL = LB Else LL = LA
For i = 1 To LL
If LL >= i Then
C = C + (A Mod 10) + (B Mod 10)
Else
If LL > LB Then C = C + (A Mod 10)
If LL < LA Then C = C + (B Mod 10)
End If
If C >= 10 Then ans = ans + 1: C = 1
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, ans
Close #2
End
End Sub
洗澡時想到有錯誤:P
回覆刪除If LL >= i Then
C = C + (A Mod 10) + (B Mod 10)
A= A \ 10
B= B \ 10
Else
If LL > LB Then C = C + (A Mod 10):A= A \ 10
If LL < LA Then C = C + (B Mod 10):B= B \ 10
End If
佑好,
回覆刪除開學了,終於。
你們幾個寒假做得並不理想。
開學後,每星期做兩題,星期一的六七兩節到選手室,討論。
早自習的部分,再等等,我和葉組長確認後再說。
**
這題的部分,請問,在迴圈內,LL什麼時候會比i小呢?
也就是說if LL>=i then 這行,一直是真吧?
Dim A As Long
回覆刪除Dim B As Long
Dim C As Integer
Dim LA As Byte
Dim LB As Byte
Dim LL As Byte
dim LL2 As Byte
Dim ans As Byte
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, A, B
Close #1
LA = Len(CStr(A))
LB = Len(CStr(B))
If LB > LA Then
LL = LB: LL2 = LA
Else
LL = LA: LL2 = LB
End If
For i = 1 To LL
If LL2 >= i Then
C = C + (A Mod 10) + (B Mod 10)
A = A \ 10
B = B \ 10
Else
If LL > LB Then C = C + (A Mod 10): A = A \ 10
If LL < LA Then C = C + (B Mod 10): B = B \ 10
End If
If C >= 10 Then ans = ans + 1: C = 1
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, ans
Close #2
End
End Sub
增加一個LL2判斷較小值
佑好,
回覆刪除If LL2 >= i Then
C = C + (A Mod 10) + (B Mod 10)
A = A \ 10
B = B \ 10
Else
If LL > LB Then C = C + (A Mod 10): A = A \ 10
If LL < LA Then C = C + (B Mod 10): B = B \ 10
End If
這一段程式,沒有必要。
直接寫成-->
C = C + (A Mod 10) + (B Mod 10)
A = A \ 10
B = B \ 10
不用判斷,答案相同吧。
熊掌好,
回覆刪除對耶
我想成
(較短的數 mod 10)
會比較早先變0
所以就做一些判斷。
結果反而更冗長...
謝謝指教
Dim num1 As Integer, num2 As Integer
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N1, N2
NN1 = N1
NN2 = N2
If N1 > N2 Then maxone = N1 Else maxone = N2
tmp = 0
ans = 0
i = 1
ch = False
Do
num1 = N1 Mod 10
num2 = N2 Mod 10
If i > Len(NN1) Then num1 = 0: If i > Len(NN2) Then num2 = 0
If Len(maxone) < i Then Exit Do
If (num1 + num2 + tmp) > 9 Then
ans = ans + 1
sums = tmp + num1 + num2
tmp = 1
Else
tmp = 0
End If
i = i + 1
N1 = N1 \ 10
N2 = N2 \ 10
Loop
Print #2, ans
Close #1
Close #2
End
End Sub
//--
arro好,
回覆刪除程式還正確而已,
If i > Len(NN1) Then num1 = 0: If i > Len(NN2) Then num2 = 0
If Len(maxone) < i Then Exit Do
這三個if都不好。
前兩個白寫,後一個應該寫在 do loop的條件上,前測、後測都行,
用until、while都行。
那個 i = i + 1 也沒必要。
再想想,再寫一次。
Private Sub Form_Load()
回覆刪除Dim m As Long
Dim n As Long
Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, m, n
If m >= n Then s = m Else s = n
t = 0
Do
a = m Mod 10
b = n Mod 10
If t + a + b >= 10 Then t = t + 1
m = m \ 10
n = n \ 10
s = s \ 10
If s = 0 Then Exit Do
Loop
Print #2, t
Close #1
Close #2
End
End Sub
緣尉好,
回覆刪除程式寫的不錯,只是進入了一個小誤區,999+1,會如何呢?
If m >= n Then s = m Else s = n
這一行的判斷是不需要的。
去掉s之後,那個 do loop的尾端改成
loop until n = 0 and m = 0
哦,還有一個錯,你將進位的變數,和記住進位的個數的變數,給搞混了。
需要再一個變數c
If t + a + b >= 10 Then t = t + 1
將這一行改成
c = (c+a+b) \10 : t=t+c
不用判斷,反正有進位加1次,沒進位加0次。
緣尉你要將程式再組合一次,貼出來哦。
Dim s
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, n
s = Split(n)
Call A1
Close
Close
End
End Sub
Sub A1()
Dim ans
Do Until Len(s(0)) >= 5: s(0) = "0" & s(0): Loop
Do Until Len(s(1)) >= 5: s(1) = "0" & s(1): Loop
Dim m1%, m2%, tmp%
For i = 5 To 1 Step -1
m1 = Mid(s(0), i, 1)
m2 = Mid(s(1), i, 1)
If m1 + m2 + tmp > 9 Then tmp = 1: ans = ans + 1 Else tmp = 0
Next
Print #2, ans
End Sub
3:27