zoukankan      html  css  js  c++  java
  • 百度程序题目连续数问题


    /*题目:
    第一题(共四题100分):连续正整数(10分) 

    题目描述:一个正整数有可能可以被表示为n(n>=2)个连续正整数之和,如: 

    15=1+2+3+4+5 
    15=4+5+6 
    15=7+8
    请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。 
    输入数据:一个正整数,以命令行参数的形式提供给程序。 

    输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。如果结 果有多个序列,按各序列的最小正整数的大小从小到大打印各序列。此外,序列不允许重复,序列内的整数用一个空格分隔。如果没有符合要求的序列,输出 “NONE”。  

    例如,对于15,其输出结果是: 
    1 2 3 4 5 
    4 5 6 
    7 8 
    对于16,其输出结果是: 
    NONE 

    评分标准:程序输出结果是否正确。 */


    /**
     * 百度之星程序大赛第一题
     * 2010年5月18日19点30分于公司
     */
    public class tai {

     private String str = "";
     private int time = 0; //答案个数
     
     private void print(int n)
     {
      int m = n/2 + 1;
      for (int i = 1; i <= m; i++) {
       int he = 0;
       int j = i;
       while(he < n && j < m+1)
       {
        he = he + j;
        str = str + j + " + ";
        j++;
       }
       if(he == n)
       {
        time ++;
        System.out.println(str.substring(0, str.length() - 3) + " = " + n);
       }
       str = "";
      }
      if(time == 0)
      {
       System.out.println("NONE");
      }
     }
     
     public static void main(String[] args) {
      new tai().print(15);
     }
    }


    /*打印结果:
     1 + 2 + 3 + 4 + 5 = 15
     4 + 5 + 6 = 15
     7 + 8 = 15
    */

  • 相关阅读:
    web 学习资源整理
    CodeSmith 学习资料收集
    常用 T_SQL 语句
    SQL Server 2000查询分析器自定义查询快捷键
    插入标识列 identity_insert
    c# 上传FTP文件
    (.Net 3.5Sp1)WebForm使用System.Web.Routing
    SPQuery.ViewAttributes
    ChatterBot之linux下安装mongodb 02
    linux端口开放指定端口的两种方法
  • 原文地址:https://www.cnblogs.com/chaohi/p/2330349.html
Copyright © 2011-2022 走看看