zoukankan      html  css  js  c++  java
  • 华为机试 --- 质数因子

    详细描述:


    函数接口说明:

    public String getResult(long ulDataInput)

    输入参数:

    long ulDataInput:输入的正整数

    返回值:

    String

    输入描述:

    输入一个long型整数

    输出描述:

    按照从小到大的顺序输出它的所有质数的因子,以空格隔开。最后一个数后面也要有空格。

    输入例子:
    180
    输出例子:
    2 2 3 3 5


    思路一:

    cpp版本:

    #include <iostream>
    using namespace std;
    int main()
    {
        int num,i;
        cin >> num;
        for(i = 2; i <= num; i++)
        {
             while(num % i == 0)
             {
                 cout << i << " ";
                 num /= i;
             }
        }
        return 0;
    }

    2.  java 版本

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner str = new Scanner(System.in); //获取输入,创建对象
            long num = str.nextLong();   // 
            String result = getResult(num);
            System.out.println(result);
        } 
         public static String getResult(long num){
             int pum = 2;
             String result = "";     // 这里用String来作为结果存放
             while(num != 1){
                 while(num%pum == 0){
                     num = num/pum;
                     result = result + pum + " ";
                 }
                 pum++;
             }
             return result;
         }
    }

    注意:

    一、扫描控制台输入

    这个例子是常常会用到,但是如果没有Scanner,你写写就知道多难受了。
    当通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象。如果要获取输入的内容,则只需要调用Scanner的nextLine()方法即可。

    /** 
    * 扫描控制台输入 

    * @author leizhimin 2009-7-24 11:24:47 
    */
     
    public class TestScanner { 
            public static void main(String[] args) { 
                    Scanner s = new Scanner(System.in); 
                    System.out.println("请输入字符串:"); 
                    while (true) { 
                            String line = s.nextLine(); 
                            if (line.equals("exit")) break
                            System.out.println(">>>" + line); 
                    } 
            } 
    }



    请输入字符串: 
    234 
    >>>234 
    wer 
    >>>wer 
    bye 
    >>>bye 
    exit 

    Process finished with exit code 0



    先写这里吧,有空再继续完善。

    二、如果说Scanner使用简便,不如说Scanner的构造器支持多种方式,构建Scanner的对象很方便。

    可以从字符串(Readable)、输入流、文件等等来直接构建Scanner对象,有了Scanner了,就可以逐段(根据正则分隔式)来扫描整个文本,并对扫描后的结果做想要的处理。

    三、Scanner默认使用空格作为分割符来分隔文本,但允许你指定新的分隔符

    使用默认的空格分隔符:
            public static void main(String[] args) throws FileNotFoundException { 
                    Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf    ......asdfkl    las"); 
    //                s.useDelimiter(" |,|\."); 
                    while (s.hasNext()) { 
                            System.out.println(s.next()); 
                    } 
            }



    123 
    asdf 
    sd 
    45 
    789 
    sdf 
    asdfl,sdf.sdfl,asdf 
    ......asdfkl 
    las 

    Process finished with exit code 0



    将注释行去掉,使用空格或逗号或点号作为分隔符,输出结果如下:
    123 
    asdf 
    sd 
    45 
    789 
    sdf 
    asdfl 
    sdf 
    sdfl 
    asdf 







    asdfkl 

    las 

    Process finished with exit code 0




  • 相关阅读:
    matplotlib 进阶之origin and extent in imshow
    Momentum and NAG
    matplotlib 进阶之Tight Layout guide
    matplotlib 进阶之Constrained Layout Guide
    matplotlib 进阶之Customizing Figure Layouts Using GridSpec and Other Functions
    matplotlb 进阶之Styling with cycler
    matplotlib 进阶之Legend guide
    Django Admin Cookbook-10如何启用对计算字段的过滤
    Django Admin Cookbook-9如何启用对计算字段的排序
    Django Admin Cookbook-8如何在Django admin中优化查询
  • 原文地址:https://www.cnblogs.com/simplepaul/p/6737233.html
Copyright © 2011-2022 走看看