我最近解決了一個問題。但是我有這樣一段代碼,我不使用for循環初始化和條件檢查。 For循環看起來有點奇怪。我想將它轉換爲while循環。請幫我做。我嘗試了很多次,但有些地方丟失了東西。需要將此循環轉換爲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;
}
}
}
在上面的代碼中去掉[index]是一個布爾值。
編輯:感謝Geoff爲我提供的解決方案,我進一步將此解決方案最小化。
while(eliminated[current] || ++inkiPinki != m)
current = (current+1) % n;
eliminated[current] = true;
printStatus(eliminated, people);
remainingGuys--;
爲什麼你需要這樣的'while'?也許你只想簡化這個循環? – SergGr 2010-04-28 16:26:05
@iPhone beg:是的 – bragboy 2010-04-28 16:28:02
從您的編輯,我不知道你想要什麼?這段代碼肯定屬於循環內部? – Geoff 2010-04-28 16:29:25