C++专项 第七讲:循环结构程序设计

一、输出所有的水仙花数

#include <iostream>
using namespace std;
int main()
{
	int n = 0;
	cout << "所有的水仙花数有:";
	for (int a = 1; a < 10; a++)
	{
		for (int b = 0; b < 10; b++)
		{
			for (int c = 0; c < 10; c++)
			{
				if (a*a*a + b*b*b + c*c*c == 100 * a + 10 * b + c)
				{
					cout << a << b << c << "\t";
					n = n + 1;
					if (n % 5 == 0)
					{
						cout << endl;
					}
				}
			}
		}
	}
	return 0;
}

二、穷举法练习:都要学C

#include <iostream>
using namespace std;
int main()
{
	for (int a = 0; a < 10; a++)
	{
		for (int b = 0; b < 10; b++)
		{
			for (int c = 0; c < 10; c++)
			{
				for (int d = 0; d < 10; d++)
				{
					if (4 * d == 8 && 3 * c == 0 && 2 * b == 0 && a == 2)
					{
						cout << "都 = " << a << endl;
						cout << "要 =" << b << endl;
						cout << "学 =" << c << endl;
						cout << "C = " << d << endl;
					}
				}
			}
		}
	}
	return 0;
}

三、穷举法:鸡兔同笼问题

#include <iostream>
using namespace std;
int main()
{
	for (int i = 1; i < 30; i++)
	{
		if (2 * i + 4 * (30 - i) == 90)
		{
			cout << "鸡的个数:" << i << endl;
			cout << "兔的个数:" << 30 - i << endl;
		}
	}
}

四、利用级数展开式求cos(x),在迭代中赋予变量term新的含义

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	double s = 0,x, term = 1, t;
	int n = 1;
	cout << "Input x:";
	cin >> x;
	t = -(x*x) / ((2 * n)*(2 * n - 1));
	while (fabs(term) >= 1e-5)
	{
		s = s + term;
		term = term*t;
		n++;
	}
	cout << "The Result is " << s << endl;
	return 0;
}

五、菲波那契数列

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	long a[40];
	a[0] = 1; a[1] = 1;
	for (int i = 2; i < 40; i++)
	{
		a[i] = a[i - 1] + a[i - 2];
	}
	int n = 0;
	for (int i = 0; i < 40; i++)
	{
		cout << setw(10) << a[i] << "  ";
		n += 1;
		if (n % 5 == 0)
			cout << endl;
	}
	return 0;
}

六、输出一个指定区间内的所有素数

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int m, n,t = 0;
	//检测输入的合法性!增强程序的健壮性!
	bool isWrong = true;
	while (isWrong)
	{
		cout << "请输入要查找素数的区别端点值m,n:";
		cin >> m >> n;
		if (m <= n)
		{
			isWrong = false;
		}
		else
		{
			cout << "输入错误!" << endl<<endl;
		}
	}
	cout << endl;
	for (int i = m; i <= n; i++)
	{
		bool prime = true;
		//检测i是不是素数
		for (int j = 2; j <= i - 1; j++)
		{
			if (i%j == 0)
			{
				prime = false;
				break;
			}
		}
		if (prime)
		{
			cout << i << "\t";
			t = t + 1;
			if (t % 10 == 0)
			{
				cout << endl;
			}
		}
	}
	cout << endl;
}

七、判断单个数m是否为素数

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int m;
	bool prime = true;
	cout << "请输入整数m:";
	cin >> m;
	int k = int(sqrt(m));
	for (int i = 2; i <= k; i++)
	{
		if (m%i == 0)
		{
			prime = false;
			break;
		}
	}
	if (prime)
	{
		cout << m << "是素数!" << endl;
	}
	else
	{
		cout << m << "不是素数!" << endl;
	}
}

八、求i的i次方的和

#include <iostream>
using namespace std;
int main()
{
	int n;
	cout << "请输入项数n:";
	cin >> n;
	long int sum = 0, t = 1;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			t = t*j;
		}
		sum = sum + t;
	}
	cout << "sum = " << sum << endl;
	return 0;
}

九、求1 + 1/2! + 1/3! +...1/n! +...

#include <iostream>
using namespace std;
int main()
{
	double sum = 0, t = 1;
	int n;
	cout << "请输入阶乘倒数求和的阶数n:";
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		t = t*i;
		sum = sum + 1.0/t;
	}
	cout << "sum = " << sum << endl;
	return 0;
}

十、求n!的和

#include <iostream>
using namespace std;
int main()
{
	//采用自顶向下,逐步求精的思想,外层循环为顶,内层循环为精
	//int sum = 0;
	//for (int i = 1; i <= 12; i++)
	//{
	//	int t = 1;
	//	for (int j = 1; j <= i; j++)
	//	{
	//		t = t*j;
	//	}
	//	sum = sum + t;
	//}
	//cout << "sum = " << sum << endl;
	//return 0;
	long int sum = 0, t = 1;
	int n;
	cout << "请输入阶乘求和的阶数n:";
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
	t = t*i;
	sum = sum + t;
	}
	cout << "n! = " << sum << endl;
	return 0;
}


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大观矩阵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值