一道XX公司程序设计大赛题

题目描述:一个正整数有可能可以被表示为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

java实现:


import java.util.Scanner;

public class FindNumber {
 /**
  * @param args
  */
 public static void OddNumber(float m ,int n){
  /**处理奇数
   * @param args
   */
  boolean isWrite = false;
  for(;n>1;n--){
   int checkResult = checkNumber(m,n);
   switch (checkResult){
   case 0:
   {
    //System.out.println("--------------------There exist numbers when n="+n+"--------------------");
    int count = n/2;
    for(int i = 0; i < n; i++){
     if(i==n-1){
      System.out.println((int)m/n-count+"\t");
      count--;
     }
     else{
      System.out.print((int)m/n-count+"\t");
      count--;
     }          
    }
   isWrite = true;
   break;
   }
   case 1:
   {
     //System.out.println("--------------------There exist numbers when n="+n+"--------------------");
     int count = n/2;
     for(int i = 0; i < n; i++){
      if(i==n-1){
       System.out.println((int)m/n-count+1+"\t");
       count--;
      }
      else{
       System.out.print((int)m/n-count+1+"\t");
       count--;
      }
      
     }
    isWrite = true;
    break;
   }
  default:{} 
 }
 }
  if(!isWrite)
  {
   System.out.println("NONE");
  }
 }
 public static void EvenNumber(float m ,int n){
  /**处理偶数
   * @param args
   */
  boolean isWrite = false;
  for(;n>1;n--){
   int checkResult = checkNumber(m,n);
   switch (checkResult){
   case 0:
   {
    //System.out.println("--------------------There exist numbers when n="+n+"--------------------");
    
   break;
   }
   case 1:
   {
    int count = n/2;
    if(n == 2){
     break;
     }
    for(int i = 0; i < n; i++){
     if(i==n-1){
      System.out.println((int)m/n-count+1+"\t");
      count--;
     }
     else{
      System.out.print((int)m/n-count+1+"\t");
      count--;
     }
     
    }
    isWrite = true;
    break;
   }
  default:{} 
 }
 }
  if(!isWrite)
  {
   System.out.println("NONE");
  }
 }
 public static int checkNumber(float m ,int n){
  /**检查m/n是否等于k(正整数)+0.5,是返回true,否则返回false。
   * m代表输入的数,n代表输出成功序列数个数
   * @param args
   */
  float fShang = m/n;
  int iShang = (int) (m/n);
  double iCha = fShang - iShang;
  if(iCha ==0){
   //System.out.println(m+"/"+n+"\tequal\t"+fShang+"\ttrue");
   return 0;// 有解
  }
  if(iCha == 0.5){
   //System.out.println(m+"/"+n+"\tequal\t"+fShang+"\ttrue");
   return 1;//有解
  }
  else{
   //System.out.println(m+"/"+n+"\tequal\t"+fShang+"\tfalse");
   return 2;//无解
  }
  
  
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 System.out.println("Please input a number");
 Scanner scanner = new Scanner(System.in);
 float inputNumber = scanner.nextFloat();
 int flag = (int) (Math.sqrt(0.25+2*inputNumber)-0.5);
 if(inputNumber%2 == 0){
  System.out.println("U enter an Even Number:"+(int)inputNumber);
  EvenNumber(inputNumber,flag);
 }
 else{
  System.out.println("U enter an Odd Number:"+(int)inputNumber);
  OddNumber(inputNumber,flag);
 } 
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值