華為猴子吃桃子問題

來源:互聯網
上載者:User

標籤:

N(3<=N<=9))只猴子採得一堆桃子,半夜裡第一隻猴子偷偷起來把桃平均分成N分, 發現還多一個,它吃了那個桃子,還拿走其中的一份,第二隻猴子起來又把桃子分成N分,還多一個,它吃了那個桃子,又拿走其中的一份,第三隻、第四隻......第N只猴子都如此做了,請問這堆桃子至少有多少個?

直接上代碼:

package test;

 

import java.util.Scanner;

 

public class Monkey {

static int n;

static int sum;

static Scanner scanner = new Scanner(System.in);

 

public static void main(String[] args) {

System.out.println(""+Integer.MAX_VALUE);

input();

 

output();

}

 

private static void output() {

 

System.out.println("" + n);

 

if (n > 9 || n < 3) {

System.out.println("0");

} else {

int number = n;

 

for (int i = n + 1; i < Integer.MAX_VALUE; i++) {

if (!isOk(i, number))

continue;

else {

System.out.println("" + i);

break;

}

}

}

}

 

private static boolean isOk(int i, int m) {

int count = 0;

while (count < m) {

if (i % (m) != 1)

return false;

i = i - 1 - (i / m);

count++;

 

}

return true;

 

}

 

private static void input() {

n = scanner.nextInt();

 

}

 

}

華為猴子吃桃子問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.