English 中文(简体)
需将这种假装改为休息时间
原标题:Need to convert this for loop to a while loop

我最近解决了一个问题。 但是,我有这部法典,其中I dont利用先令和条件检查。 这对 lo来说是一种奇迹。 我想把它变成一种 while。 请帮助我这样做。 我曾多次尝试过,但有些地方没有。

for(;;current =(current+1)%n){
    if(eliminated[current%n]){
        continue;
    }else{
        inkiPinki++;
        if(inkiPinki == m){
            eliminated[current%n] = true;
            printStatus(eliminated, people);
            remainingGuys--;
            break;
        }
    }
}

在上述法典中,被放弃的是一种ool。

http://www.ohchr.org。 由于Geoff为我提供了一种解决办法,我进一步尽量减少了这种解决办法。

while( eliminated[current] || ++inkiPinki != m )
    current = (current+1) % n;
eliminated[current] = true;
printStatus( eliminated, people );
remainingGuys--;
最佳回答

提 出

while( true ) {
    if( !eliminated[current] ) {
        if( ++inkiPinki == m ) {
            break;
        }
    }
    current = (current+1) % n;
}
eliminated[current] = true;
printStatus( eliminated, people );
remainingGuys--;

这应该符合逻辑。

问题回答

使用以下模式的休息时间可以转换为:

for (..xxx..; ..yyy..; ..zzz..) {
  ..aaa..
}

......

...xxx...
while (...yyy...) {
  ..aaa..
  ..zzz..
}

铭记

for (;;) {
  ..aaa..
}

等于

for (nop; true; nop) {
  ..aaa..
}

“不适用”是指无业务。

您的榜样是:

for(;;current =(current+1)%n){
    if(eliminated[current%n]){
        continue;
    }else{
        inkiPinki++;
        if(inkiPinki == m){
            eliminated[current%n] = true;
            printStatus(eliminated, people);
            remainingGuys--;
            break;
        }
    }
}

相当于

// no initialzation needed
while(true) {
    //if(eliminated[current%n]){
    //    continue;
    //}else{
    if(!eliminated[current%n]){
        inkiPinki++;
        if(inkiPinki == m){
            eliminated[current%n] = true;
            printStatus(eliminated, people);
            remainingGuys--;
            break;
        }
    }
    current =(current+1)%n;
}

如果你愿意,你可以进一步简化。

我将如何这样做:

while (inkiPinki < m) {
    if (!eliminated[current % n]) {
        inkiPinki++;

        if (inkiPinki == m) {
            eliminated[current % n] = true;
        }
    }

    if (inkiPinki < m) {
        current = (current + 1) % n;
    }
}

printStatus(eliminated, people);
remainingGuys--;

该法典完全与你原来的住所相同,但使用合乎逻辑的测试来确定它是否应当继续居留。 无需<代码>continue或break。 如果你利用其中任何一个发言,或许可以做一些令人振奋的事情。

我似乎在使用诱杀装置时有过轻的倾向:

for (;inkiPinki<m; inkPinki += !eliminated[current])
    current = (current + 1) %n;

eliminated[current] = true;
printStatus(eliminated, people);
remainingGuys--;

I ve还更改了 Current%n ,简单地改为 Current ,因为%n已经在现行增益的情况下完成,即 当前。 更正应作在印发的记录上,由有关的代表团成员一人署名,送交逐字记录处处长(C-178)。

如果我这样做的话,我可能会改变这种感觉,因此,而不是eliminated

for (;inkiPinki<m; inkPinki += remaining[current])
    current = (current + 1) %n;

remaining[current] = false;
printStatus(remaining, people);
remainingGuys--;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签