Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a7d4ca5

Browse files
upload
1 parent dd7a6d5 commit a7d4ca5

File tree

8 files changed

+258
-125
lines changed

8 files changed

+258
-125
lines changed

‎3.C++程序设计/week4/编程练习/1.cpp

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎3.C++程序设计/week4/编程练习/2.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎3.C++程序设计/week4/编程练习/3.cpp

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <cstdlib>
4+
using namespace std;
5+
6+
class BigInt
7+
{
8+
private:
9+
string num;
10+
char sign;
11+
public:
12+
BigInt(string n = "0");
13+
BigInt operator+(const BigInt &bi);
14+
BigInt operator-(const BigInt &bi);
15+
BigInt operator*(const BigInt &bi);
16+
BigInt operator/(const BigInt &bi);
17+
friend ostream& operator<<(ostream &o, const BigInt &b);
18+
};
19+
20+
BigInt::BigInt(string n)
21+
{
22+
if (n[0] == '-')
23+
{
24+
sign = '-';
25+
num = n.substr(1, n.length() - 1);
26+
}
27+
else
28+
{
29+
sign = '+';
30+
num = n;
31+
}
32+
}
33+
34+
BigInt BigInt::operator+(const BigInt &bi)
35+
{
36+
string operand1 = num;
37+
string operand2 = bi.num;
38+
39+
int maxLength = operand1.length() > operand2.length() ? operand1.length() : operand2.length();
40+
while (operand2.length() < operand1.length())
41+
{
42+
operand2.insert(0, "0");
43+
}
44+
while (operand1.length() < operand2.length())
45+
{
46+
operand1.insert(0, "0");
47+
}
48+
49+
char *result = new char[maxLength + 1];
50+
result[maxLength] = '0円';
51+
int carry = 0;
52+
for (int i = maxLength - 1; i >= 0; i--)
53+
{
54+
int add1 = operand1[i] - '0';
55+
int add2 = operand2[i] - '0';
56+
int add = (add1 + add2 + carry) % 10;
57+
result[i] = '0' + add;
58+
carry = (add1 + add2 + carry) / 10;
59+
}
60+
61+
BigInt tmp(result);
62+
if (carry)
63+
{
64+
tmp.num = "1" + tmp.num;
65+
}
66+
tmp.sign = sign;
67+
return tmp;;
68+
}
69+
70+
BigInt BigInt::operator-(const BigInt &bi)
71+
{
72+
string operand1 = num;
73+
string operand2 = bi.num;
74+
BigInt tmp;
75+
if (operand1 == operand2)
76+
{
77+
tmp.num = "0";
78+
tmp.sign = '+';
79+
return tmp;
80+
}
81+
82+
int maxLength = 0;
83+
char tmpSign = '+';
84+
if (operand1.length() < operand2.length() || (operand1.length() == operand2.length() && operand1 < operand2))
85+
{
86+
tmpSign = '-';
87+
swap(operand1, operand2);
88+
}
89+
maxLength = operand1.length();
90+
while (operand2.length() < operand1.length())
91+
{
92+
operand2.insert(0, "0");
93+
}
94+
95+
char *result = new char[maxLength + 1];
96+
result[maxLength] = '0円';
97+
int borrow = 0;
98+
for (int i = maxLength - 1; i >= 0; i--)
99+
{
100+
int minus1 = operand1[i] - '0';
101+
int minus2 = operand2[i] - '0';
102+
int minus = minus1 - minus2 - borrow;
103+
if (minus < 0)
104+
{
105+
minus += 10;
106+
borrow = 1;
107+
}
108+
else
109+
{
110+
borrow = 0;
111+
}
112+
result[i] = '0' + minus;
113+
}
114+
115+
tmp.num = result;
116+
while (tmp.num[0] == '0')
117+
{
118+
tmp.num.erase(tmp.num.begin());
119+
}
120+
tmp.sign = tmpSign;
121+
return tmp;
122+
}
123+
124+
BigInt BigInt::operator*(const BigInt &bi)
125+
{
126+
string operand1 = num;
127+
string operand2 = bi.num;
128+
BigInt tmp;
129+
BigInt result;
130+
result.sign = '+';
131+
132+
for (int i = 0; i < operand2.length(); i++)
133+
{
134+
tmp.num = "0";
135+
for (int j = 0; j < operand2[i] - '0'; j++)
136+
{
137+
tmp = tmp + *this;
138+
}
139+
tmp.num.resize(tmp.num.length() + i, '0');
140+
result = result + tmp;
141+
}
142+
return result;
143+
}
144+
145+
BigInt BigInt::operator/(const BigInt &bi)
146+
{
147+
string operand1 = num;
148+
string operand2 = bi.num;
149+
BigInt quotient;
150+
quotient.sign = '+';
151+
if (operand1.length() < operand2.length())
152+
{
153+
quotient.num = "0";
154+
return quotient;
155+
}
156+
157+
int m = operand1.length() - operand2.length();
158+
operand2.resize(operand1.length(), '0');
159+
BigInt dividend(operand1);
160+
BigInt divisor(operand2);
161+
for (int i = 0; i <= m; i++)
162+
{
163+
int tmp = 0;
164+
while ((dividend - divisor).sign != '-')
165+
{
166+
tmp++;
167+
dividend = dividend - divisor;
168+
}
169+
quotient.num += ('0' + tmp);
170+
divisor.num.resize(divisor.num.length() - 1);
171+
}
172+
while (quotient.num[0] == '0')
173+
{
174+
quotient.num.erase(quotient.num.begin());
175+
}
176+
return quotient;
177+
}
178+
179+
180+
181+
ostream& operator<<(ostream &o, const BigInt &b)
182+
{
183+
if (b.sign == '+')
184+
{
185+
o << b.num;
186+
}
187+
else if (b.sign == '-')
188+
{
189+
o << "-" << b.num;
190+
}
191+
return o;
192+
}
193+
194+
int main()
195+
{
196+
string s1, s2;
197+
char Operator;
198+
cin >> s1 >> Operator >> s2;
199+
BigInt a(s1);
200+
BigInt b(s2);
201+
BigInt result;
202+
203+
switch (Operator)
204+
{
205+
case '+':
206+
result = a + b;
207+
break;
208+
case '-':
209+
result = a - b;
210+
break;
211+
case '*':
212+
result = a * b;
213+
break;
214+
case '/':
215+
result = a / b;
216+
break;
217+
}
218+
cout << result << endl;
219+
return 0;
220+
}

‎4.算法基础/week2/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Week2:枚举
2+
3+
## 枚举的基本思想
4+
5+
枚举是基于已有知识进行答案猜测的一种问题求解策略。
6+
7+
枚举从可能的集合中一一列举各元素, 对问题可能解集合的每一项,根据问题给定的检验条件判定哪些是成立的,使条件成立的即是问题的解。
8+
9+
### 枚举过程
10+
11+
- 判断猜测的答案是否正确
12+
- 进行新的猜测: 有两个关键因素要注意
13+
- 猜测的结果必须是前面的猜测中没有出现过的.
14+
- 猜测的过程中要及早排除错误的答案.
15+
16+
### 枚举中的三个关键问题
17+
18+
问题一:给出解空间
19+
20+
- 给出解空间,建立简洁的数学模型
21+
- 模型中变量数尽可能少, 它们之间相互独立
22+
23+
问题二:减少搜索空间
24+
25+
- 利用知识缩小模型中各变量的取值范围, 避免不必要的计算
26+
- 减少代码中循环体执行次数
27+
28+
问题三:合适的搜索顺序
29+
30+
- 搜索空间的遍历顺序要与模型中条件表达式一致
31+
32+
例:求小于N的最大素数
33+
34+
- 2是素数, 记为PRIM0
35+
- 根据PRIM0, PRIM1 , ..., PRIMk, 寻找比PRIMk大的最小素数PRIMk+1
36+
- 如果PRIMk+1大于N, 则PRIMk是我们需要找的素数, 否则继续寻找
37+
- 解空间:2到N的所有素数
38+
- 减小搜索空间:排除所有奇数
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /