Talk about line breaks on Windows/Linux/macOS from two failed test points on 1407: Stupid Monkey

Question: One pass 1407: The 8th and 10th test points of the Stupid Monkey (Logu P1125 Stupid Monkey) can not pass, but the same code is OK on Logu.

#include <bits/stdc++.h>
using namespace std;


int main() {
   char ch[110];
   gets(ch);
   printf("%d.", strlen(ch));
   int n = strlen(ch), max = 0, min = 100;
   for (int i = 0; i < n; i++) {
      int t = 0;
      //if( 0x0d == ch[i]) continue; // \r(RETURN)
      for (int j = 0; j < n; j++)
         if (ch[i] == ch[j]) t++;

      if (t > max) max = t;
      if (t < min) min = t;
   }
   int x = max - min;
   if (x >= 2) {
      int flag = 0;
      for (int i = 2; i <= sqrt(x); i++) {
         if (x % i == 0) {
            flag = 1;
            break;
         }
      }
      if (flag == 0)  cout << "Lucky Word" << endl << x;
      else            cout << "No Answer"  << endl << '0';
   } else             cout << "No Answer"  << endl << 0;
   
   return 0;
}

The code above passes after replacing gets with scanf
 

#include <bits/stdc++.h>
using namespace std;
// lg: P1125 Stupid Monkey
inline bool is_small_alpha(char x) { return 'a' <= x && 'z' >= x; }

int main() {
   char ch[110];
   //gets(ch);
   scanf("%s", ch);
   int n = strlen(ch), max = 0, min = 100;
   for (int i = 0; i < n; i++) {
      int t = 0;
      //if( 0x0d == ch[i]) continue; // \r(RETURN)
      for (int j = 0; j < n; j++)
         if (ch[i] == ch[j]) t++;

      if (t > max) max = t;
      if (t < min) min = t;
   }
   int x = max - min;
   if (x >= 2) {
      int flag = 0;
      for (int i = 2; i <= sqrt(x); i++) {
         if (x % i == 0) {
            flag = 1;
            break;
         }
      }
      if (flag == 0)  cout << "Lucky Word" << endl << x;
      else            cout << "No Answer"  << endl << '0';
   } else             cout << "No Answer"  << endl << 0;
   
   return 0;
}

After adding filter return, one copy will also be AC.

Add a line: if (0x0d == ch[i]) continue; // \r (RETURN)

#include <bits/stdc++.h>
using namespace std;


int main() {
   char ch[110];
   gets(ch);
   printf("%d.", strlen(ch));
   int n = strlen(ch), max = 0, min = 100;
   for (int i = 0; i < n; i++) {
      int t = 0;
      if( 0x0d == ch[i]) continue; // \r(RETURN) 
      for (int j = 0; j < n; j++)
         if (ch[i] == ch[j]) t++;

      if (t > max) max = t;
      if (t < min) min = t;
   }
   int x = max - min;
   if (x >= 2) {
      int flag = 0;
      for (int i = 2; i <= sqrt(x); i++) {
         if (x % i == 0) {
            flag = 1;
            break;
         }
      }
      if (flag == 0)  cout << "Lucky Word" << endl << x;
      else            cout << "No Answer"  << endl << '0';
   } else             cout << "No Answer"  << endl << 0;
   
   return 0;
}

 

An analysis of the reasons why a Book failed:

Filter out the return and OK.It can be inferred that a pass-through test data has been generated or saved in the Windows system, and there is an extra carriage return character.

gets end at the end of a line or at the end of a text file.Line end is different in different systems

It is not due to "mixing":

Alternatively, change gets to cin, and make some corresponding changes, or AC.
cin, cout are of the C++ family.
gets, scanf, printf are of the C family.
Do not mix the two groups.

 

Line breaks in Windows/Linux/macOS systems

 

Line break'\n'and carriage return'\r'

 

(1) A line break is a new line -'\n'10 line break (newline)

(2) A carriage return is a return to the beginning of a line -'\r'13

So the carriage return characters we usually write files should be exactly called carriage return line breaks.

 

2. Application:

(1) In Microsoft's MS-DOS and Windows, the characters "Enter CR('\r')" and "Line Break LF('\n')" are used as line breaks;

(2) In a Windows system, each line ends with a carriage return + line break (CR+LF), i.e.'\r\n';

(3) In Unix systems, there is only a newline CR at the end of each line, that is,'\n';

(4) In a Mac system, each line ends with a return CR, or'\r'.

About Mac Line Break

Referenced from: https://blog.csdn.net/menc15/article/details/71731825?Depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

according to Wikipedia-Newline and Wikipedia-macOS

The Mac operating system before 2001, called Classic Mac OS, used r at the end of the line. The last version of this series of operating systems was Mac OS 9 released in 1999.

Starting with the release of MacOS 10.0 in March 2001, n, known as macOS or OS X, is used at the end of the line.Lines end with the same "\n" as Unix.

The line break under Windows is 0x0D 0x0A, as follows:

The line break under Linux is 0x0A, as follows:

The line break character in macOS 10.13.6 is 0x0A, as follows:

Quote

  1. Enter and Line Break in Linux/Windows/Mac OS
Reproduction CharlotteMen Last released in 2017-05-12 15:16:35 Reading 4378 Collections

https://blog.csdn.net/menc15/article/details/71731825?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

 

The problem took 5 or 6 hours today.

Tags: Windows Mac Linux Unix

Posted on Sun, 08 Mar 2020 09:19:37 -0700 by Groogy