Input 1: Output 0; input 0: Output 1

Source: Internet
Author: User
Input 1: Output 0; input 0: Output 1

I had an interview that day. I encountered such a problem: input 0, output 1, and input 1, and output 0. At that time, I came up with the first three methods. I was wondering if there were any other solutions.

1. The most common if-else solution comes into my mind first. Of course, there are some silly questions about HR. Is it true if-Else?

  1: #include <iostream>
  2: 
  3: using namespace std;
  4: int main()
  5: {
  6:   int input;
  7:   cin >> input;
  8:   if (1 == input)
  9:     cout << 0;
 10:   else if (0 == input)
 11:     cout << 1;
 12:   else 
 13:     cout << "invalid input";
 14:   cout << endl;
 15: }

2. Table-driven, this is also an instant mind, space for time.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: int main()
  6: {
  7:   int invert_table[] = { 1, 0};
  8:   int input;
  9:   
 10:   cin >> input;
 11:   //Well, Test the input if the valid index!!!
 12:   //I ignored & just use the assert macro
 13:   assert(input==0 || input ==1);
 14:   cout << invert_table[input] << endl;
 15: }

3. bitwise operations are easy to think of. Use a proper bitwise operator and the corresponding bit mask operation.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: int main()
  6: {
  7:   int mask_num = 0x1;
  8:   int input;
  9:   cin >> input;
 10:   assert(0==input || 1 == input);
 11:   cout << (input^mask_num) << endl;
 12: }

Come back and think about whether there are other methods. As soon as the result is put, I am too lazy!

The purpose of thinking is to use the minimum space and time possible. Obviously, the input and output are stored in byte (the above Code uses int; well, I am not harsh enough to use 1 bit), and the operations are output directly as far as possible, that is, use less commands or use less time-consuming commands.

  1. If-else judgment is obviously not desirable, and similar methods such as switch are not available.
  2. Table driven seems to consume a small amount of space and has a large proportion of data! Not available.
  3. Bitwise operations take up an additional 1 byte, and then pass the XOR operation. It seems that it is OK, but there are still many unnecessary operations.

This problem is very simple. To put it down, the difference between 1 and 0 is that the smallest bit is different. The ideal method is:Input to the smallest bit, and then the smallest bit is reversed.

Another thought: in fact, the fastest thing is to know the answer between compilers, Meta programming. Unfortunately, I know very little about this, and the user input is 1 or 0, which cannot be known during compilation.

In fact, the user input 0 or 1 can be considered as a probability event. Theoretically, the probability of 0 and 1 is 1/2 (ignore other cases), but it is not. This road is a little far away.

Another thought: if 1 and 0 are another object and inherit an abstract base class together, isn't it good to dynamically bind the output result based on them, think too far.

 

Still on the right track, I have always regarded 1 and 0 as "Numbers ". In fact, they are also true and false. From this perspective, there is also a better solution.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: 
  6: int main()
  7: {
  8:   bool input;
  9:   cin >> input;
 10:   cout << !input << endl;
 11: }

This last method was not found at the time. It was a bit crazy because it was a simple method and the fault tolerance was good (of course, the fault tolerance here is also a kind of error !)

I firmly believe that there must be other methods, but I haven't thought of them yet!

From: http://www.cnblogs.com/painful/archive/2011/08/16/2140782.html

I had an interview that day. I encountered such a problem: input 0, output 1, and input 1, and output 0. At that time, I came up with the first three methods. I was wondering if there were any other solutions.

1. The most common if-else solution comes into my mind first. Of course, there are some silly questions about HR. Is it true if-Else?

  1: #include <iostream>
  2: 
  3: using namespace std;
  4: int main()
  5: {
  6:   int input;
  7:   cin >> input;
  8:   if (1 == input)
  9:     cout << 0;
 10:   else if (0 == input)
 11:     cout << 1;
 12:   else 
 13:     cout << "invalid input";
 14:   cout << endl;
 15: }

2. Table-driven, this is also an instant mind, space for time.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: int main()
  6: {
  7:   int invert_table[] = { 1, 0};
  8:   int input;
  9:   
 10:   cin >> input;
 11:   //Well, Test the input if the valid index!!!
 12:   //I ignored & just use the assert macro
 13:   assert(input==0 || input ==1);
 14:   cout << invert_table[input] << endl;
 15: }

3. bitwise operations are easy to think of. Use a proper bitwise operator and the corresponding bit mask operation.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: int main()
  6: {
  7:   int mask_num = 0x1;
  8:   int input;
  9:   cin >> input;
 10:   assert(0==input || 1 == input);
 11:   cout << (input^mask_num) << endl;
 12: }

Come back and think about whether there are other methods. As soon as the result is put, I am too lazy!

The purpose of thinking is to use the minimum space and time possible. Obviously, the input and output are stored in byte (the above Code uses int; well, I am not harsh enough to use 1 bit), and the operations are output directly as far as possible, that is, use less commands or use less time-consuming commands.

  1. If-else judgment is obviously not desirable, and similar methods such as switch are not available.
  2. Table driven seems to consume a small amount of space and has a large proportion of data! Not available.
  3. Bitwise operations take up an additional 1 byte, and then pass the XOR operation. It seems that it is OK, but there are still many unnecessary operations.

This problem is very simple. To put it down, the difference between 1 and 0 is that the smallest bit is different. The ideal method is:Input to the smallest bit, and then the smallest bit is reversed.

Another thought: in fact, the fastest thing is to know the answer between compilers, Meta programming. Unfortunately, I know very little about this, and the user input is 1 or 0, which cannot be known during compilation.

In fact, the user input 0 or 1 can be considered as a probability event. Theoretically, the probability of 0 and 1 is 1/2 (ignore other cases), but it is not. This road is a little far away.

Another thought: if 1 and 0 are another object and inherit an abstract base class together, isn't it good to dynamically bind the output result based on them, think too far.

 

Still on the right track, I have always regarded 1 and 0 as "Numbers ". In fact, they are also true and false. From this perspective, there is also a better solution.

  1: #include <iostream>
  2: #include <assert.h>
  3: 
  4: using namespace std;
  5: 
  6: int main()
  7: {
  8:   bool input;
  9:   cin >> input;
 10:   cout << !input << endl;
 11: }

This last method was not found at the time. It was a bit crazy because it was a simple method and the fault tolerance was good (of course, the fault tolerance here is also a kind of error !)

I firmly believe that there must be other methods, but I haven't thought of them yet!

From: http://www.cnblogs.com/painful/archive/2011/08/16/2140782.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.