求前N項的值

Home Home
引用 | 編輯 chpo
2009-04-20 13:43
樓主
推文 x0
求前N項的值,N由使用者輸入

ex:10000列10000前的值數
..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 leacks
2009-04-20 17:33
1樓
  
你是要加還是要啥?
還是要乘阿?

獻花 x0
引用 | 編輯 chpo
2009-04-20 22:13
2樓
  
是把1~n數全部列出來

獻花 x0
引用 | 編輯 totsi
2009-04-22 08:22
3樓
  
Java我不會..不過用c++可以這樣寫

char *pcNum = NULL;
int nNo = 0;

scanf("%d", &nNo);//取得輸入值

pcNum = new char[nNo];//allocate memory

for ( int i = 0 ; i < nNo ; i++ )
{
  pcNum[0] = i+1;//放在陣列裡面
  printf("%s\n", pcNum);//印出來
}

delete pcNum;//release memory
pcNum = NULL;

獻花 x0
引用 | 編輯 jim28791
2009-04-23 23:36
4樓
  
不知你想要的是不是這樣~~

複製程式
import java.io.*;
class bn
{
     public static void main(String args[]) throws IOException
     {
           BufferedReader buf;
           String str;
           int x,y=0;
           buf = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("請輸入數字:");
           str = buf.readLine();
           x=Integer.parseInt(str);
           int tmn[]=new int[x];
           for(int i=1;i<x;i++)if(test(i)==0)
           {
                  tmn[y]=i;
                  y++;
           }
           for(int i=0;i<tmn.length;i++)if(tmn[i]!=0)System.out.println(tmn[i]);else break;
     }
     public static int test(int n)
          {
              for(int i=2;i<=Math.sqrt(n);i++)if(n%i==0)return 1;
              return 0;
          }

}



java還不太熟~~~
對於怎麼讓陣列需要時才加空間~
這我不太會~~
所以~這程式會佔用許多多餘的空間XD

獻花 x0