Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя 4T-Shirt

Автор 4T-Shirt, 13 лет назад, По-английски
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;
}


Теги re, spoj
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Error in row:
const int rowMax = 1234;
const int colMax = 5678;

and initialize in cycle
for (int i=1;i<=colMax;i++)
Edit: =>
const int rowMax = 1240;
const int colMax = 5680;


And result?
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    Ай-я-яй... Какой нихароший совет... ;-)

    Т.е. если мы подозреваем выход за пределы массива, то надо просто на глазок увеличить размеры этих самых массивов? Т.е. ошибка останется, но перестанет воспроизводиться?

    Пожалуй я готов это допустить если задача решается во время контеста и осталось совсем мало времени. В других же случаях это методически неграмотно по-моему... %)

    Правда комментарий по-моему на русском сделан.

    У этого товарища по-моему массивы сделаны до colMax+1 и rowMax+1, так что ошибка не в этой строчке (хотя лучше б он сам подебажил и нашёл в какой, разбираться лень)...
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Could you yourself debug your program? Most common mistake of all people who write here "I have C++ program which works wrong or gives runtime error" is "array index out of bounds".

If you want to master programming - you could not avoid learning how to search for your own mistakes in your own works.
  • 13 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Submit this and meditate a little.

    (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.
                else if (ch[0] == 'Q')
                {
                    sscanf(ch,"%c %d %d",&c,&a,&b);
                    int row = 1,col=1;
                    while (mat[row][0] != a ) {
                        if (row > rowMax) {
                            printf("I am an idiotic bug\n");
                            return 1;
                        } // if
                        row++;
                    } // while
                    while (mat[0][col] != b ) {
                        if (col > colMax) {
                            printf("I am an wonderful feature\n");
                            return 2;
                        } // if
                        col++;
                    } // while
                    printf("%d\n",mat[row][col]);
                }