c++程序设计教程第二版 钱能 第二章答案

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

记录学习c++的点滴!

所有都写在了一起,如有改进,欢迎讨论!

主函数:

#include
#include
#include
#include

using namespace std;

int main()
{



system("pause");
}

 

将下面的1-9分别置于主函数内即可使用!

1、级数计算


	//-------------------------------------级数计算-------------------------//
	double item = 1;
	double sum = 1;
	double x;
	cout << "please input the value of x:";
	cin >> x;
	for (int demon = 1,  sgn = 1; abs(item) > 1e-8; sgn *= -1, demon *=(demon++),x*=x)
	{
		item = sgn*x/demon;
		sum = sum + item;
	}
	cout<

2、阶乘计算

//----------------------------------普通阶乘计算-----------------------//
int sum = 0;
	for (int i = 1,demon=1; i <= 4; i++,demon*=i)
	{
		sum += demon;
	}
	cout << sum << endl;


3、水仙花数

	//-----------------------------------水仙花数-------------------------//
	unsigned int a, b, c;
	for (int i = 100; i < 1000; i++)
	{
		a = i / 100;
		b = (i - a * 100) / 10;
		c = (i - a * 100 - b * 10);
		if (a*a*a + b*b*b + c*c*c == i)
			cout << i << endl;
	}


4、完数

//--------------------------------完数-----------------------------//
	for (int i = 2,sum=1; i < 1000; i++, sum = 1)
	{
		for (int j = 2; j < i; j++)
		{
			if (i%j == 0)
			{
				sum += j;
			}
		}
		if ((sum - i) == 0)
			cout << i << endl;
	}

5、对称素数

	//-------------------------一千以内对称素数-------------------------//
	for (int i = 100, a = 0, b = 0, c = 0,flag=1; i < 1000; i++, a = 0, b = 0, c = 0,flag=1)
	{
		for (int j = 2; j <= sqrt(i); j++)
		{
			if (i%j == 0)
			{
				flag = 0;
				break;
			}
		}	
		if (flag)
		{
			a = i / 100;
			b = (i - a * 100) / 10;
			c = i - 100 * a - 10 * b;
			if (a == c)
			cout << i << endl;
		}		
	}

6、棱形%图案

//---------------------------棱形%图案-------------------------------//
	for (int i = 1; i <= 19; i++)
	{
		if (i <= 10)
			cout << string(10 - i, ' ') + string(2 * i - 1, '%') << endl;
		else
			cout << string(i - 10, ' ') + string(39-2*i, '%') << endl;
	}
	

 

7、兑换金币

    //-----------------------------------兑换金币类问题-------------------//
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= 19; j++)
		{
			for (int k = 1; k <= 99; k++)
			{
				if ((10 * i + 5 * j + k) == 100 && (i+j+k) == 50)
					cout << i << ' ' << j << ' ' << k << endl;
			}
		}

	}

8、矩阵打印

	//----------------------第一个矩阵--------------------------//
	for (int i = 1; i <= 6; i++)
	{
		cout << i << string(5,' ');
		for (int j = 0; j < 7; j++)
		{
			cout << (i - 1 + j) % 7 << string(3,' ');
		}
		cout << endl;
	}

9、九九乘法表

	//-------------------------打印9*9乘法表----------------------//
	
	cout << '*'<

10、空格+交替ST图案

//----------------------------------空格+交替ST----------------------//
	for (int i = 1; i <= 10; i++)
	{
		cout << string(i-1,' ');
			for (int j = 1,sgn=-1; j <= (21 - 2 * i); j++,sgn*=-1)
		{
			if (sgn == -1)
				cout << 'S';
			else
				cout << 'T';
			
		}
			cout << endl;
	}

 

你可能感兴趣的:(c++,c++)