2 answers
Hello Providence,
Your algorithm is wrong, indeed your loop actually counts the number of 1s in 2007 pages and not the number of pages to get 2007 "1s", it would be better to write
Function test() As Integer
Dim int_I, Int_Pages As Integer
Dim str_Temp As String
int_I = 0
Int_Pages = 0
While int_I < 2007
Int_Pages = Int_Pages + 1
str_Temp = Int_Pages
While InStr(1, str_Temp, "1") > 0
int_I = int_I + 1
str_Temp = Right(str_Temp, Len(str_Temp) - InStr(1, str_Temp, "1"))
Wend
Wend
test = Int_Pages
End Function
And you get 3169 pages
Irem
Your algorithm is wrong, indeed your loop actually counts the number of 1s in 2007 pages and not the number of pages to get 2007 "1s", it would be better to write
Function test() As Integer
Dim int_I, Int_Pages As Integer
Dim str_Temp As String
int_I = 0
Int_Pages = 0
While int_I < 2007
Int_Pages = Int_Pages + 1
str_Temp = Int_Pages
While InStr(1, str_Temp, "1") > 0
int_I = int_I + 1
str_Temp = Right(str_Temp, Len(str_Temp) - InStr(1, str_Temp, "1"))
Wend
Wend
test = Int_Pages
End Function
And you get 3169 pages
Irem
In the 2007 encyclopedia of games, it took 2007 times "1" to number all the pages.
Write a VB6 program that allows us to determine the number of pages in this encyclopedia.
Note: When numbering from 1 to 20, we use "1" a total of 12 times!
On the forum, I got a rough solution that involves the SUBSTRING function, but I'm not very sure how it works. I would like to get your opinion on this below is the solution:
Option Explicit
Dim i As Integer
Dim chaine As String
Dim nb_page As Integer
nb_page = 0
i = 0
While (i <> 2007)
chaine = Str(i) 'I have doubts about the syntax for converting an integer to a string
'Here you read your string using an if statement like this
' We loop as long as there are "1"s in the string.
j = 0
while (substring(j, chaine, "1") > 0)
nb_page = nb_page + 1
j = substring(j, chaine, "1")
wend
i = i + 1 'essential to avoid an infinite loop!
Wend