c++程序设计(钱能)第二章习题答案

1.#include
using namespace std;
int main()
{
int sum = 0, b = 1;
for (int a = 1; a <= 3; ++a)
{
b *= a;
sum += b;
}
cout << sum;
}

2.#include
#include
#include
using namespace std;
int main()
{
int x;
double sum = 1, item = 1, a = 1, b = 1, c = -1;
cin >> x;
for (int n = 1; abs(item) > 1e-8; n++)
{
a *= x;
b *= n;
c *= -1;
item = a * c / b;
sum += item;
}
cout << sum;
}

//3.
#include
using namespace std;
int main()
{
int a, b, c;
for (int i = 100; 1 < 1000; i++)
{
a = i / 100;
b = (i - 100) / 10;
c = i % 10;
if (i == a * a * a + b * b * b + c * c * c)
{
cout << i;
}
}
}

//7.打印菱形
#include
#include
using namespace std;
int main()
{
for (int i = 0; i < 19; i++)
{
for (int j = 0; j < abs(19 / 2 - i); j++)
cout << " ";
for (int j = 0; j < 19 - 2 * abs(19 / 2 - i); j++)
cout << “%”;
cout << “\n”;
}
}

//8.1
#include
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 9 - i; j++)
{
cout << " ";
}
for (int k = 0; k < i + 1; k++)
{
cout << “#”;
}
cout << " ";
for (int a = 0; a <= i; a++)
{
cout << “$”;
}
cout << “\n”;
}
}

//8.2
#include
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < i; j++)
{
cout << " ";
}
for (int k = 0; k < 19 - 2 * i; k++)
{
if (k % 2 == 0)
{
cout << “S”;
}
else cout << “T”;
}
cout << “\n”;
}
}

//9.母牛问题
#include
using namespace std;
int main()
{
int one = 1, two=0, three=0, four=0, n;
cin>>n;
for (int i = 1; i < n; i++)
{
four = three + four;
three = two;
two = one;
one = four;
}
cout << (one + two + three + four);
}

//10
#include
using namespace std;
int main()
{
double n = 100.0, sum = 100.0;
for (int i = 0; i < 10; i++)
{
n = n / 2;
sum += n;
}
cout << “sum:” << sum << “, n:” << n;
}

//11.换钱
#include
using namespace std;
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j= 1; j <= 17; j++)
{
int k = 100 - 10 * i - 5 * j;
if (10 * i + 5 * j + k == 100 && k > 0)
{
cout << “i:” << i << “, j:” << j << “, k:” << k;
}
cout << “\n”;
}
}
}

//12.1
#include
using namespace std;
int main()
{
int k = 1, h, d;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 1; j++)
{
cout << k<<" ";
k++;
}
h = i;
for (d = 0; d < 6; d++)
{
cout << h << " ";
h++;
if (h > 6)
{
h = 0;
cout << h << " ";
h++;
}
}
if (h == 6 && d == 6)
{
cout << h;
}
cout << “\n”;
}
}

//12.2
#include
using namespace std;
int main()
{
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 7; j++)
{
cout << “(” << i << “,” << j << “)”;
}
cout << “\n”;
}
}

//13.1
#include
using namespace std;
int main()
{
int d = 1;
cout << "* ";
for (int i = 1; i <= 9; i++)
{
cout << i << “\t”;
}
cout << “\n”;
cout << “--------------------------------------------------------------------------”;
cout << “\n”;
for (int i = 1; i <= 9; i++)
{
for (int m = 1; m < 2; m++)
{
cout << d << " ";
d++;
}
for (int j = 1; j <= i; j++)
{
int k = i * j;
cout << k << “\t”;
}
cout << “\n”;
}
}

//13.2
#include
using namespace std;
int main()
{
int d = 1;
cout << "* ";
for (int i = 1; i <= 9; i++)
{
cout << i << “\t”;
}
cout << “\n”;
cout << “--------------------------------------------------------------------------”;
cout << “\n”;
for (int i = 1; i <= 9; i++)
{
for (int m = 1; m < 2; m++)
{
cout << d << " ";
d++;
}
for (int j = 1; j <= 9; j++)
{
if (j < i)
{
cout << " " << “\t”;
}
else
{
int k = i * j;
cout << k << “\t”;
}
}
cout << “\n”;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值