问题描述:有些整数可以写成若干个连续整数之和的形式,比如15=1+2+3+4+5,15=4+5+6,15=7+8等。要求编写一个控制台应用程序,满足以下功能。
(1)从键盘接收一个整数;
(2)若该整数可以写成若干个连续整数之和,就输出其所有的整数序列。
例如对于15,就输出
1 2 3 4 5 
4 5 6 
7 8
(3)若该整数不能写成若干个连续整数之和,输出提示信息“不存在满足条件的序列”。
(4)计算求1000内满足条件的整数时循环执行所用的时间。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;namespace lianxuzhengshu
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            int i=0,j=0,sum=0;
            Console.WriteLine("请输入一个整数:");
            string str=Console.ReadLine();
            int y = Convert.ToInt32(str);
            for ( i = 1; i < y/2; i++)
            {
                j = i + 1;
                sum = i;
                for (; j < y / 2;j++ ) 
                {
                    sum +=j;  
                    } //第二个
                
                }//第一个
              if (sum == y && j != i)
              {
                Console.WriteLine(" " + i + " " + j);
                Console.WriteLine();
              }
              else
              {
                Console.WriteLine("不存在满足条件的序列!");
              }
                  
           
        
          //  Console.WriteLine();
            Console.ReadLine();
            sw.Stop();
            Console.WriteLine("计算时间为:" + sw.ElapsedMilliseconds.ToString() + "ms");
            Console.WriteLine();
        }
    }
}

一直运行不了,求指教啊啊啊啊......c#控制台应用求改错

解决方案 »

  1.   

    略改了一下            Stopwatch sw = new Stopwatch();
                sw.Start();
                int i = 0, j = 0, sum = 0;
                Console.WriteLine("请输入一个整数:");
                string str = Console.ReadLine();
                int y = Convert.ToInt32(str);
                int n=0;
                bool bFind = false;
                for (i = 1; i <= y / 2; i++)
                {
                    sum = i;
                    n=0;
                    for (j = i + 1; j <= y / 2 + 1; j++)
                    {
                        sum += j;
                        if (sum == y)
                        {
                            n++;
                            Console.WriteLine("{0}的第{1}个连续数:{2}-{3}", y, n, i, j);
                            bFind = true;
                            break;
                        }                } //第二个            }//第一个
                if(!bFind)
                {
                    Console.WriteLine("不存在满足条件的序列!");
                }            //  Console.WriteLine();
               // Console.ReadLine();
                sw.Stop();
                Console.WriteLine("计算时间为:" + sw.ElapsedMilliseconds.ToString() + "ms");
                Console.WriteLine();
                Console.ReadLine();运行结果
    请输入一个整数:
    15
    15的第1个连续数:1-5
    15的第1个连续数:4-6
    15的第1个连续数:7-8
    计算时间为:1960ms
      

  2.   

    sum=i;下一行的n=0;要去掉 for (i = 1; i <= y / 2; i++)
     {
        sum = i;
        //n=0; //这句要注销掉
         ...
    这样运行结果如下:
    请输入一个整数:
     15
     15的第1个连续数:1-5
     15的第2个连续数:4-6
     15的第3个连续数:7-8
     计算时间为:1960ms
     
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
              
                Console.WriteLine("请输入一个整数:");
                string str = Console.ReadLine();
                int y = Convert.ToInt32(str);
              
                Stopwatch sw1 = new Stopwatch();
                sw1.Start();
                PrintNum(y);
                Console.WriteLine("计算时间为:" + sw1.ElapsedMilliseconds.ToString() + "ms");
                sw1.Stop();
                Console.WriteLine();
                Console.ReadLine();
            }        public static int MaxCount(int sum)
            {
                int n =Convert.ToInt32(Math.Ceiling( System.Math.Sqrt(Convert.ToDouble(sum*2-4))));
                return n;
            }        public static void PrintNum(int sum)
            {
                string str = "";
                int n=MaxCount(sum);
                int tempj = 0, tempi = 0;
                for (int i = 2; i < n; i++)
                {
                    int m = Convert.ToInt32(System.Math.Ceiling((Convert.ToDouble(2*sum)-i*(i-1)) / (2*i)));
                    for (int j = 1; j <= m; j++)
                    {
                        if (i * (i - 1) + 2 * i*j == 2 * sum)
                        {
                            tempj = j;
                            tempi = i;
                            str = "";
                        }
                    }
                    if (tempj > 0)
                    {
                        str = "";
                        for (int k = tempj; k < tempj + i; k++)
                        {
                            str = str + k.ToString() + "+";
                        }
                        Console.WriteLine(tempi.ToString() + "个数:" + str.TrimEnd( '+') + "=" + sum.ToString());
                    }
                    tempj = 0; tempi = 0;
                }
                if (string.IsNullOrEmpty(str))
                {
                    Console.WriteLine("没有连续数字的和等于此值");
                }
            }    }
        
    }