java 程式問題 ”15點”

我們老師上課第一次出了這題~可是沒有人看懂請各位幫忙寫出~

1.寫程式計算 (x+1)/(n) + (x+2)/(n-1) + ... + (x+n)/1, 其中x 及n 的值由執行命令列參數輸入

其中注意之問題為迴圈有兩個值和x,n需以命令列參數輸入

4 Answers

Rating
  • 2 decades ago
    Favorite Answer

    public int WhateverThisMethodIsCalled(int x, int n)

    {

    int temp = 0;

    for (int i = 1; i <= n; i++)

    {

    temp += (x+i)/(n-i+1)

    }

    return temp;

    }

    hope this helps,

    this is a simple problem, i think someone else might come up with a

    much more brilliant idea ^^

    think throuhgh the problem, don't just copy, okay? ^^

    • Commenter avatarLogin to reply the answers
  • 2 decades ago

    換個角度想,用遞迴表示會簡單一點~不過題目並沒提到精準度,如果數值要精準應該用float or double 處理會好一點..這部份要發問者自己考慮了吧public class text{   public static void main(String[] args){        int n,x;        x=Integer.parseInt(args[0]);        n=Integer.parseInt(args[1]);   System.out.println(recursive(x,n));   }   public static int recursive(int x,int n)   {    if(n==1){   return  (x+1)/(n);         }    else     return  (x+1)/(n)+recursive(x+1,n-1);   }}

    圖片參考:http://blog.xuite.net/_users/58/50/10528319/blog_1...

    Source(s): Recursion
    • Commenter avatarLogin to reply the answers
  • Lv 7
    2 decades ago

    給你一點提示.在讀進x和n之後要用個迴圈從1走到n把(x+1)/(n) + (x+2)/(n-1) + ... + (x+n)/1中每項加起來.

    • Commenter avatarLogin to reply the answers
  • Anonymous
    2 decades ago

    呃…作業應該還是要自己寫比較好唄

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.