Can't use letter W or numbers, how to print "Hello World"?

The answerer prints "Hello World", but the code cannot use the letter W or any numbers .

(A total of 521 users tried to solve the problem, and a total of 232 succeeded in the challenge within the recommended time without receiving a prompt from the virtual interviewer.)

Among them, only 30% of the answerers are junior (0-2 years experience) developers, and 85% of the answerers who did not solve the problem on time are also junior developers. This also means that the challenge is hard for novices, which also sounds like a good way to check if a candidate is junior or advanced.

The platform selected several interesting results to share, let’s take a look below!

1. The most common and simplest: use the %c specifier

Reminder : char 87 in ascii is W

1printf("Hello %corld", ++*(char[]){"V"});
2printf("Hello %corld", *"V" | *"A");
3printf("Hello %corld", *"*" + *"-");
4char c = c^c++; 
5
6c=(c<<(c+c+c+c+c+c))+(c<<(c+c+c+c))+(c<<(c+c))+(c<<c)+c;
7printf("Hello %corld",c);
8printf("Hello %corld", EUSERS);

EUSERS is indeed 87:

This solution requires adding a specific #include, so probably not the shortest code.

 2. Cool C++ solution

1char c = (sizeof(bool)+sizeof(short)) * (sizeof(long) * sizeof(float) - sizeof(char) - sizeof(short));
2cout << "Hello " << c << "orld";

It's worth mentioning that the solution is saved as multiple steps, so it's possible to see how the solver proceeded, not just his final solution.

3. Cool Python solution

1from googletrans import Translator
2text = 'Hola Mundo!'
3translator = Translator()
4print(translator.translate(text).text)
5
6import hello
7# _hellol is a known library that prints hello world. Problem solved.

4. The "encrypt" method in C and C++

1char str[] = "Obkkh'Phukc\n";
2char strPtr = str; while (strPtr != '\n') *strPtr++ ^= '\a';
3printf("%s", str);

5. The same method, in C++

1std::string str = "V_xyz";
2char v_x;
3for(auto i:str){ v_x=i; break; }
4v_x++;
5std::cout<<"Hello "<<v_x<<"orld";

6. Cool cheating scheme

1system("echo \"echo Hello World\" > /usr/bin/gcc");

The answerer found the challenge to use gcc at /usr/bin/gcc to compile the code, so replaced it with "echo Hello World", super nice!

Finally, do you have any other unique solutions? You can try to challenge yourself, maybe you can get new coding ideas and inspiration in such a challenge~


Programming Exchange Official Account: Qiniu Programming

Guess you like

Origin blog.csdn.net/weixin_55751709/article/details/129441594