+ 1
C# Algorithm Pi.Works till 32?And then Exception.The problem was deciding!!!))
C# Works till 32?And then Exception https://code.sololearn.com/c3Btcqz3KJfc/?ref=app
5 ответов
+ 2
It definitely fails at:
int replaced = Int32.Parse(pi.ToString().Substring((i-k), (i - k + 1)));
Changing to this prevents the crash but I didn't test that the digits of PI are calculated perfectly:
int replaced = Int32.Parse(pi.ToString().Substring((i-k), 1));
Note that the second parameter of Substring is a length. It is not an end index. Maybe confusing these is your mistake. More details on the Substring method are at:
https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netcore-3.1
+ 1
thanks, You are right)
+ 1
And how to decide this problem?
+ 1
I decided problem)) It was in this int replaced = Int32.Parse(pi.ToString().Substring((i-k), 1));
if (replaced == 9) {
replaced = 0;
} else {
replaced++;
}
pi.ToString().Remove(i - k,1);
+ 1
So algorithm very well, but it’s a simple and not quick