Blue Bridge Cup Past Zhenti Ends 1


The code below the elimination of the trailing 1 turns the rightmost consecutive 1s in the binary representation of an integer into 0.
If the last digit is 0, the original number remains unchanged.
If the test data in the code is used, it should output:
Input : 000000000000001100111
Output : 000000000000001100000
Input : 000000000000000001100
Output : 000000000000000001100

#include<STdio.h>
#include<String.h>
int main()
{
    
    
	char str[100];
	gets(str);
	int i,j,cns=0,xb=1;
	cns=strlen(str);
	cns=cns-1;
	for(i=cns;i>=0;i--)
	{
    
    
		if(str[cns]=='0')
		{
    
    
			puts(str);
			break;
		}
		  if(xb)
	     	{
    
    
		        for(j=i;j>=0;j--)
		        {
    
    
                      if(str[j]=='1')
                      {
    
    
					  str[j]='0';
				       
					 }
					 else break;
		        }  
		        xb--;
	        }
	}
   puts(str);
   return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_46232829/article/details/107297696