Mooc 课程:程序设计基础——发现计算之美
李骏扬 、魏海坤 、仰燕兰 、朱蔚萍 、杨万扣
网址:https://www.icourse163.org/course/SEU-1003771004
递推求和
有数列 2/3、4/5、6/9、10/15、16/25、26/41、42/67……
从第三项 6/9 开始,分子是前两项分子之和,分母是前一个分数的分子分母之和,求此数列前30项的和(以小数形式输出即可)。
扩展要求:该数列求和的项数可以通过键盘输入(5分!!)。
#include <iostream>
using namespace std;
int main()
{
double a1 = 2, a2 = 4, a = 2;
double b1 = 3, b = 3;
int n;
cin >> n;
if (n < 1) cerr << "error!" << endl;
else if (n == 1) cout << a1 / b1 << endl;
else if (n == 2) cout << a1 / b1 + 0.8 << endl;
else
{
double sum = 0;
for (size_t i = 1; i <= n; i++)
{
sum += a / b;
double temp_a = a, temp_b = b;
a = a1 + a2;
b = a1 + b1;
a1 = a2;
a2 = temp_a;
b1 = temp_b;
}
cout << sum << endl;
}
return 0;
}
国际象棋棋盘
Way I
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
for (size_t i = 1; i <= 8; i++)
{
for (size_t j = 1; j <= 8; j++)
{
if ((i + j) % 2) cout << "█";
else cout << " ";
}
cout <<endl;
}
return 0;
}
Output:
Way II
或者把背景色改了:
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
system("color F0"); // change into white background, black word
for (size_t i = 1; i <= 8; i++)
{
for (size_t j = 1; j <= 8; j++)
{
if ((i + j) % 2 == 1) cout << "█";
else cout << " ";
}
cout <<endl;
}
return 0;
}
Output:
(感觉亮白色比白色效果好)
例如
system("color 0A")
;
其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
0=黑色
1=蓝色
2=绿色
3=湖蓝色
4=红色
5=紫色
6=黄色
7=白色
8=灰色
9=淡蓝色
A=淡绿色
B=淡浅绿色
C=淡红色
D=淡紫色
E=淡黄色
F=亮白色
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。