| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1059 人关注过本帖
标题:有关Prime numbers输出的问题
只看楼主 加入收藏
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
 问题点数:0 回复次数:4 
有关Prime numbers输出的问题
The following pseudocode will build an array, a, containing the first n prime numbers:
Set the first element of a to the lowest prime number (2).
For each odd number, test, starting at 3:
    For each element, p, in a:
        Compute the remainder of test/p.
    If the remainder was non-zero for every p:
        Append test to a.
    If the length of a equals n:
        break
Assignment
Create a Java program, Prime.java, to implement this algorithm. The program should accept one command-line argument, the number of primes to generate (n). It should respond by printing the first n prime numbers, followed by a message stating the n-th prime number. If more than 10 prime numbers are requested, the program should print only the first five and the last five, separated by a line displaying an ellipsis ("...").
For example:
$ java Prime 13
   2
   3
   5
   7
   11
   ...
   23
   29
   31
   37
   41
The 13-th prime number is 41.
$
Other requirements:
?    Your program should generate the entire array of prime numbers before printing any of them, rather than printing them "on the fly".
?    For n equal to 1, 2, or 3, the output should read "first", "second", or "third" rather than "1-th", "2-th", or "3-th". (Feel free to generalize this to other numbers.)
?    If no command-line argument is supplied, the program should print a helpful message to the Java error stream, System.err, and exit.
?    Use int (4-byte) variables throughout the program.
?    Your program should make no assumption about the maximum number of primes which can be requested. For example, you cannot simply define a as an 8000-element array.
?    If the command-line argument is less than one, assume that it is equal to one.

下面的代码是我写的,只是能完成生成符合要求的素数....
现在要实现控制台输入的,我对类型转换的地方还不熟,请大家帮帮忙...
/**
 * @(#)Prime.java
 *
 *
 * @author liude
 * @version 1.00 2008/3/12
 */

public class Prime {

    /**
     * Creates a new instance of <code>Prime</code>.
     */
    public Prime() {
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
   
    int input;int i,j;input=568;//input就是接收输入的参数,并转换为int类型
    int temp=3;
    int []prime=new int[input];
    prime[0]=2;
    if(input>1)
        prime[1]=3;
    for(i=2;i<input;i++)
    {j=0;
    while(i!=j)
    {if(temp%prime[j]==0)
     {temp=temp+2;
     j=0;
     }
         else
             j++;
          }
          prime[i]=temp;
          j=0;
    }
        for(i=0;i<input;i++)
           System.out.println(prime[i]);
       }
}
搜索更多相关主题的帖子: Prime numbers remainder test The 
2008-03-13 16:49
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
大家帮忙给解决下吧````
2008-03-13 18:51
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
弄个输入到n就行了,哪需要什么类型转换啊?

Fight  to win  or  die...
2008-03-13 19:32
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
在控制台传入的参数是保存在args[]中的啊...在程序中使用时使用的int类型的数据...我感觉是应该转换的啊```
2008-03-13 21:33
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
/**
 * @(#)Prime.java
 *
 *
 * @author liude
 * @version 1.00 2008/3/12
 */

public class Prime {

    /**
     * Creates a new instance of <code>Prime</code>.
     */
    public Prime() {
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
   
    int i,j;
    Integer input=new Integer(args[0]);
    int temp=3;
    int []prime=new int[input];
    prime[0]=2;
    if(input>1)
        prime[1]=3;
    for(i=2;i<input;i++)
    {j=0;
    while(i!=j)
    {if(temp%prime[j]==0)
     {temp=temp+2;
     j=0;
     }
         else
             j++;
          }
          prime[i]=temp;
          j=0;
    }
    if(input<4)
    {
       for(i=0;i<input;i++)
          System.out.println("the "+(i+1)+"-th is: "+prime[i]);
        }
        else if(input>3&&input<11)
        {
        for(i=0;i<input;i++)
           System.out.println("the "+(i+1)+"-th is: "+prime[i]);
            }
            else
            { for(i=0;i<5;i++)
                System.out.println("the "+(i+1)+"-th is: "+prime[i]);
                System.out.println("...");
                for(i=input-5;i<input;i++)
                System.out.println("the "+(i+1)+"-th is: "+prime[i]);
            }
}
}

现在基本上完成了...还有点小问题..
就是在这里...
For n equal to 1, 2, or 3, the output should read "first", "second", or "third" rather than "1-th", "2-th", or "3-th". (Feel free to generalize this to other numbers.)
是不是必须自己手动一个一个转?
2008-03-14 14:40
快速回复:有关Prime numbers输出的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038857 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved