3.3、3.4 for循环(单重循环、多重循环)

【重点】for(int i = 0; i < 26; i += 2)中的i只在这个for循环中起作用,当变量比较多时,适合用这种定义变量的方法。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int i = 5;
	for (int i = 0; i < 26; ++i)
		cout << char('a'+i);//用char强制类型转换 
	cout << endl;
	for (int i = 0; i < 26; i+=2)
		cout << char('A'+i);
	cout << endl;
	return 0;
}
 

total:计数器

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int total = 0;
	int n, m;
	cin >> n >> m;
	for (int i = 1; i < n; ++i)
	{
		for(int j = i+1; j <= n; ++j)
		{
			if(m % (i+j) == 0)//(i+j)为m的因子 
			{
				++ total;
			}
		}
	}
	cout << total;
	return 0;	
} 

猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/81008539