第一题:无穷级数的计算
编写一个结构 Fraction{int a,b},表示分数,其中分子为 a,分母为 b。为简化计算,约定分母为正整
数,分子为整数。输出一个分数应化简分子分母,例如分数{-9,6}应当输出为-1(1/2)。 编写函数Fraction(Fraction
a,Fraction b)计算分数的和,void Print(Fraction a)输出分数。在main()函数中计算数列1+1/2+…+1/n的分数值并输出(n由用户输入)。测试你的代码,n到多少时结果就不正确了?尽可能使有效的n值最大。
#include<iostream>
using namespace std;
class Fraction {
public:
//初始化为0
Fraction() {
this->numerator = 0;
this->denominator = 0;
this->integer = 0;
}
Fraction(int numerator, int denominator, int integer = 0) {
this->numerator = numerator;
this->denominator = denominator;
this->integer = integer;
}
long long GetDenominator() {
return this->denominator;
}
long long GetNumerator() {
return this->numerator;
}
long long GetInteger() {
return this->integer;
}
void SetValue(long long numerator, long long denominator, long long integer = 0) {
this->numerator = numerator;
this->denominator = denominator;
this->integer = integer;
Simplify();
}
//化简分式的函数
void Simplify() {
//假分式转换成真分式
integer += numerator / denominator;
numerator -= (numerator / denominator) * denominator;
//化简真分式
for (long long i = 2; i <= numerator; i++) {
if (numerator % i == 0 && denominator % i == 0) {
numerator /= i;
denominator /= i;
i--;
}
}
}
//求后一个值加给前一个值
void Add(Fraction f) {
this->integer += f.integer;
this->numerator = f.GetDenominator() * this->numerator + this->denominator * f.GetNumerator();
this->denominator = this->denominator * f.GetDenominator();
Simplify();
}
//显示当前对象的private值
void ShowValue() {
cout << "分数形式为:" << this->integer << "(" << this->numerator << "/" << this->denominator << ")" << endl;
}
private:
long long numerator;
long long denominator;
long long integer;
};
int main() {
int n;
cin >> n;
Fraction f(1, 1);
for (int i = 2; i <= n; i++) {
Fraction f1(1, i);
f.Add(f1);
}
f.ShowValue();
return 0;
}
第二题:超级扫雷
构建扫雷棋盘,输入尺寸和雷数量(w,h,n,8<=w<=80,8<=h<=20,0<n<wh),在棋盘范围wh内随机布n个雷。按扫雷规则计算棋盘中所有区域的数字(数字为该位置8邻域中雷的数量),打印到屏幕上(0不显示,雷显示为*)。
例如输入
10 10 8输出为:
由于使用了随机数,你的输出可以和上图不同,但需要满足规则和输出格式。
提示:雷区是一个二维平面,可以设计为二维字符数组。雷区数组数值定义为:10为地雷,0-8为该点周边雷数
80 * 20的扫雷有什么意思,来个100 * 100的扫雷~
#include<iostream>
#include<ctime>
using namespace std;
void Print(int arr[100][100], int width, int height) {
for (int k = 0; k < width; k++) cout << "-";
cout << endl;
for (int i = 1; i < height; i++) {
for (int j = 1; j < width; j++) {
if (arr[i][j] == 0) cout << " ";
else if (arr[i][j] == 100) cout << "*";
else cout << arr[i][j];
}
cout << endl;
}
}
int main() {
srand((unsigned int)time(NULL));
int width, height, num, arr[100][100] = { 0 };
cin >> width >> height >> num;
for (int i = 0; i < num; i++) {
//防止访问到界外,导致报错
int row = rand() % height + 1, column = rand() % width + 1;
if (arr[row][column] < 100) {
arr[row][column] = 100;
//对地雷周围的数都进行标记
if (arr[row - 1][column - 1] < 100) arr[row - 1][column - 1]++;
if (arr[row - 1][column] < 100) arr[row - 1][column]++;
if (arr[row - 1][column + 1] < 100) arr[row - 1][column + 1]++;
if (arr[row][column - 1] < 100) arr[row][column - 1]++;
if (arr[row][column + 1] < 100) arr[row][column + 1]++;
if (arr[row + 1][column - 1] < 100) arr[row + 1][column - 1]++;
if (arr[row + 1][column] < 10) arr[row + 1][column]++;
if (arr[row + 1][column + 1] < 100) arr[row + 1][column + 1]++;
}
else i--;
}
Print(arr, width, height);
return 0;
}
注:由于尺寸过大,栈区可能被挤爆,可以考虑将数据存放到堆区。