Basic exercise of "Blue Bridge Cup": 01 string, Blue Bridge string
Problem description
For a 01 string with a length of 5 digits, each digit may be 0 or 1, a total of 32 possibilities. The first few of them are:
00000
00001
00010
00011
00100
Output these 32 01 strings in ascending order.
The input format is not entered. The output format outputs 32 rows. Each row has a 01 string of 5 in ascending order. Sample output 00000
00001
00010
00011
<The following part is omitted> tips: This question reminds me of the idea of using an integer array to store large numbers. The idea is to calculate the first element each time, and then adjust it. If each element in the array does not overflow, use while.
1 # include <stdio. h> 2 int main (void) 3 {4 int a [5] = {0}; 5 int temp; 6 int times; 7 int I; 8 printf ("00000 \ n"); 9 for (times = 1; times <= 31; times ++) 10 {11 I = 0; 12 a [I] + = 1; 13 while (a [I]> = 2) 14 {15 temp = a [I]/2; 16 a [I] = a [I] % 2; 17 I ++; 18 a [I] = a [I] + temp; 19} 20 for (I = 4; i> = 0; I --) 21 {22 printf ("% d", a [I]); 23} 24 printf ("\ n"); 25}
Return 0; 26}
Code: