需要打出的是10000个随机数0和1,在进行0多少个1多少个的汇总111000001000001111100010011111.。。(一行70个随机数)
..................................................................................
汇总:
1:3 * 0:5 * 1:1 * 0:5.....................(一行10个汇总,所表达的意思是第一个1有三个相同的,第二个零有5个相同的)
应该怎么写啊
..................................................................................
汇总:
1:3 * 0:5 * 1:1 * 0:5.....................(一行10个汇总,所表达的意思是第一个1有三个相同的,第二个零有5个相同的)
应该怎么写啊
int i=((int)(Math.random()*10))%2;
通过与2的余数来确定是1还是0,
要10000个就加个循环
先把10000个存到一个集合里,然后遍历集合,再声明全局变量,一个记录1的变量=0,一个记录0的变量=0,
在if(集合遍历数==1)时 {
if(变量0!=0){
输出“0:”+变量0;变量0=0;}
变量1做累加;
}
当集合遍历数==0时,
if(变量1!=0){
输出“1:”+变量1;变量1=0;
}
变量0做累加。
大概思路这样了,呵呵可能有些问题,具体代码我也没写,你自己动动手吧。
1 转变成String: number=“0000010111011011010101010111111”
2 以0分割:arrayZero[]=number.split("0");
3 以1分割:arrayOne[]=number.split("1");然后获得生成的这两个array里面元素的长度就可以了,例如:arrayZero[0].length();
char current = 0;
int count = 0;
int control = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] == current)
count++;
else {
if (count != 0) {
System.out.printf("%c:%d", current, count);
control = (control + 1) % 7;
System.out.print(control == 0 || i == ch.length - 1 ? "\n" : " * ");
}
current = ch[i];
count = 1;
}
}count("00100000111110111010101010101001");
结果:
0:2 * 1:1 * 0:5 * 1:5 * 0:1 * 1:3 * 0:1
1:1 * 0:1 * 1:1 * 0:1 * 1:1 * 0:1 * 1:1
0:1 * 1:1 * 0:1 * 1:1 * 0:2
String numLine="00100000111110111010101010101001";
count(numLine);
}
public static void count(String numLine){
if(numLine==null||numLine.equals("")) return;
List<String> result=new ArrayList<String>();
char[] chrs=numLine.toCharArray();
char chr,curChar=chrs[0];
int count=1;
for(int i=1,len=chrs.length;i<len;i++){
chr=chrs[i];
if(curChar!=chr||i==len-1){
result.add(curChar+"*"+count);
curChar=chrs[i];
count=0;
}
count++;
}
int i=1;
for(String str:result){
System.out.print(str+"\t"+(i%10==0?"\n":""));
i++;
}
}
public class Test { public static void count(String str) {
int pre = 0;
int count = 0;
int len = str.length();
for (int i = 0; i < len; ++i) {
int ii = Integer.parseInt(String.valueOf(str.charAt(i)));
int tmp = ii & 1;
if (tmp == pre) {
++count;
} else {
if (count != 0 && i != (len - 1))
System.out.print(pre + ":" + count + " * ");
if (i == len - 1)
System.out.print(pre + ":" + count);
count = 1;
}
pre = tmp;
}
} public static void main(String[] args) {
Test.count("111000001000001111100010011111");
}}
public class Test { public static void count(String str) {
int pre = 0;
int count = 0; int len = str.length();
// System.out.print("len= " + len);
for (int i = 0; i < len; ++i) {
int ii = Integer.parseInt(String.valueOf(str.charAt(i)));
int tmp = ii & 1;
if (tmp == pre) {
++count;
} else {
if (count != 0)
System.out.print(pre + ":" + count + " * "); count = 1;
}
pre = tmp;
// System.out.println("i = " + i);
}
System.out.print(pre + ":" + count);
} public static void main(String[] args) {
Test.count("111000001000001111100010011111");
}}结果是:
1:3 * 0:5 * 1:1 * 0:5 * 1:5 * 0:3 * 1:1 * 0:2 * 1:5
char current = 0;
int count = 0;
int control = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] == current)
count++;
else {
if (count != 0) {
System.out.printf("%c:%d", current, count);
control = (control + 1) % 10;
System.out.print(control == 0 ? "\n" : " * ");
}
current = ch[i];
count = 1;
}
if (i == ch.length - 1)
System.out.printf("%c:%d", current, count);
}
}
StringBuffer sb = new StringBuffer();
int length = alphabet.length;
for (int i = 0; i < n; i++) {
int random = (int)(Math.random() * length);
sb.append(alphabet[random]);
}
return sb.toString();
}这个是生成函数,参数n是个数,alphabet是字母表,要生成只有0和1的话char[] alphabet = {'0', '1'};就可以自己多动手练练吧,这又不是什么很难的东西
这样就是生成一个长度为10000的0和1组成的字符串
trimStart('1')
来回交替
计算长度,直到长度为0