7.24 Learning notes nesting for loops, exhaustive iterations for loops

Source: Internet
Author: User

Nesting for loops
Enter a positive integer to find the factorial and

Nesting
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
Console.WriteLine ("The result is:" + sum);
Console.ReadLine ();
99 Table of formulas
for (int i = 1; I <= 9; i++)
{
for (int j = 1; J <= I; j + +)
{
Console.Write ("{0}*{1}={2}\t", j,i, (J*i));
}
Console.Write ("\ n");
}

Console.ReadLine ();

Triangle at right-hand corner
for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= 5-i; j + +)
{
Console.Write ("");
}
for (int j = 1; J <= I; j + +)
{
Console.Write ("-");
}
Console.WriteLine ();
}

Console.ReadLine ();

Types of problems that loops can solve
Exhaustive, go through all the possible situations, and use the IF condition to filter out conditions that satisfy the condition.

Unit to send a 150-dollar shopping card,
Take the supermarket to buy three kinds of cleaning supplies.
Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan.
Just spend 150 yuan, how many kinds of buying method,
How do you buy every kind of buying method?
Shampoo x 10
Toothbrush y 30
Soap Z 75
int ci = 0;
int Biao = 0;
for (int x = 0; x <= × x + +)
{
for (int y = 0; y <=; y++)
{
for (int z = 0; z <=; z++)
{
ci++;
if (x + y * 5 + z * 2 = = 150)
{
biao++;
Console.WriteLine ("{0} Buy Method: Shampoo {1} bottle, toothbrush {2} branch, soap {3} block. ", Biao, X, Y, z);
}
}
}
}
Console.WriteLine ("There are a total of {0} buying methods.") ", Biao);
Console.WriteLine (CI);
Console.ReadLine ();

Iterative, from the initial situation according to the law and constantly solve the intermediate situation, the final derivation of the results.

paper can be folded in half, paper thickness of 0.07 mm.
            Q How many times can I fold at least 8848?
            Double height = 0.07;//8848m=8848000
            int ci = 0;
            while (height<=8848000)
             {
               ci++;
              height *= 2;//height=height*2;
           }
            Console.WriteLine (CI);
            console.readline ();

7.24 Learning notes nesting for loops, exhaustive iterations for loops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.