C Language Programming Questions 002

Source: Internet
Author: User

Give two integers, L and R, where l<=a<=b<=r, and then find the maximum number of a^b values. Which 1<=l<=r<=1000.

For example L = 1; R = 3;

L 0001

R 0011

There is also 0010 in the middle of LR, where the maximum value is 0001 ^ 0010 = 0011; the output is 2.

Write a function to calculate the number;

I wrote one, but did not pass the final test, only 12 points, total 20 points, do not know where to improve?

1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <stdlib.h>5#include <assert.h>6 #defineMAXSIZE (1000)7 /*8 the maximum XOR value for two numbers. 9  */Ten  One  A /* -  *  -  */ the intMaxxor (intLintR) - { -     //ASSERT use method: Put in function start, detect the legality of function parameter -ASSERT (L >=1); +ASSERT (R <=MAXSIZE); -ASSERT (L <=R); +      A     //Number of Bits at     intLENGTHL =sizeof(l) *8; -     intLengthr =sizeof(r) *8; -     //Maximum Value -     intMax =0; -     intLen = (l+r)/2; -     //flag Bit in     intflag0, Flag1; -     //Two for loops traverse all combinations of Ken to      for(inti = l; I <= Len; i++) +     { -         //printf ("i=%d", I); the  *            for(intj = r; J >= Len; j--) $           {Panax Notoginseng                  //printf ("j=%d\n", j); -                the                  //Initialize various values +LENGTHL =sizeof(l) *8; ALengthr =sizeof(r) *8; theFLAG0 =0; +Flag1 =0; -                $                  //Judging from the highest bit $                   while(LENGTHL-->0&& Lengthr-->0) -                  { -                      if(i>>lengthl ^ j>>lengthr)//XOR is 1, record the                      { -                         //printf ("flag1=%d\n", Flag1);WuyiFlag1 + +;// the                      } -                      Else Wu                      { -                          //printf ("flag0=%d\n", flag0); AboutFLAG0 + +; $                          if(Flag1! =0)//if the XOR result first appears after 1, 0 is definitely not the maximum value, exiting the while loop -                          { -printf"break\n"); -                               Break;//exit the While loop and remove a number A                          } +                      } the                      //output maximum, as long as the loop is not exited prematurely, so that it is possible to be the maximum value -                      //Compare all eligible values and take the maximum value $                      if((flag0 + flag1) = =sizeof(l) *8) the                      { the                          if(Max < (i ^j)) the                          { theMax = (i ^j); -                             //printf ("Output Max =%d\n", Max); in                              the                          } the                      } About                  } the                the                the           } +       -     } the     //printf ("Output maximum: \ n");Bayi  the     returnMax; the } -  -  the  the  the intMain () the { -     intRes; the     int_l; thescanf"%d", &_l); the     94     int_r; thescanf"%d", &_r); the      theres =Maxxor (_l, _r);98printf"%d", res); About      -     return 0;101}

C Language Programming Questions 002

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.