First [show me the code] Problem Solving report (7-8)

Source: Internet
Author: User

[Back To Douban] http://site.douban.com/196781/widget/notes/12161495/note/259026439/

[Question] 7. suppose you cannot use the cout in C Language printf and C ++, or print in Python. Similarly, the output function that we can use is only printdigit (n, 0 <= n <= 9. This function can only print a single number. In this case, print any positive integer N using recursive programming.

[Idea] there is no such questionAlgorithmIt refers to the simple recursion. The tomato students think of embedded programming knowledge and give a general knowledge annotation, which is worth learning:

"In the embedded system development process, after the embedded hardware system is set up in the early stage, for convenience of subsequent software developmentLogOutput Function (mainly usedUARTOutput)DriverGenerally1 byteData output, orByteData Stream output. In this case, developers make a set of similarPrintfIn the development process, problems may occur easily.CodeJoinLogOutput,Bug ReproductionOutput major variable helpDebug."

[Code]

View code

 1 # Include <iostream> 2 # Include <cstdlib> 3   4   Using   Namespace  STD;  5   6   Void Printdigit ( Int  N ){  7       If (N> =0 & Amp; n <= 9  ){  8 Cout < N;  9   }  10   }  11   12   Void Printnum ( Int  N ){  13       If (N>9  ){  14 Printnum (N/ 10  );  15   }  16 Printdigit (N % 10  );  17   18   }  19   20   Int  Main7 () 21   {  22       Int N = 0  ;  23 Cin> N;  24   Printnum (N );  25       26 System ( "  Pause  "  );  27      Return   0  ;  28 }

[Question] 8. Known: If n is an odd number, it is equal to 1 in the Binary Expression of N/2 plus 1. Write a recursive method that returns the number N in binary representation of 1.

[Idea] It also examines recursion and bit operation knowledge. The key point is that the number of binary 1 of an even n is equal to the number of binary 1 of n/2. For more algorithms to calculate the number of one in a binary number, Go to page 155th of "the beauty of programming.

[Code] for this question, the tomato students gave the common young people code. The young people code and the 2B young people code are very colorful.

 1 # Include <stdio. h> 2 # Include <stdlib. h> 3   Int Cnt_bit1_nomal (unsigned Int ); /*  Ordinary youth  */  4   Int Cnt_bit1_recursion (unsigned Int ); /*  Literary Youth  */  5   Int Cnt_bit1_recursion_fuck (unsigned Int ); /*  2b youth  */ 6   Int Cnt_bit1_nomal_robust (unsigned Int ); /*  Skillful  */  7   8   Int  Main ()  9   {  10 Unsigned Int X = 7  ; 11       //  Printf ("% d \ n", sizeof (unsigned INT ));  12 Printf ( "  % D bit1: % d \ n  "  , X, cnt_bit1_nomal (x ));  13 Printf ( "  % D bit1: % d robustness \ n  "  , X, cnt_bit1_nomal_robust (x ));  14 Printf ( " % D bit1: % d recursion \ n  "  , X, cnt_bit1_recursion (x ));  15 Printf ( "  % D bit1: % d recursion fuck \ n  "  , X, cnt_bit1_recursion_fuck (x ));  16 X = 18  ;  17 Printf ( "  % D bit1: % d \ n  " , X, cnt_bit1_nomal (x ));  18 Printf ( "  % D bit1: % d robustness \ n  "  , X, cnt_bit1_nomal_robust (x ));  19 Printf ( "  % D bit1: % d recursion \ n  "  , X, cnt_bit1_recursion (x ));  20 Printf ( "  % D bit1: % d recursion fuck \ n "  , X, cnt_bit1_recursion_fuck (x ));  21       Return   0  ;  22   }  23   24   25   /*  General cyclic Solution  */  26   Int Cnt_bit1_nomal (unsigned Int X)  27   {  28       Int Result = 0  ;  29       While (X! = 0  )  30   {  31 Result + = (X & 0x1  );  32 X> = 1  ;  33   }  34       Return  (Result );  35   }  36   37   38   /*  Robustness  */  39   Int Cnt_bit1_nomal_robust (unsigned Int  X)  40   {  41       Int I, result = 0  ;  42       For (I = 0 ; (I <( Sizeof (X )* 8 ) & (X! = 0 ); I ++) /* Adapt to various data types and reduce the chance of endless loops when variables are rewritten.  */  43   {  44 Result + = (X & 0x1  );  45 X> = 1  ;  46   }  47       Return  (Result );  48  }  49   50   51   /*  The code for writing such XXX will be cracked.  */  52   Int Cnt_bit1_recursion_fuck (unsigned Int  X)  53   {  54       Return (X? (X & 0x1 + Cnt_bit1_recursion_fuck (x>1 )): 0  );  55   }  56   57   58   /*  This solution is the same as above, but it is more like a human...  */  59   Int Cnt_bit1_recursion (unsigned Int  X)  60   { 61       If  (X)  62   {  63           Return (X & 0x1 ) + Cnt_bit1_recursion (x> 1  ));  64   }  65       Else  66   {  67          Return ( 0  );  68   }  69 }

 

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.