Hi! This is a problem on SPOJ: http://www.spoj.pl/problems/ACS/
Could someone please tell me why my program is always "runtime error ".
Here is my code:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
const int rowMax = 1234;
const int colMax = 5678;
int mat[rowMax+1][colMax+1];
char ch[100];
char c;
int a,b;
int main()
{
int cnt = 0;
for (int i=1;i<=colMax;i++) mat[0][i] = i;
for (int i=1;i<=rowMax;i++) mat[i][0] = i;
for (int i=1;i<=rowMax;i++)
for (int j=1;j<=colMax;j++)
{
mat[i][j] = cnt + 1;
cnt ++;
}
while (gets(ch))
{
if (strlen(ch)==0)
{
}
else if (ch[0]=='R')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
mat[a][0] = b;mat[b][0] = a;
}
else if (ch[0] == 'C')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
mat[0][a] = b;mat[0][b] = a;
}
else if (ch[0] == 'Q')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
int row = 1,col=1;
while (mat[row][0] != a ) row++;
while (mat[0][col] != b ) col++;
printf("%d\n",mat[row][col]);
}
else if (ch[0] == 'W')
{
sscanf(ch,"%c %d",&c,&a);
int row = (a-1)/colMax + 1;
int col = a - (row-1)*colMax;
printf("%d %d\n",mat[row][0],mat[0][col]);
}
}
return 0;
}
Could someone please tell me why my program is always "runtime error ".
Here is my code:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
const int rowMax = 1234;
const int colMax = 5678;
int mat[rowMax+1][colMax+1];
char ch[100];
char c;
int a,b;
int main()
{
int cnt = 0;
for (int i=1;i<=colMax;i++) mat[0][i] = i;
for (int i=1;i<=rowMax;i++) mat[i][0] = i;
for (int i=1;i<=rowMax;i++)
for (int j=1;j<=colMax;j++)
{
mat[i][j] = cnt + 1;
cnt ++;
}
while (gets(ch))
{
if (strlen(ch)==0)
{
}
else if (ch[0]=='R')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
mat[a][0] = b;mat[b][0] = a;
}
else if (ch[0] == 'C')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
mat[0][a] = b;mat[0][b] = a;
}
else if (ch[0] == 'Q')
{
sscanf(ch,"%c %d %d",&c,&a,&b);
int row = 1,col=1;
while (mat[row][0] != a ) row++;
while (mat[0][col] != b ) col++;
printf("%d\n",mat[row][col]);
}
else if (ch[0] == 'W')
{
sscanf(ch,"%c %d",&c,&a);
int row = (a-1)/colMax + 1;
int col = a - (row-1)*colMax;
printf("%d %d\n",mat[row][0],mat[0][col]);
}
}
return 0;
}
If you want to master programming - you could not avoid learning how to search for your own mistakes in your own works.
(UPD: I mean that with this check you will be getting non-zero return code instead of run-time error, which shows that check is working and your program's logic is not correct. You can also simply comment out all this branch which works on 'Q' and see that you are getting WA - which also shows that you have mistake in this block, but not as precisely.)
By the way I think it is very wrong idea each time to search throw an array to find required number. You should just keep array of current numbers of lines which were swapped.