高分,求一段算法:字符串转换为循环

aoenzh 2007-12-04 09:42:51
我有一个字符串
String a="100,200,1000";
现在想实现一下这段代码
for(int i=0;i<100;i++){
for(int i=0;i<200;i++){
for(int i=0;i<1000;i++){

}
}
}
可是,我的a中的字符串的长度和位数都不一定,或许是String a="100,200,1000,9";
求一段简洁程序,实现上边逻辑
...全文
347 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
cchaha 2007-12-05
  • 打赏
  • 举报
回复
MARK
ooo19841080xinxin 2007-12-05
  • 打赏
  • 举报
回复
Mark
wcx19852 2007-12-04
  • 打赏
  • 举报
回复
可以再少一点。
……
String[] array = a.split(",");
int n = array.length;
int[] result = new int[n];

public void execute(int value)
{
n--;
for(int i=0;i <Integer.parseInt(array[n]);i++)
{
result[n]=i;
if(n==0)//找到了一组数,存在数组 result 中
{
……//对组数进行判断,看是否符合条件。
return;
}
else if(n> 0)
{
execute();//递归
}
else if(n <0)
return;
}
}
……
wcx19852 2007-12-04
  • 打赏
  • 举报
回复
看到贴了,乍一看很简单,细看要求,还真不好做,写的不简洁。
有点想法写出来不知道对不对,仅供参考。
……
String[] array = a.split(",");
int n = array.length;
int[] result = new int[n];

public void execute(int value)
{
int testValue;
if(n==array.length)//第一次执行给 value 赋值
value=1;

n--;
for(int i=0;i<Integer.parseInt(array[n]);i++)
{
testValue=value*i;
result[n]=i;
if(n==0&&testValue==100)//找到了符合条件的一组数,存在数组 result 中
{
……//
return;
}
if(n>0)
{
execute(testValue);//递归
}
if(n<0) return;
}

}

……
jinxfei 2007-12-04
  • 打赏
  • 举报
回复
class StrangeNumber{
int[] max;
int[] current;

public static void main(String[] args){
String a="100,200,1000";
StrangeNumber num=new StrangeNumber(a);
while(!num.reachMax()){
num.increase();
if (num.satisfied()){
//实现要做的事情
}
}
}



public StrangeNumber(String str){
String[] segs=str.split(",");
max=new int[str.length];
current=new int[str.length]
for(int i=0; i<segs.length;i++){
max[i]=Integer.parseInt(segs[i]);
current[i]=0;
}
}

public void increase(){
current[current.length-1]++;
for(int i=current.length-1;i--;i>=0){
if (current[i]>=max[i]){
current[i]=0;
current[i-1]++;//简单起见,先不处理下标越界
}
}
}

public boolean reachMax(){
//判断各位是否已经达到最大
}

public boolean satisfied(){
//计算current的当前各位数字是否符合要求
}


}
wdman 2007-12-04
  • 打赏
  • 举报
回复
深度优先搜索问题。


List<Integer> LoopDo(List<Integer> intList,List<Integer> indexList) {
if (intList.size == 0) {
//伪代码
if indexList的所有元素的集合满足你的要求,输出你所有的元素。
return indexList;
}
for (int i=0; i<intList.get(0).intValue; i++) {
LoopDo(intList.subList(1,intList.size()),indexList.add(new Integer(i)));
}
}

调用方法
main(){
List<Integer> intList = 你的string中分割得到的list
List<Integer> indexList = new ArrayList<Integer>();
LoopDo(intList,indexList);
}
aoenzh 2007-12-04
  • 打赏
  • 举报
回复
shan1119 ,果然是高人啊,,,
shan1119 2007-12-04
  • 打赏
  • 举报
回复
            if(i>=loop[len-1]){
i=0;
}

这块不要,忘去掉了.
醉面韦陀 2007-12-04
  • 打赏
  • 举报
回复
谢谢aoenzh,不过我觉得我的代码还是不够简洁,希望其他高手给点指点!
shan1119 2007-12-04
  • 打赏
  • 举报
回复
		String   a="1,2,2,3"; 
String[] arr = a.split(",");
int len = arr.length;
int[] loop = new int[arr.length];
int[] curr = new int[arr.length];
for(int i=0;i<loop.length;i++)
loop[i] = new Integer(arr[i]).intValue();

for(int i=0;curr[0]<loop[0];i++){

for(int j=0;j<loop.length;j++)
System.out.print(curr[j]+"\t");
System.out.println();

curr[len-1]++;

for(int j=len-1;j>0;j--){
if(curr[j]==loop[j]){
for(int k=j;k<len;k++)
curr[k]=0;
curr[j-1]++;
}
}

if(i>=loop[len-1]){
i=0;
}

}
ml_dark 2007-12-04
  • 打赏
  • 举报
回复
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,1,2,3};
execute(a,0);
}

public static void execute(int[] a,int index){
if (index >= a.length)
return;
for (int i = 0; i < a[index]; i++){
//some code here
execute(a,index + 1);

}
}
ml_dark 2007-12-04
  • 打赏
  • 举报
回复
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,1,2,3};
execute(a,0);
}

public static void execute(int[] a,int index){
if (index >= a.length)
return;
for (int i = 0; i < a[index]; i++){
//some code here
execute(a,index + 1);

}
}
TerryLhw1983 2007-12-04
  • 打赏
  • 举报
回复
以下应该可以满足楼主您的要求,用递归。
public class MTest {

private static int count = 1;
public static void main (String [] args) {
String a = "1,7,3,4";
String[] lens = a.split("[,]");
doLoop(lens, 0);
}

public static void doLoop(String [] lens, int index) {
if (index != lens.length) {
int range = Integer.parseInt(lens[index]);
for (int i = 0; i < range; i++) {
doLoop(lens, index + 1);
}
} else {
System.out.println("doLoop" + String.valueOf(count));
count++;
}
}
}
jinxfei 2007-12-04
  • 打赏
  • 举报
回复
还有,以上提供递归算法的也都可以,但要求给递归函数再传递一个参数,表示当前已经循环到的i.j.k.l等,否则搂主无法判断当前循环变量是否符合要求。
aoenzh 2007-12-04
  • 打赏
  • 举报
回复
14楼,写的挺好
aoenzh 2007-12-04
  • 打赏
  • 举报
回复
15楼批评的是,我尝试了很多次,现实了这个思路,不过,写的太烂,
想看看大家的思路,学习学习
jinxfei 2007-12-04
  • 打赏
  • 举报
回复
搂主,我觉得我的思路已经非常好了,
你能不能实践一下,总不能等着别人都做好吧。
醉面韦陀 2007-12-04
  • 打赏
  • 举报
回复

package com.capinfo.test;

public class StringTest {
public static void main(String[] args) {

String s="100,200,1000,9,2000";
digui(s);
}
/**
* @param s
* @author sundful
* @vesion 1.0
*/
public static void digui(String s)
{
String s1="";
String s2="";
if("".equals(s) || s.trim().length()<0) return;
int num=s.indexOf(",");
if(num !=-1)
{
s1=s.substring(0,num);
s2=s.substring(num+1);
for(int i=0;i<Integer.parseInt(s1);i++)
{
digui(s2);
System.out.println(i);
}
}
}
}

aoenzh 2007-12-04
  • 打赏
  • 举报
回复
9楼,你的递归我没看明白,愚钝了

我有一个字符串
String a="100,200,1000";
然后用split把a分割了
现在想实现一下这段代码
for(int i=0;i <100;i++){
for(int j=0;j <200;j++){
for(int k=0;k <1000;k++){
//最重要的,我是想实现这里的逻辑,我想把所有符合条件的ijk都找出来
}
}
}
ustbsjl 2007-12-04
  • 打赏
  • 举报
回复
初看很简单的问题,仔细想想还是很复杂的
加载更多回复(13)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧