Блог пользователя Tornad0

Автор Tornad0, история, 9 лет назад, По-английски

A problems are simple, and I tried to submit once without testing, but I always failed to accept at once. Sometimes I get stuck in very simple mistakes. I feel that programming is so hard because of those mistakes, so I even don't have confidence to think about harder problems with advanced algorithm. Any advice please? Maybe I should change from C to another language? (But sometimes I really enjoy graspping the idea of hard problems. There are so many difficulties and exceptions for me to communicate with my computer.)

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

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +12 Проголосовать: не нравится

but I always failed to accept at once

That's because of inexperience. I used to get stupid bugs in the past. But after some practice, I got accustomed to the usual bugs that would occur in my code (like dividing by zero, integer overflow, off by one bugs, etc.). You just got to keep practicing. Spotting these trivial bugs will soon become second nature to you.

Anyway, (div 2) problem A usually don't have many corner cases (if any at all). The obvious things to look out for, though, would be extremely small input, extremely large input, empty sets, etc.

What I do though, is to read through my code once to see if it is intuitively correct. If it is clearly correct, I submit. Otherwise, I manually try a few cases. Worst case, if I am not confident of my solution, I would start writing a test case generator.

  • »
    »
    5 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится -7 Проголосовать: не нравится

    How do you generate test cases through a code ?

    • »
      »
      »
      5 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Do I really have to teach you that? All you need to know is how to generate random numbers. That's all. The rest is just fitting the numbers to the problem constraints.

      In case you were asking about strategy for testing, I usually tweak the bounds between small and large to make sure that extreme cases are covered. Of course, I will make sure to manually enter some extreme cases by hand because those are unlikely to be generated automatically.

      • »
        »
        »
        »
        5 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Yeah, we generate random numbers. But how do you know it always produces correct output?

        • »
          »
          »
          »
          »
          5 лет назад, # ^ |
            Проголосовать: нравится +5 Проголосовать: не нравится

          You write bruteforce solution as a checker.

        • »
          »
          »
          »
          »
          5 лет назад, # ^ |
            Проголосовать: нравится +1 Проголосовать: не нравится

          Well, you can almost always write a brute force program that searches the entire solution space. Although this program can not pass time limit, it is very unlikely to be wrong (unless you made some coding bug). Thus, you can be confident it will give you the correct answer to your test cases. If you are really paranoid, submit your brute force program, have it TLE (not WA). This should give you extra confidence in its correctness.