发布网友
共5个回答
热心网友
唉~~,平常回答问题都要去对方空间看看 是不是 美女,不过今天心情好算你赚到了 = =!。下面是代码,但是字符 太多会有溢出问题,Integer装不下,也懒得 改了。
package com.gby.util;
public class Moniter {
/**
* 记录监视者当前的位置
*/
private int current;
/**
* 记录最大的位置信息
*/
private int maxLength;
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public int getMaxLength() {
return maxLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
}
package com.gby.test;
import java.util.List;
import com.gby.util.Moniter;
import com.gby.util.MoniterManager;
public class Sperater {
public static void main(String[] args)
{
String str = "bushibat";
MoniterManager manager = new MoniterManager();
for (int i = 1; i < str.length()-1; i++)
{
Moniter moniter = new Moniter();
moniter.setCurrent(1);
moniter.setMaxLength(str.length() - 1);
manager.clear();
manager.add(moniter);
for (int j = 0; j < i-1; j++)
{
Moniter tem = new Moniter();
tem.setCurrent(j+2);
tem.setMaxLength(str.length() - 1);
manager.add(tem);
}
int count = 1;
int s = i;
while(s > 0)
{
count = (count * (str.length()-s));
s--;
}
int t =i;
while(t > 0)
{
count = count / t;
t--;
}
for (int j = 0; j < count; j++)
{
List<Integer> arr = manager.toIntArray();
System.out.print(str.substring(0,arr.get(0)));
System.out.print("/");
for (int k = 0; k < arr.size()-1; k++)
{
System.out.print(str.substring(arr.get(k), arr.get(k+1)));
System.out.print("/");
}
System.out.print(str.substring(arr.get(arr.size() - 1)));
System.out.println();
manager.run();
}
}
}
}
package com.gby.util;
import java.util.ArrayList;
import java.util.List;
public class MoniterManager {
private List<Moniter> allMoniter = new ArrayList<Moniter>();
public List<Integer> run()
{
if (allMoniter.size() == 0 )
{
return new ArrayList<Integer>();
}
if (allMoniter.get(allMoniter.size()-1).getCurrent() >= allMoniter.get(allMoniter.size()-1).getMaxLength())
{
adjustLocation();
return toIntArray();
}
allMoniter.get(allMoniter.size()-1).setCurrent(allMoniter.get(allMoniter.size()-1).getCurrent() + 1);
return toIntArray();
}
public void adjustLocation()
{
for (int i = allMoniter.size()-1; i > 0; i--)
{
Moniter last =allMoniter.get(i);
Moniter pre = allMoniter.get(i-1);
Boolean justOK =false;
if (pre.getCurrent() + 2 <= last.getCurrent())
{
pre.setCurrent(pre.getCurrent() + 1);
for (int j = 0; j < allMoniter.size()-i; j++)
{
Moniter before = allMoniter.get(allMoniter.size() - j - 2);
Moniter after = allMoniter.get(allMoniter.size() - j - 1);
justOK = true;
after.setCurrent(before.getCurrent() + 1);
}
if (justOK)
{
break;
}
}
}
}
public List<Integer> toIntArray()
{
List<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < allMoniter.size(); i++)
{
arr.add(allMoniter.get(i).getCurrent());
}
return arr;
}
public void add(Moniter moniter)
{
allMoniter.add(moniter);
}
public void clear()
{
allMoniter.removeAll(allMoniter);
}
public String toString()
{
String str ="";
for (int i = 0; i < allMoniter.size(); i++)
{
str = str + allMoniter.get(i).getCurrent() + " ";
}
return str;
}
public Moniter getRunMoniter()
{
return allMoniter.get(allMoniter.size()-1);
}
}
运行结果:
b/ushibat
bu/shibat
bus/hibat
bush/ibat
bushi/bat
bushib/at
bushiba/t
b/u/shibat
b/us/hibat
b/ush/ibat
b/ushi/bat
b/ushib/at
b/ushiba/t
bu/s/hibat
bu/sh/ibat
bu/shi/bat
bu/shib/at
bu/shiba/t
bus/h/ibat
bus/hi/bat
bus/hib/at
bus/hiba/t
bush/i/bat
bush/ib/at
bush/iba/t
bushi/b/at
bushi/ba/t
bushib/a/t
b/u/s/hibat
b/u/sh/ibat
b/u/shi/bat
b/u/shib/at
b/u/shiba/t
b/us/h/ibat
b/us/hi/bat
b/us/hib/at
b/us/hiba/t
b/ush/i/bat
b/ush/ib/at
b/ush/iba/t
b/ushi/b/at
b/ushi/ba/t
b/ushib/a/t
bu/s/hiba/t
bu/sh/i/bat
bu/sh/ib/at
bu/sh/iba/t
bu/shi/b/at
bu/shi/ba/t
bu/shib/a/t
bus/h/iba/t
bus/hi/b/at
bus/hi/ba/t
bus/hib/a/t
bush/i/ba/t
bush/ib/a/t
bushi/b/a/t
bushi/b/a/t
bushi/b/a/t
bushi/b/a/t
bushi/b/a/t
bushi/b/a/t
bushi/b/a/t
b/u/s/h/ibat
b/u/s/hi/bat
b/u/s/hib/at
b/u/s/hiba/t
b/u/sh/i/bat
b/u/sh/ib/at
b/u/sh/iba/t
b/u/shi/b/at
b/u/shi/ba/t
b/u/shib/a/t
b/us/h/iba/t
b/us/hi/b/at
b/us/hi/ba/t
b/us/hib/a/t
b/ush/i/ba/t
b/ush/ib/a/t
b/ushi/b/a/t
bu/s/hib/a/t
bu/sh/i/ba/t
bu/sh/ib/a/t
bu/shi/b/a/t
bus/h/ib/a/t
bus/hi/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
bush/i/b/a/t
b/u/s/h/i/bat
b/u/s/h/ib/at
b/u/s/h/iba/t
b/u/s/hi/b/at
b/u/s/hi/ba/t
b/u/s/hib/a/t
b/u/sh/i/ba/t
b/u/sh/ib/a/t
b/u/shi/b/a/t
b/us/h/ib/a/t
b/us/hi/b/a/t
b/ush/i/b/a/t
bu/s/hi/b/a/t
bu/sh/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
bus/h/i/b/a/t
b/u/s/h/i/b/at
b/u/s/h/i/ba/t
b/u/s/h/ib/a/t
b/u/s/hi/b/a/t
b/u/sh/i/b/a/t
b/us/h/i/b/a/t
bu/s/h/i/b/a/t
Hello 运行结果如下:
H/ello
He/llo
Hel/lo
Hell/o
H/e/llo
H/el/lo
H/ell/o
He/l/lo
He/ll/o
Hel/l/o
H/e/l/lo
H/e/ll/o
H/el/l/o
He/l/l/o
热心网友
如下:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class StringSplit {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<List<String>> liststring = new ArrayList<List<String>>();
String str = "12345670";
int length = str.length();
if(length!=0)
{
for(int i=1;i<=length;i++){
List<String> list = new ArrayList<String>();
int beginIndex =0;
int endIndex = i;
for(int j=0;j<length;j++)
{
if(endIndex<=length){
list.add(str.substring(beginIndex, endIndex)+File.separator+str.substring(endIndex));
}else{
break;
}
//beginIndex = endIndex;
endIndex = endIndex+i;
}
liststring.add(list);
}
}
for(List<String> listArray : liststring)
{
String string ="";
for(String list : listArray)
{
string += list+"\t";
}
System.out.println(string);
}
}
}追问没有达到预期的目的啊。。。
热心网友
能力很有限,只能想到这些:
题目一
建立一个包含N个元素的数组,先让所有元素都等于1,从头到尾遍历并同时报数,报数为3的,将对应元素置0。然后反复遍历,直到数组中只剩一个或两个元素的值为1,那么这个(或这两个)元素的下标,就是最后的解。
或者用链表做:建立一个N个节点的链表,并按从1到N编号,遍历时,报数为3的,删除这个节点。最后只剩一个或两个节点时,这个(两个)节点的编号就是最后的解。
题目二
可以在一个窗口里面,排放一大堆按钮。落子的地方,改变按钮背景颜色。每落一子,判断下是否为五子连珠(可用以个二维数组同步记录落子情况,以便于判断是否出现了五子连珠)。追问没那么复杂,就是想把一个字符串拆开,找出所有的拆法,并且用集合追踪拆法
热心网友
有个思路:
byte[] getBytes() 用这个方法,获得单个的字符的数组,也就是如1234 会获得1 2 3 4 的数组。之后你用排列组合的方式来获得所有可能性的集合容器就ok了。追问能不能提供一段代码?这两天一直在想这些问题,一想到循环头就晕啊。谢谢了
热心网友
关注下