栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++案例练习:循环

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++案例练习:循环

1.案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身

例如:1^3 + 5^3+ 3^3 = 153

请利用do...while语句,求出所有3位数中的水仙花数

#include 
using namespace std;
int main() {
	//
	int num = 100;
	do
	{
		int a = 0;
		int b = 0;
		int c = 0;

		a = num % 10;
		b = (num / 10) % 10;
		c = num / 100;

		if (a*a*a+b*b*b+c*c*c==num)
		{

			cout << num << endl;
			
		}

	num++;
	} 
	while (num < 1000);

	system("pause");

	return 0;
}

2.案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。

#include 
using namespace std;
int main() {
	for (int i = 1; i < 100; i++)
	{
		int a = 0;
		int b = 0;
		int c = 0;
		
		a = i % 7;
		b = i % 10;
		c = i/ 10;
		if (a == 0 || b == 7 || c == 7)
		{
			cout << "敲桌子" << endl;
		}
		else
		{ 
			cout << i << endl;
		}
		
	}


	system("pause");

	return 0;
}

3.简单的嵌套循环案例

int main() {
	for (int i = 1; i < 11; i++)
	{
		for (int j = 1; j <11 ; j++)
		{
			cout << " *" ;
		}
		cout << endl;
	}

 

int main() {
	for (int i = 1; i < 11; i++)
	{
		for (int j = 1; j  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022405.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号