從1加到100的算法你會嗎?那從第M加到第N呢?

今天在看視頻教程的時候,聽到“楊中科”老師說有很多大公司,在面試的時候常常問一些基礎的東西,甚至常問你一些簡單到“變態”的題,對於我們做Web開發來說,突然問你一些算法題,也許有好多人當時的腦子是空白的;楊老師說例如問到從1加到100的算法,有很多程序員都寫不出來;今天在這裏,我用一種數學公式來算出這道題~

首先要知道從1加到100的數式; n*(n-1)/2+n;  大家都知道從1加到100等於5050;  不妨用這個公式套一下,看看等不等於5050;

n就是從1加到第幾的數字;    100*(100-1)=9900;  9900/2=4950;   4950+100=5050;  看來這個公式是行的通的;所以當你不會用for循環來寫的時候,就可以用這種公式的寫法了;

 

從1加到N的代碼:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
public class Program
    {
        
static void Main(string[] args)
        {
            
int n, m, result;
            
string chars = "Y";
            
bool test;
            
do
            {
                Console.WriteLine(
"===============從1加到N的和===============");
                Console.WriteLine(
"請輸入一個數字!");
                test 
= int.TryParse(Console.ReadLine(), out n);
                
if (test == true)
                {
                    
result = n * (n - 1/ 2 + n;
                    Console.WriteLine(
"和爲:{0}", result);
                    Console.ReadLine();
                }
                
else
                {
                    Console.WriteLine(
"請不要輸入非法字符!");
                    chars 
= Console.ReadLine();
                }
            } 
while (chars == "N");

        }
    }
}

 

現在我們知道了從1到N的公式,那從M加到N的公式又是什麼呢?

我們來算一下,假設我們從5加到10 用計算機自帶的計算器算一下看看等於多少(數學好的可以心算);  結果等於45;

我們用這個公式來試一下: (n * (n - 1) / 2 + n) - ((m - 1) * (m - 2) / 2 + m - 1);

 

 
謝謝刀刀的公式:(m+n)*(n-m+1)/2 , (首項+末項)×項數/2 大家也可以用這個方法試一下!比起我寫的那個公式簡單明瞭;
 
14樓: ...神... 的遞歸寫法;
private state int Add(int m)
{
if(m<1)
return 0;
if (m=1)
return 1;
if (m>1)
return add(m) + add (m-1);
}

 

m就是第一個數字,n就是加到多少的數字;一看到這麼多的括號可能有點發蒙;不過不要緊,我們先把括號去掉,再分成兩塊來看就清淅多了;

 

第一部份還是和1加到N的公式一樣:n*(n-1)/2+n;    第二部份就是 ( m - 1 ) * ( m - 1 - 1 ) / 2 + m - 1 ;

爲什麼要m-1呢?  你想想看,我從5加到10,5前面還有幾個數字?  4個對吧?(1,2,3,4);  所以這裏要m-1 ,是爲了算出前面的數字和是多少;  不知道講到這裏大家有沒有暈掉;(我表達的不是很清楚,慢慢理解就好)

再用第一部份的公式,減掉第二部份的公式,就是我們要的結果;

我們再來試一次;

(10 * (10 - 1) / 2 + 10) - ((5 - 1) * (5 - 2) / 2 + 5 - 1)=45;

 

下面是代碼了:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
public class Program
    {
        
static void Main(string[] args)
        {
            
int n, m, result;
            
string chars = "Y";
            
bool test;
            
do
            {

                Console.WriteLine(
"從N到M的和");
                Console.Write(
"請輸入第一個數字:");
                test 
= int.TryParse(Console.ReadLine(), out m);
                
if (test == true)
                {
                   //如果是數字,我們什麼都不操作,因爲還有一個數沒有輸入;
                }
                
else
                {
                    Console.WriteLine(
"請不要輸入非法字符!");
                }
                Console.Write(
"請輸入第二個數字:");
                test 
= int.TryParse(Console.ReadLine(), out n);
                
if (test == true)
                {
                    result 
= (n * (n - 1/ 2 + n) - ((m - 1* (m - 2/ 2 + m - 1);
                    Console.WriteLine(
"從{0}到{1}的和爲:{2}", m, n, result);
                    Console.ReadLine();
                }
                
else
                {
                    Console.WriteLine(
"請不要輸入非法字符!");
                }
            } 
while (chars == "N");

        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章