This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23049|Reply: 0

[Source] C# three methods achieve recursive addition of 1 to 100

[Copy link]
Posted on 8/26/2015 10:50:46 PM | | |
  1. public int number = 0;
  2.         protected void Page_Load(object sender, EventArgs e)
  3.         {
  4.             Add(1);
  5.             test(1,0);
  6.             int bbb = num(1);
  7.             Response.Write(bbb);
  8.             Response.End();
  9.         }
  10.         
  11.         public void Add(int o)
  12.         {
  13.             if (o <= 100) {
  14.                 number += o;
  15.                 Add(o+1);
  16.             }
  17.         }
  18.         public int test(int start, int sum)
  19.         {
  20.             //start=1;
  21.             int aaa = 0;
  22.             if (start <= 100)
  23.             {
  24.                 sum += start;
  25.                 test(start + 1, sum);
  26.                 aaa = sum;
  27.             }
  28.             return aaa;

  29.         }
  30.         public int num(int i)
  31.         {
  32.             if (i == 100)
  33.             {
  34.                 return i;
  35.             }
  36.             else {
  37.                 return i + num(i + 1);
  38.             }
  39.         }
Copy code
Recursive algorithmIt is a sub-problem that transforms the problem into a similar problem that is reduced in scale. Then recursively call the function (or procedure) to represent the solution to the problem. A process (or function) directly or indirectly calls itself, and this process (or function) is called a recursive process (or function).Recursive algorithmRecursive processes are generally implemented through functions or subprocedures. Recursive method: Directly or indirectly call your own algorithm inside a function or subprocedure.




Previous:Campus projects urgently recruit partnership agents - waiting for you
Next:Ashley Madison website database download, total 9.69G, with Thunderbolt BT download link
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com