Solved problems on acmp.ru , and in the simplest problem , where you just have to count and print a number, there is a solution that is half as long as my rating (only 35 sim.)! Is it cheating or magic?

Well, how can you come up with a solution shorter than that?

#include <iostream> int main() { int a; std::cin >> a; std::cout << a; } // 59 символов 

On this site the best solutions are those that are the shortest.

  • Actually, this solution does not match the task. There you have to open the input file named INPUT.TXT , then the output file ... - VTT
  • @VTT There are two ways - either read and output to a file, or read and output to the console. - ʞɔᴉN ɹǝꓥ
  • one
    You can not worry, the ability to reduce the code does not correlate with the professionalism of the developer. As a rule, they try to write clean and readable code that is easy to understand and maintain. For example, your code reads well, 3 lines - 3 actions, created a variable (it would be nice to initialize at the same time;), thought there was something, it brought something back. And this is std::cout << std::cin.rdbuf(); already read harder, because There are two actions on the same line, and this code is unlikely to occur on a real project. - goldstar_labs
  • one
    In most cases, solutions to such problems are written not in real languages ​​(C or C ++, for example), but on "surzhik", implemented by specific compilers in default configurations. For example, in your version, you can try to omit the return type of an int from the main declaration. This is forbidden in both C and C ++, but GCC "misses." And now the code has become shorter ... - AnT
  • one
    There is a link to the article of the author of the shortest solution on the subject of code reduction: iqbotan.blogspot.com/2012/12/c-acmp.html . I do not know if there is an answer in it. - AnT

4 answers 4

Since pedantic "standardization" and "certainty of behavior" is not required with the code, by tying up the features of the GCC compiler in its default configuration, you can reduce the code to

 main(){char*gets(),b[9];puts(gets(b));} 

http://coliru.stacked-crooked.com/a/bf02104f47ad8b3c

(based on the answer @ vegorov). Bearing in mind, however, that the gets function in the standard library is formally no more.

It can even be very rough, since the length of the input string does not exceed 3 conditionally (assuming that leading zeros in the record of numbers are not allowed), and the type int in GCC has size 4

 main(){int*gets(),b;puts(gets(&b));} 

http://coliru.stacked-crooked.com/a/c64f32776b7fa150

and

 b;*gets();main(){puts(gets(&b));} 

http://coliru.stacked-crooked.com/a/e92785b2be0324a6

This is already 33 characters.


And on a 32-bit platform (i.e., on a platform where the pointer size coincides with the size of the int type), you can even do without the gets declaration, although this will be quite a rude, impudent and dirty hack

 b;main(){puts(gets(&b));} 

Unfortunately, I did not find online GCC compilers with libraries for -m32 . Only https://godbolt.org/z/j3Ye9u .

However, if the variable b appears to be located in the area of ​​the 64-bit address space where the upper 32 bits of the address are zero, then this code will most likely work on the 64-bit X86 platform: http: //coliru.stacked-crooked. com / a / b70346370d1f08f6

  • Anyway, the error comes out when I try to run. - ʞɔᴉN ɹǝꓥ
  • i.stack.imgur.com/LLX4Y.png - ʞɔᴉN ɹǝꓥ
  • @Ver Nick: How exactly to run? coliru.stacked-crooked.com/a/bf02104f47ad8b3c - AnT
  • one
    @Ver Nick: This “solution” needs to be compiled in C mode, not in C ++ mode. - AnT
  • And, I understood everything, tomorrow I compile, and perhaps I will accept the answer. - ʞɔᴉN ɹǝꓥ
 #include <stdio.h> main() { char b[9]; puts(gets(b)); } 

48 characters. Followed the tips from the article C ++ code abbreviation for acmp.ru

  • And for what an array of charms? To iostream not to use? - ʞɔᴉN ɹǝꓥ
  • @VerNick Well, the type counted the string and output the string. Rather, to use gets / puts. - vegorov
  • Perhaps in those examples for 35 characters there are asma inserts or some other hint. - vegorov
  • The task was about int . But it even shortens the code :) - ʞɔᴉN ɹǝꓥ
  • @Ver Nick: The condition says nothing about reading data as an int . In fact, all that the condition gives us is that the length of the sequence does not exceed 3 characters. Of course, the picky reader may ask: can such a number be written as 0000042 ... - AnT

Here is a little shorter.

 #include <iostream> int main() { std::cout << std::cin.rdbuf(); } 

The radical option on C89:

 main(){system("cp INPUT.TXT OUTPUT.TXT");} 

online compiler

  • and it turned out exactly 2 times shorter) - Aqua
  • @SeeSharp I have 59 characters, he has 57. It seems that two times shorter :) - ʞɔᴉN ɹǝꓥ
  • @VerNick Well, wait, as far as I know. #include <iostream> int main (){} thing is mandatory And the fact that inside and outside, this is work. And you say, the creative part is 2 times more than that of VTT 2/2 = 1. - Aqua
  • @SeeSharp The shortest solution is 35 characters. I'm sure C ++ has secrets :) - ʞɔᴉN ɹǝꓥ
  • one
    @VTT if the server on Windows can be reduced INPUT.TXT to I~1 some - pavel

C and POSIX read / write

  avp@avp-ubu1:hashcode$ cat golf.c main(){char s[4]; write(1, s, read(0, s, 4));} avp@avp-ubu1:hashcode$ wc golf.c 1 7 47 golf.c avp@avp-ubu1:hashcode$ gcc golf.c -Wno-implicit-function-declaration -Wno-implicit-int avp@avp-ubu1:hashcode$ echo 123 | ./a.out 123 avp@avp-ubu1:hashcode$ 

With the loss of readability is easily reduced to 40 characters:

main(){int s;write(1,&s,read(0,&s,4));}

Update

At the hint @pavel in the comments turned out 35 !!! (and that is characteristic, without warnings -), and the -w flag was enough

 avp@avp-ubu1:hashcode$ gcc golf.c -w avp@avp-ubu1:hashcode$ cat golf.c s; main(){write(1,&s,read(0,&s,4));} avp@avp-ubu1:hashcode$ 

Thank you, @pavel


b;main(){puts((read(0,&b,4),&b));}

using hints from all answers and comments - 34 (!) characters

Oddly enough, gcc -w compiles altogether without swearing, and ./a.out works -)

  • @VerNick, this is the formatter like this .... - avp
  • On the site checked? I #include <unistd.h> I do not see - vegorov
  • I checked. 58 characters, because #include <unistd.h> necessary. Right now, the keys are compilation - vegorov
  • one
    @vegorov, yes spit you on the crosses (switch to the crescent moon -)). By the way, if we restrict ourselves to copying one character, then main(){putchar(getchar());} generally fits into 27 - avp
  • one
    @vegorov, I understand. But I am not going to spend time on a thorough study of the generally more harmful for the practice of the language (C ++) (probably in his decision some kind of external (called before main() ) constructor nontrivially works) - avp