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

Автор Brown_Wolf, история, 8 месяцев назад, По-английски

I have seen in many people's code that they use a solve function instead of writing the logic in the main function. What advantages it has over writing the code directly in the main function?

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

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Brown_Wolf (previous revision, new revision, compare).

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

The use of the function solve() does not affect the running time of the program. But it makes the code more readable.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I do this too just because i prefer that my main function has shorter code and in problems with many test cases it is easier to bring the line of function inside the while loop of testcases,in general it depends on you whether you like modular or linear programming alhtough writing functions makes code more readable

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

it's make the code easier to read and pretty handy when dealing problem with multiple testcases

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

    In terms of short questions , Where i dont need to work with multiple testcases should i use solve () ??

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

      this is a personal preference of mine, where I typically set t to 1 and then comment out the cin line when working on problems that involve a single test case.

»
8 месяцев назад, # |
  Проголосовать: нравится -66 Проголосовать: не нравится
»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Allows for easier switching between single testcase and multiple testcase problems. For example:

void Main() {
    // your logic here...
}
int main() {
#ifdef MULTI
    int T;
    rd(T);
    while (T--)
#endif
        Main();
    return 0;
}
»
8 месяцев назад, # |
  Проголосовать: нравится +79 Проголосовать: не нравится

You can use return to easily and quickly exit the current testcase.

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

    By far the best reason. Imagine having something like if specific case, then continue the loop. And now you have a bunch of such cases. Your code would be pretty ugly and filled with just exiting the loop.

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

      Thanks Sir!!! If you don't mind Sir, Do you have any tutorial about this ??.. I actually don't know how to use it.

»
8 месяцев назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

You don't have to worry about using continue in the testcase loop when you're done printing the answer to stdout halfway to the code, you can just return after that.

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

    If you don't mind Sir, Do you have any tutorial about this ??.. I actually don't know how to use it.

»
8 месяцев назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

One important characteristic of high-quality code is orthogonality. (I'm not telling you to make your code more vertical, but more clearly.) If you want to write orthogonal code, it is often more efficient to encapsulate statements within functions, making debugging more convenient as well.

Additionally, the best part is that the "return;" statement can directly jump to the next test case without the need for control variables or goto-like statements.

Moreover, some programmers try to have fewer statements within their main function to make the logic clearer, specifically for their own reading purposes.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

for me , the reason I use solve() is because I can just return in the function whenever I want to and preceed to the next testcase , that makes it easy to exit a testcase , doing it in main would require multiple variables and ifs so it is just convenient