第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

循環(huán)遍歷一個(gè)序列并返回模數(shù)

循環(huán)遍歷一個(gè)序列并返回模數(shù)

PHP
溫溫醬 2023-10-15 15:22:51
我有一個(gè)從 1 到 7 的整數(shù)序列:(1, 2, 3, 4, 5, 6, 7這些實(shí)際上是數(shù)組鍵)我需要一個(gè)函數(shù),它接受一個(gè)整數(shù)作為參數(shù),并返回該序列中的下一個(gè)項(xiàng)目。它會(huì)“循環(huán)”該序列,或“迭代”該序列。不確定是否足夠清楚,所以這里有一些例子:myNextNumber(3)會(huì)回來(lái)4,myNextNumber(7)會(huì)回來(lái)1,myNextNumber(1)會(huì)回來(lái)2我需要與之前的數(shù)字相同的內(nèi)容:myPreviousNumber(3)會(huì)返回2,myPreviousNumber(7)會(huì)返回6,myPreviousNumber(1)會(huì)返回7,這 2 個(gè)函數(shù)是參數(shù)的 +1 或 -1 步長(zhǎng)。如果我可以將這兩個(gè)函數(shù)合并為一個(gè)可以接受第二個(gè)參數(shù)的函數(shù),該參數(shù)將是 或+1或-1其他任何內(nèi)容的步驟,例如+42,它會(huì)在返回正確的索引之前“循環(huán)”序列六次,那就太好了。但這樣要求就太多了。現(xiàn)在我將非常感謝您的指導(dǎo)myNextNumber()。我很確定一個(gè)帶有模運(yùn)算符的非常簡(jiǎn)單的單行代碼就滿足了它的需要,但我無(wú)法讓它工作。
查看完整描述

3 回答

?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

public int getNextNumber(int[] array, int index) {

    index = Math.floorMod(index, array.length) + 1;

    return array[index];

}

我不懂 PHP,但我想這是準(zhǔn)確的:


function getNextNumber(&$array, int $index) {

    $index = fmod($index, count(array)) + 1

    return $array[$index]


查看完整回答
反對(duì) 回復(fù) 2023-10-15
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

代碼


function getKey($current, $step): int

{

    $keys = [1,2,3,4,5,6,7];

    $currentKey = array_search($current, $keys);

    $size = count($keys);


    // normalize step offset

    $step = ($step % $size) + $size;

    //       ^-----------^  ^-----^

    //              │          └ move possible negative to positive

    //              └ get a value from -7 to +7

    

    // add offset for current key

    $newKey = ($currentKey + $step) % $size;

    //         ^-----------------^  ^-----^

    //                  │              └ wrap around in case we exceed $size

    //                  └ add normalized step offset to new element to current

    return $keys[$newKey];

}


// Tests


echo getKey(1,-1) . PHP_EOL;

echo getKey(3,1) . PHP_EOL;

echo getKey(7,1) . PHP_EOL;

echo getKey(7,15) . PHP_EOL; // same as +1

echo getKey(1,-8) . PHP_EOL; // same as -1

echo getKey(1,-15) . PHP_EOL; // same as -1

輸出


7

4

1

1

7

7

工作示例。



查看完整回答
反對(duì) 回復(fù) 2023-10-15
?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

這個(gè)函數(shù)就可以解決問(wèn)題,你應(yīng)該使用 mod 來(lái)查找數(shù)字(對(duì)于 php 和 java 來(lái)說(shuō)是“%”);


function myNextNumber($i){

     if($i>6){

       return ($i%7)+i;

     }

     return $i+1;

}



function myPreviousNumber($i){

    if($i>8){

       return $i%7-1;

    }

    if($i == 1){

      return 7;

    }

    return $i-1;

}


function myNextNumber($i, $param){

   if($param == 1){

     return myNextNumber($i);

   }


   if($param == -1){

     return myPreviousNumber($i);

   }

   

   return 'Invalit Param';

}


查看完整回答
反對(duì) 回復(fù) 2023-10-15
  • 3 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)