02字串2

来源:互联网 发布:混合高斯模型算法原理 编辑:程序博客网 时间:2024/05/16 10:17
/*问题描述  对于长度为6位的一个01串,每一位都可能是0或1,一共有64种可能。它的前几个是:  000000  000001  000010  000011  000100  有这些01串中,有一些01串的1的个数是奇数个,这部分01串的前几个是:  000001  000010  000100  000111  001000  001011  请按从小到大的顺序输出这些01串。输出格式  每行一个01串。*/public class Main9 {public static int item;public static void main(String[] args) {char[] str = { '0', '0', '0', '0', '0', '0' };String s;for (int i = 0; i < 63; i++) {s = format(str, 1);if (item % 2 == 0) {} else {System.out.println(s);}}}public static String format(char[] str, int n) {int flag = 0;item = 0;if (str[5] == '1') {flag = 1;str[5] = '0';} else {str[5] = '1';}if (flag == 1) {for (int i = 4; i >= 0; i--) {if (flag == 1) {if (str[i] == '0') {flag = 0;str[i] = '1';} else {str[i] = '0';flag = 1;}} else {break;}}}for (int i = 0; i < 6; i++) {if (str[i] == '1')item++;}return new String(str);}}

0 0
原创粉丝点击