Answer the question
In order to leave comments, you need to log in
Because of what there can be an access violation when solving this problem?
I'm trying to solve problem 1258. Pool . A segmentation fault occurs even though the same C++ code passes.
Here is the C# code:
using System;
using System.Threading;
namespace _1258шарп
{
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
long W, H, x, y, xf, yf;
string str;
String[] N = Console.ReadLine().Split();
W = long.Parse(N[0].Trim()); H = long.Parse(N[1].Trim());
N = Console.ReadLine().Split();
x = long.Parse(N[0].Trim()); y = long.Parse(N[1].Trim());
N = Console.ReadLine().Split();
xf = long.Parse(N[0].Trim()); yf = long.Parse(N[1].Trim());
str = Console.ReadLine();
long xd = 0, yd = 0;
foreach (char c in str)
{
switch (c)
{
case 'F':
yd += y;
y = 0;
break;
case 'B':
yd += H - y;
y = H;
break;
case 'R':
xd += W - x;
x = W;
break;
case 'L':
xd += x;
x = 0;
break;
}
}
xd += Math.Abs(x - xf); yd += Math.Abs(y - yf);
Console.Write("{0:F4}", Math.Sqrt((double)(xd * xd + yd * yd)));
}
}
}
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
long long W, H, x, y, xf, yf;
string str;
cin >> W >> H >> x >> y >> xf >> yf >> str;
long long xd = 0, yd = 0;
for (auto c : str)
{
switch (c)
{
case 'F':
yd += y;
y = 0;
break;
case 'B':
yd += H - y;
y = H;
break;
case 'R':
xd += W - x;
x = W;
break;
case 'L':
xd += x;
x = 0;
break;
}
}
xd += abs(x - xf), yd += abs(y - yf);
cout << fixed << setprecision(4) << sqrt(double(xd * xd + yd * yd));
}
Answer the question
In order to leave comments, you need to log in
access violation clearly indicates a problem with memory.
The only controversial place is the input. Maybe there are extra line breaks or crooked tests in the file (some number is missing). Your program looks like it will try to access in this case and fail N[1]
because there are only 1 or 0 numbers in the string.
Rewrite the input. Make a class that stores the current array of tokens. When requesting the next one, it returns the next one, if there are no more tokens, it reads another line until there are tokens in it.
Well or read stupidly functions of reading of numbers. In this problem, the input file is small - you can safely read one number at a time, even if it will be slower.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question