程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 找出數列中不存在的數據項

找出數列中不存在的數據項

編輯:C#入門知識

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();
                }
            }

            Console.WriteLine();

            Console.WriteLine("The missing number is: {0}.", FindMissingNumber(list, low, up));
        }
    }
}
Output


   1    2    3    4    5    6    7    8    9   10
  11   12   13   14   15   16   17   18   19   20
  21   22   23   24   25   26   27   28   29   30
  31   32   33   34   35   36   37   38   39   40
  41   42   43   44   45   46   47   48   49   50
  51   52   53   54   55   56   57   58   59   60
  61   62   63   64   65   66   67   68   69   70
  71   72   73   74   75   76   77   78   79   80
  81   82   83   84   85   86   87   88   89   90
  91   92   93   94   95   97   98   99  100
Expected sum without missing 5050
Actual sum without missing 4954
The missing number is: 96.

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved