当前位置:编程学习 > C#/ASP.NET >>

找出数列中不存在的数据项

Problem

Find the missing number from the list of 99 distinct numbers which are from 1-100


Solution

Compute sum of the list.
Compute desirable sum without missing number.
The missing number is desirable sum minus actual sum.

 

[csharp]
using System; 
using System.Collections.Generic; 
using System.Linq; 
 
namespace MissingNumber 

    class Program 
    { 
        static int FindMissingNumber(List<int> list, int low, int up) 
        { 
            int sum = (up + low) * (up - low + 1) >> 1; 
 
            Console.WriteLine("Expected sum without missing {0}", sum); 
            Console.WriteLine("Actual sum without missing {0}", list.Sum()); 
 
            return sum - list.Sum(); 
        } 
 
        static void Main(string[] args) 
        { 
            List<int> list = new List<int>(); 
            Random random = new Random(100); 
            int low = 1; 
            int up = 100; 
            int missNumber = random.Next(low, up); 
 
            for (int i = low; i < missNumber; ++i) 
            { 
                list.Add(i); 
            } 
 
            for (int i = missNumber + 1; i <= up; ++i) 
            { 
                list.Add(i); 
            } 
 
            int line = 0; 
            foreach (int k in list) 
            { 
                Console.Write("{0, 4} ", k); 
                line++; 
                if (line == 10) 
                { 
                    line = 0; 
                    Console.WriteLine(); 
                } 
            } 
 
            Console.WriteLine(); 
 
            Console.WriteLine("The missing number is: {0}.", FindMissingNumber(list, low, up)); 
        } 
    } 

using System;
using System.Collections.Generic;
using System.Linq;

namespace MissingNumber
{
    class Program
    {
        static int FindMissingNumber(List<int> list, int low, int up)
        {
            int sum = (up + low) * (up - low + 1) >> 1;

            Console.WriteLine("Expected sum without missing {0}", sum);
            Console.WriteLine("Actual sum without missing {0}", list.Sum());

            return sum - list.Sum();
        }

        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            Random random = new Random(100);
            int low = 1;
            int up = 100;
            int missNumber = random.Next(low, up);

            for (int i = low; i < missNumber; ++i)
            {
                list.Add(i);
            }

            for (int i = missNumber + 1; i <= up; ++i)
            {
                list.Add(i);
            }

            int line = 0;
            foreach (int k in list)
            {
                Console.Write("{0, 4} ", k);
                line++;
                if (line == 10)
                {
                    line = 0;
                    Console.WriteLine();
                }
            }

&

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,