Informatics Olympiad through train ----- loop, nested loop

There are also many types of C++ loops, such as for loops and while loops. Below I will introduce the functions and usage methods of these two loops.

1. for loop

What is a for loop?

A for loop is a loop statement in a common programming language, and this loop statement consists of a loop body and loop conditions. Simply summarize the expression: for (definition of initial variables; loop conditions; variable changes)

example

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n=5, sum=0;
    for(int i=1;i<=n;i++)
    {
        sum++;
    }
    cout<<sum<<endl;
    return 0;
}

Output result: 5

Two. while loop

The while  loop statement executes a target statement repeatedly as long as the condition given in the parentheses is true .

example

#include <iostream>
using namespace std;
int main ()
{
   int n = 10;
   while( n < 20 )
   {
       cout << "n 的值:" << n << endl;
       n++;
   }
   return 0;
}

operation result:

Value of n: 10 
Value of n: 11 
Value of n: 12 
Value of n: 13 Value of n: 
14 Value 
of n: 15 Value of n: 16 
Value of n: 17 
Value 
of n: 18 
Value of n: 19

3. do-while loop

A do...while loop checks its condition at the end of the loop. The do...while loop is similar to the while loop, but the do...while loop ensures that the loop is executed at least once

example

#include <iostream>
using namespace std;
int main ()
{
   int a = 1;
   do
   {
       cout << a << endl;
       a++;
   }while( a < 10 );
   return 0;
}

operation result:

1
2
3
4
5
6
7
8
9

Four. Nesting

In C language, if-else, while, do-while, and for can all be nested in each other. The so-called nesting (Nest) means that a statement contains another statement, for example, there is a for inside a for, and a while inside a while, or a while inside a for, and an if-else inside a while, all of which are allowed.

example

#include<iostream>
using namespace std;
bool a[1001];//在数组被定义为布尔变量时初始值都为零 
int main()
{
//埃氏筛
//质数的倍数一定不是质数
//所以只要从最小的数开始对质数的倍数进行标记,就可以找出质数 
	int n;
	cin>>n;
	for(int i=2;i<=n;i++)
	{
		if(a[i]==1)//“0”代表是质数“1”代表不是 
		{
			continue;//意思是不进行下面的操作,直接进入新的循环 
		}
		cout<<i<<endl;
		for(int j=i;i*j<=n;j++)
		{
			a[i*j]=1;
		}
	}
	return 0;
}

If you enter 5

operation result:

2
3
5

Let's do some practice!

 

1.  For loop summation

【Description】

Use for loop. Calculate the sum of output 1+2+3+...+n.

【enter】

Enter n.

【Output】

As the title says, the sum.

【Input sample】

10

【Example of output】

55

 

2. Items

 

【Description】

For positive integers n, m, find s=1+2+3...+n, when added to which item, the value of s will exceed m?

【enter】

Enter m.

【Output】

output n.

【Input sample】

1000

【Example of output】

45

【hint】

【data range】

For all data: 1≤m≤40000

 

3. Last two digits

【Description】

What is the last two digits of the product of n 1992?

【enter】

Enter n.

【Output】

As in the last two digits of the title.

【Input sample】

3

【Example of output】

88

【hint】

【data range】

For all data: n<2000.

 

4. Factorial and

 

【Description】

Find S=1!+2!+3!+....+n!

【enter】

Enter a positive integer n.

【Output】

output s.

【Input sample】

3

【Example of output】

9

【hint】

【data range】

For all data: 1≤n≤10.

 

5. Sum of factorials

 

【Description】

Input n to calculate the last 66 bits of S=1!+2!+3!+…+n! (without the leading 0). n≤10 to the sixth power, n! represents the product of the first n positive integers.

【enter】

Enter n.

【Output】

As the title says, the sum.

【Input sample】

10

【Example of output】

37913

【hint】

【data range】

For all data, 1≤n≤999999.

 

Answer

one.

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int sum = 0;
	for(int i = 1; i<=n;i++)//for循环 
	{
		sum += i;//"+="的意思是把i里的值累加到c里 
	}
	cout<<sum;
	return 0;
}

two.

#include<iostream>
using namespace std;
int main()
{
	int m;
	cin>>m;
	int s=0,n=0;
	while(s<=m)
	{
		n++;
		s+=n;
	}
	cout<<n<<endl;
	return 0;
}

three.

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int i=1;
	int ans=1;
	do
	{
		ans=ans*1992%100;
		i++;
	}while(i<=n);
	cout<<ans<<endl;
	return 0;
}

Four.

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int S=0;
	for(int i=1;i<=n;i++)
	{
		int s=1;
		for(int j=1;j<=i;j++)
		{
			s*=j;
		}
		S+=s;
	}
	cout<<S<<endl;
	return 0;
}

five.

#include<iostream>
using namespace std;
int main()
{
	const int N=1e6;//const int  常变量 
	int n;
	cin>>n;
	long long S=0,s=1;
	for(int i=1;i<=n;i++)
	{
		s=s*i%N;
		S=(S+s)%N;
	}
	cout<<S<<endl;
	return 0;
 } 

Are you doing everything right?

If you can do the first question correctly, it means that you have initially mastered the for loop. If you can do the second question correctly, it means that you have initially mastered the while loop. If you can do the third question correctly, It means that you have initially mastered the do whole loop. If you can do the fourth question correctly, it means that you have basically mastered the nested loop. If you can still do the fifth question correctly, it means that you have mastered these skills proficiently , you can start the study of the next chapter

 

Guess you like

Origin blog.csdn.net/m0_73220913/article/details/131253201