'commit'
This commit is contained in:
19
TangDou/LuoGuBook/第8章模拟与高精度/P1009_1.cpp
Normal file
19
TangDou/LuoGuBook/第8章模拟与高精度/P1009_1.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
//本题有坑,使用长整也过不了洛谷的用例,因为它的用例是22,
|
||||
//需要高精度,也就是乘法高精度+加法高精度
|
||||
typedef long long LL;
|
||||
int n;
|
||||
LL res;
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) { // 1~n
|
||||
LL f = 1; // 基底
|
||||
for (int j = 1; j <= i; j++) f *= j; // f =n!
|
||||
res += f; // 1!+2!+3!+...+n!
|
||||
}
|
||||
cout << res << endl;
|
||||
return 0;
|
||||
}
|
||||
24
TangDou/LuoGuBook/第8章模拟与高精度/P1009_2.cpp
Normal file
24
TangDou/LuoGuBook/第8章模拟与高精度/P1009_2.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
//高精度+高精度
|
||||
|
||||
//高精度*低精度
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
LL res = 0;
|
||||
LL f = 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// f =i!
|
||||
// 3!= 3*2*1
|
||||
// 4!=4*3*2*1
|
||||
f *= i; //计算i! =(i-1)! * i 举栗子: 4!= 4* 3!
|
||||
res += f; //计算阶乘的和 res= 1!+2!+3!+...+n!
|
||||
}
|
||||
//输出要需要改
|
||||
cout << res << endl;
|
||||
return 0;
|
||||
}
|
||||
47
TangDou/LuoGuBook/第8章模拟与高精度/P1009_3.cpp
Normal file
47
TangDou/LuoGuBook/第8章模拟与高精度/P1009_3.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<int> add(vector<int> A, vector<int> B) {
|
||||
if (A.size() < B.size()) swap(A, B);
|
||||
vector<int> C;
|
||||
int t = 0;
|
||||
for (int i = 0; i < A.size(); i++) {
|
||||
t += A[i];
|
||||
if (i < B.size()) t += B[i];
|
||||
C.push_back(t % 10);
|
||||
t /= 10;
|
||||
}
|
||||
if (t) C.push_back(t);
|
||||
return C;
|
||||
}
|
||||
|
||||
vector<int> mul(vector<int> A, int b) {
|
||||
vector<int> C;
|
||||
int t = 0;
|
||||
// 8899 * 2 = 17798
|
||||
for (int i = 0; i < A.size() || t; i++) {
|
||||
if (i < A.size()) t += A[i] * b;
|
||||
C.push_back(t % 10);
|
||||
t /= 10;
|
||||
}
|
||||
while (C.size() > 1 && C.back() == 0) C.pop_back();
|
||||
return C;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
vector<int> F, RES;
|
||||
//初始化
|
||||
RES.push_back(0);
|
||||
F.push_back(1);
|
||||
for (int i = 1; i <= n; i++) { //
|
||||
F = mul(F, i);
|
||||
RES = add(RES, F);
|
||||
}
|
||||
//倒序输出
|
||||
for (int i = RES.size() - 1; i >= 0; i--)
|
||||
printf("%d", RES[i]);
|
||||
return 0;
|
||||
}
|
||||
33
TangDou/LuoGuBook/第8章模拟与高精度/P1009_Prepare.cpp
Normal file
33
TangDou/LuoGuBook/第8章模拟与高精度/P1009_Prepare.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
|
||||
//母题
|
||||
//求n阶乘
|
||||
LL f(int n) {
|
||||
LL s = 1;
|
||||
for (int i = 1; i <= n; i++) s *= i;
|
||||
return s;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//INT_MAX
|
||||
//2147483647
|
||||
// s=1!+2!+3!+...+n!
|
||||
// 3!=3*2*1
|
||||
// 4!=4*3*2*1 四步
|
||||
// 4!=4*3! 一步,3!我知道
|
||||
|
||||
//20!=20*19*18*....*1
|
||||
LL s = 0;
|
||||
for (int i = 1; i <= 20; i++) s += f(i);
|
||||
cout << s << endl;
|
||||
|
||||
|
||||
LL t = 1;
|
||||
s = 0;
|
||||
for (int i = 1; i <= 20; i++) t = t * i, s += t;
|
||||
cout << s << endl;
|
||||
return 0;
|
||||
}
|
||||
18
TangDou/LuoGuBook/第8章模拟与高精度/P1009_Study.cpp
Normal file
18
TangDou/LuoGuBook/第8章模拟与高精度/P1009_Study.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n; // n!+(n-1)!+(n-2)!+...+1!
|
||||
|
||||
int sum = 0; //结果,累加和
|
||||
for (int i = n; i >= 1; i--) { //枚举 n,n-1,n-2,....,1
|
||||
//计算n这个数字的阶乘
|
||||
int res = 1;
|
||||
for (int j = i; j >= 1; j--) res *= j;
|
||||
sum += res;
|
||||
}
|
||||
cout << sum << endl;
|
||||
return 0;
|
||||
}
|
||||
48
TangDou/LuoGuBook/第8章模拟与高精度/P1042.cpp
Normal file
48
TangDou/LuoGuBook/第8章模拟与高精度/P1042.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int N = 25 * 2500 + 10; //每行至多 25 个字母,最多有 2500 行。
|
||||
int a[N];//小华的输赢状态表
|
||||
int idx;
|
||||
|
||||
int main() {
|
||||
char op;
|
||||
while (true) {
|
||||
cin >> op;
|
||||
if (op == 'E') break;
|
||||
if (op == 'W')
|
||||
a[idx++] = 1;
|
||||
else if (op == 'L')
|
||||
a[idx++] = 0;
|
||||
}
|
||||
//开始分析11分
|
||||
int w = 0, l = 0;
|
||||
for (int i = 0; i < idx; i++) {
|
||||
if (a[i])w++; else l++;
|
||||
if ((w >= 11 || l >= 11) && abs(w - l) >= 2) {
|
||||
printf("%d:%d\n", w, l);
|
||||
//下一局开始,清零
|
||||
w = l = 0;
|
||||
}
|
||||
}
|
||||
//存在未打完的没分出胜负的
|
||||
printf("%d:%d\n", w, l);
|
||||
|
||||
//两种计分制中间需要有空行
|
||||
puts("");
|
||||
|
||||
//开始分析21分
|
||||
w = 0, l = 0;
|
||||
for (int i = 0; i < idx; i++) {
|
||||
if (a[i])w++; else l++;
|
||||
if ((w >= 21 || l >= 21) && abs(w - l) >= 2) {
|
||||
printf("%d:%d\n", w, l);
|
||||
//下一局开始,清零
|
||||
w = l = 0;
|
||||
}
|
||||
}
|
||||
printf("%d:%d\n", w, l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
37
TangDou/LuoGuBook/第8章模拟与高精度/P1067.cpp
Normal file
37
TangDou/LuoGuBook/第8章模拟与高精度/P1067.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
|
||||
//这模拟太啰嗦了~
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
int x;
|
||||
// a[0~n-1]是系数,a[n]是常数
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> x;
|
||||
if (x == 0) continue;
|
||||
|
||||
if (x < 0) {
|
||||
if (x == -1)
|
||||
cout << "-";
|
||||
else
|
||||
cout << x; //-1不要输出1,其它的-2,-3等如实输出
|
||||
} else { // x肯定大于0
|
||||
if (i > 0) cout << "+"; //第一位前面不能有+号
|
||||
if (x != 1) cout << x; //系数1不输出,2,3等正常输出
|
||||
}
|
||||
if (n - i > 1) cout << "x^" << n - i;
|
||||
else
|
||||
cout << "x"; // 1次方特殊
|
||||
}
|
||||
|
||||
//常数位
|
||||
cin >> x;
|
||||
if (x > 0)
|
||||
cout << "+" << x;
|
||||
else if (x < 0)
|
||||
cout << x;
|
||||
return 0;
|
||||
}
|
||||
54
TangDou/LuoGuBook/第8章模拟与高精度/P1067_0.cpp
Normal file
54
TangDou/LuoGuBook/第8章模拟与高精度/P1067_0.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
/*
|
||||
5
|
||||
100 -1 1 -3 0 10
|
||||
*/
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
int a;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> a;
|
||||
if (a > 0) {
|
||||
if (i > 0) cout << "+";
|
||||
if (a > 1) {
|
||||
if (n - i > 1)
|
||||
cout << a << "x^" << n - i;
|
||||
else
|
||||
cout << a << "x";
|
||||
}
|
||||
|
||||
else {
|
||||
if (n - i > 1)
|
||||
cout << "x^" << n - i;
|
||||
else
|
||||
cout << "x";
|
||||
}
|
||||
|
||||
} else if (a < 0) {
|
||||
if (a == -1) {
|
||||
if (n - i > 1)
|
||||
cout << "-x^" << n - i;
|
||||
else
|
||||
cout << "-x";
|
||||
} else {
|
||||
if (n - i > 1)
|
||||
cout << a << "x^" << n - i;
|
||||
else
|
||||
cout << a << "x";
|
||||
}
|
||||
}
|
||||
}
|
||||
cin >> a;
|
||||
if (a > 0)
|
||||
cout << "+" << a << endl;
|
||||
else if (a < 0)
|
||||
cout << a << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
-x^99-7x^50+100x^2+x+8
|
||||
-x^99-7x^50+100x^2+x^1+8
|
||||
*/
|
||||
48
TangDou/LuoGuBook/第8章模拟与高精度/P1067_1.cpp
Normal file
48
TangDou/LuoGuBook/第8章模拟与高精度/P1067_1.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
/*
|
||||
5
|
||||
100 -1 1 -3 0 10
|
||||
*/
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
int a;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> a;
|
||||
if (a > 0) {
|
||||
if (i > 0) cout << "+";
|
||||
if (a > 1) {
|
||||
if (n - i == 1)
|
||||
cout << a << "x";
|
||||
else
|
||||
cout << a << "x^" << n - i;
|
||||
} else {
|
||||
if (n - i == 1)
|
||||
cout << "x";
|
||||
else
|
||||
cout << "x^" << n - i;
|
||||
}
|
||||
|
||||
} else if (a < 0) {
|
||||
if (a == -1) {
|
||||
if (n - i == 1)
|
||||
cout << "-x";
|
||||
else
|
||||
cout << "-x^" << n - i;
|
||||
} else {
|
||||
if (n - i == 1)
|
||||
cout << a << "x";
|
||||
else
|
||||
cout << a << "x^" << n - i;
|
||||
}
|
||||
}
|
||||
}
|
||||
cin >> a;
|
||||
if (a > 0)
|
||||
cout << "+" << a << endl;
|
||||
else
|
||||
cout << a << endl;
|
||||
return 0;
|
||||
}
|
||||
48
TangDou/LuoGuBook/第8章模拟与高精度/P1067_2.cpp
Normal file
48
TangDou/LuoGuBook/第8章模拟与高精度/P1067_2.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
/*
|
||||
5
|
||||
100 -1 1 -3 0 10
|
||||
*/
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
int a;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> a;
|
||||
if (a > 0) {
|
||||
if (i > 0) cout << "+";
|
||||
if (a > 1) {
|
||||
if (n - i == 1)
|
||||
cout << a << "x";
|
||||
else
|
||||
cout << a << "x^" << n - i;
|
||||
} else {
|
||||
if (n - i == 1)
|
||||
cout << "x";
|
||||
else
|
||||
cout << "x^" << n - i;
|
||||
}
|
||||
|
||||
} else if (a < 0) {
|
||||
if (a == -1) {
|
||||
if (n - i == 1)
|
||||
cout << "-x";
|
||||
else
|
||||
cout << "-x^" << n - i;
|
||||
} else {
|
||||
if (n - i == 1)
|
||||
cout << a << "x";
|
||||
else
|
||||
cout << a << "x^" << n - i;
|
||||
}
|
||||
}
|
||||
}
|
||||
cin >> a;
|
||||
if (a > 0)
|
||||
cout << "+" << a << endl;
|
||||
else if (a < 0)
|
||||
cout << a << endl;
|
||||
return 0;
|
||||
}
|
||||
34
TangDou/LuoGuBook/第8章模拟与高精度/P1303.cpp
Normal file
34
TangDou/LuoGuBook/第8章模拟与高精度/P1303.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* 功能:高精度乘低精度模板
|
||||
* @param A
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
vector<int> mul(vector<int> &A, int b) {
|
||||
vector<int> C;
|
||||
int t = 0;
|
||||
for (int i = 0; i < A.size() || t; i++) {
|
||||
if (i < A.size()) t += A[i] * b;
|
||||
C.push_back(t % 10);
|
||||
t /= 10;
|
||||
}
|
||||
while (C.size() > 1 && C.back() == 0) C.pop_back();
|
||||
return C;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string a;
|
||||
int b;
|
||||
vector<int> A, C;
|
||||
cin >> a >> b;
|
||||
//倒着放的噢~
|
||||
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
|
||||
C = mul(A, b);
|
||||
//倒着输出噢~
|
||||
for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
|
||||
return 0;
|
||||
}
|
||||
44
TangDou/LuoGuBook/第8章模拟与高精度/P1303_2.cpp
Normal file
44
TangDou/LuoGuBook/第8章模拟与高精度/P1303_2.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* 功能:高精度乘高精度模板
|
||||
* @param A
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
vector<int> mul(vector<int> &A, vector<int> &B) {
|
||||
//初始化大小
|
||||
vector<int> C(A.size() + B.size());
|
||||
//先放里再说
|
||||
for (int i = 0; i < A.size(); i++)
|
||||
for (int j = 0; j < B.size(); j++)
|
||||
C[i + j] += A[i] * B[j];
|
||||
|
||||
//处理余数
|
||||
for (int i = 0, t = 0; i < C.size(); i++) {
|
||||
t += C[i];
|
||||
if (i >= C.size()) C.push_back(t % 10);
|
||||
else C[i] = t % 10;
|
||||
t /= 10;
|
||||
}
|
||||
//去掉前导0
|
||||
while (C.size() > 1 && C.back() == 0) C.pop_back();
|
||||
return C;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
//准备动作
|
||||
vector<int> A, B;
|
||||
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
|
||||
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
|
||||
//计算
|
||||
vector<int> C = mul(A, B);
|
||||
//倒序输出
|
||||
for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
22
TangDou/LuoGuBook/第8章模拟与高精度/P1328.md
Normal file
22
TangDou/LuoGuBook/第8章模拟与高精度/P1328.md
Normal file
@@ -0,0 +1,22 @@
|
||||
## 问题
|
||||
|
||||
### 1、需要整理一个胜负的关系表
|
||||
0 表示“剪刀”,1 表示“石头”,2 表示“布”,3 表示“蜥蜴人”,4表示“斯波克”。
|
||||
|
||||
前一个人的手势 后二个人的手势 前一个人的得分 数组表示
|
||||
0 0 0 g[0][0]=0
|
||||
0 1 0 g[0][1]=0
|
||||
0 2 1 g[0][2]=1
|
||||
0 3 1
|
||||
0 4 0
|
||||
|
||||
1 0 1
|
||||
1 1 0
|
||||
1 2 0
|
||||
1 3 1
|
||||
1 4 0
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
42
TangDou/LuoGuBook/第8章模拟与高精度/P1328_1.cpp
Normal file
42
TangDou/LuoGuBook/第8章模拟与高精度/P1328_1.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
//0 表示“剪刀”,1 表示“石头”,2 表示“布”,3 表示“蜥蜴人”,4表示“斯波克”。
|
||||
//看图打表写出对阵双方的胜负平结果二维数组
|
||||
//这个胜负平表是关键
|
||||
int g[5][5] = {
|
||||
{0, 0, 1, 1, 0},
|
||||
{1, 0, 0, 1, 0},
|
||||
{0, 1, 0, 0, 1},
|
||||
{0, 0, 1, 0, 1},
|
||||
{1, 1, 0, 0, 0}
|
||||
};
|
||||
|
||||
int n; //表示进行N次猜拳
|
||||
int n1; //小A出拳的周期长度
|
||||
int n2; //小B出拳的周期长度
|
||||
int na[N]; //表示小A出拳的规律
|
||||
int nb[N]; //表示小B出拳的规律
|
||||
int s1; //小A赢几次
|
||||
int s2; //小B赢几次
|
||||
|
||||
int main() {
|
||||
//数据读入
|
||||
cin >> n >> n1 >> n2;
|
||||
for (int i = 0; i < n1; i++)cin >> na[i];
|
||||
for (int i = 0; i < n2; i++)cin >> nb[i];
|
||||
|
||||
//开始猜拳
|
||||
for (int i = 0; i < n; i++) {
|
||||
//这一轮小A出的是啥?
|
||||
int a = na[i % n1]; //n1为循环的长度,所以每一轮后,都需要mod n1
|
||||
//这一轮小B出的是啥?
|
||||
int b = nb[i % n2]; //n2为循环的长度,所以每一轮后,都需要mod n2
|
||||
|
||||
s1 += g[a][b]; //根据事先推出的胜负平表,找出胜负平关系,累加到小A的得分中。
|
||||
s2 += g[b][a]; //根据事先推出的胜负平表,找出胜负平关系,累加到小A的得分中。
|
||||
}
|
||||
cout << s1 << " " << s2 << endl;
|
||||
return 0;
|
||||
}
|
||||
51
TangDou/LuoGuBook/第8章模拟与高精度/P1328_2.cpp
Normal file
51
TangDou/LuoGuBook/第8章模拟与高精度/P1328_2.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
|
||||
//本文件是示例数组下标从1开始如何计算
|
||||
//数组下标从1开始时,需要注意的问题: 前n-1个用MOD没有问题,
|
||||
// 关键是n的倍数时,需要输出n,而不是0,这个与下标从0开始时有区别!
|
||||
|
||||
//0 表示“剪刀”,1 表示“石头”,2 表示“布”,3 表示“蜥蜴人”,4表示“斯波克”。
|
||||
//看图打表写出对阵双方的胜负平结果二维数组
|
||||
//这个胜负平表是关键
|
||||
int g[5][5] = {
|
||||
{0, 0, 1, 1, 0},
|
||||
{1, 0, 0, 1, 0},
|
||||
{0, 1, 0, 0, 1},
|
||||
{0, 0, 1, 0, 1},
|
||||
{1, 1, 0, 0, 0}
|
||||
};
|
||||
|
||||
int n; //表示进行N次猜拳
|
||||
int n1; //小A出拳的周期长度
|
||||
int n2; //小B出拳的周期长度
|
||||
int na[N]; //表示小A出拳的规律
|
||||
int nb[N]; //表示小B出拳的规律
|
||||
int s1; //小A赢几次
|
||||
int s2; //小B赢几次
|
||||
|
||||
int main() {
|
||||
//数据读入
|
||||
cin >> n >> n1 >> n2;
|
||||
for (int i = 1; i <= n1; i++)cin >> na[i];
|
||||
for (int i = 1; i <= n2; i++)cin >> nb[i];
|
||||
|
||||
//开始猜拳
|
||||
for (int i = 1; i <= n; i++) {
|
||||
//这一轮小A出的是啥?
|
||||
int a = na[i % n1 == 0 ? n1 : i % n1];
|
||||
//n1为循环的长度,所以每一轮后,都需要mod n1
|
||||
//这一轮小B出的是啥?
|
||||
int b = nb[i % n2 == 0 ? n2 : i % n2];
|
||||
//n2为循环的长度,所以每一轮后,都需要mod n2
|
||||
|
||||
s1 += g[a][b]; //根据事先推出的胜负平表,找出胜负平关系,
|
||||
// 累加到小A的得分中。
|
||||
s2 += g[b][a]; //根据事先推出的胜负平表,找出胜负平关系,
|
||||
// 累加到小A的得分中。
|
||||
}
|
||||
cout << s1 << " " << s2 << endl;
|
||||
return 0;
|
||||
}
|
||||
35
TangDou/LuoGuBook/第8章模拟与高精度/P1563.cpp
Normal file
35
TangDou/LuoGuBook/第8章模拟与高精度/P1563.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
int n, m;
|
||||
const int N = 1e5 + 10;
|
||||
struct Node {
|
||||
int dir; //方向,0:圈内,1:圈外
|
||||
char name[120]; //姓名
|
||||
} c[N];
|
||||
|
||||
int main() {
|
||||
//读入数据
|
||||
scanf("%d%d", &n, &m);
|
||||
for (int i = 0; i < n; i++)
|
||||
scanf("%d%s", &c[i].dir, c[i].name);
|
||||
|
||||
//出发点
|
||||
int i = 0;
|
||||
while (m--) {
|
||||
// a==0 ,表示向左数 s 个人
|
||||
// a==1 ,表示向右数 s 个人
|
||||
int a, s;
|
||||
scanf("%d%d", &a, &s);
|
||||
if (a == 0 && c[i].dir == 0) //圈内
|
||||
i = (i - s + n) % n; //向前找
|
||||
else if (a == 0 && c[i].dir == 1) //圈内
|
||||
i = (i + s) % n; //向后找
|
||||
else if (a == 1 && c[i].dir == 0) //圈外
|
||||
i = (i + s) % n; //向后找
|
||||
else if (a == 1 && c[i].dir == 1) //圈外
|
||||
i = (i - s + n) % n; //向前找
|
||||
}
|
||||
printf("%s\n", c[i].name);
|
||||
return 0;
|
||||
}
|
||||
47
TangDou/LuoGuBook/第8章模拟与高精度/P1601.cpp
Normal file
47
TangDou/LuoGuBook/第8章模拟与高精度/P1601.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* 功能:高精度加法模板
|
||||
* @param A
|
||||
* @param B
|
||||
* @return
|
||||
*/
|
||||
vector<int> add(vector<int> &A, vector<int> &B) {
|
||||
//声明结果数组C
|
||||
vector<int> C;
|
||||
|
||||
//以长的为根基,进行计算
|
||||
if (A.size() < B.size()) return add(B, A);
|
||||
//进位
|
||||
int t = 0;
|
||||
//列竖式进行计算
|
||||
for (int i = 0; i < A.size(); i++) {
|
||||
t += A[i];//进位加法
|
||||
if (i < B.size()) t += B[i]; //存在这一位,就加
|
||||
C.push_back(t % 10);//保留余数
|
||||
t /= 10;//进位
|
||||
}
|
||||
//最后可能还有进位
|
||||
if (t) C.push_back(t);
|
||||
//返回
|
||||
return C;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//以字符串形式读入
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
//1、准备动作,将字符串的每一位,反序放入到整数运算数组中
|
||||
vector<int> A, B, C;
|
||||
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
|
||||
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
|
||||
|
||||
//2、调用模板进行计算
|
||||
C = add(A, B);
|
||||
|
||||
//3、输出结果,需要倒序噢
|
||||
for (int i = C.size() - 1; i >= 0; i--)printf("%d", C[i]);
|
||||
return 0;
|
||||
}
|
||||
33
TangDou/LuoGuBook/第8章模拟与高精度/P2670.cpp
Normal file
33
TangDou/LuoGuBook/第8章模拟与高精度/P2670.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
char a[N][N];
|
||||
int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};
|
||||
int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1}; //左右上下 左上 左下 右上 右下
|
||||
//
|
||||
int n, m;
|
||||
|
||||
int main() {
|
||||
scanf("%d%d", &n, &m);
|
||||
//计入地雷地图
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= m; j++)
|
||||
scanf("%d", &a[i][j]);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
if (a[i][j] == '*')
|
||||
printf("%c", '*');
|
||||
else {
|
||||
int cnt = 0;
|
||||
for (int k = 0; k < 8; k++)
|
||||
//当前点周边的*的个数是多少个?
|
||||
if (a[i + dx[k]][j + dy[k]] == '*') cnt++;
|
||||
printf("%d", cnt);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
42
TangDou/LuoGuBook/第8章模拟与高精度/P4924.cpp
Normal file
42
TangDou/LuoGuBook/第8章模拟与高精度/P4924.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 510;
|
||||
int n, m;
|
||||
int x, y, r, z;
|
||||
int tmp[N][N], res[N][N];
|
||||
int cnt;
|
||||
|
||||
// 数组res为最后输出的结果数组,tmp为辅助数组;
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
//初始化数据
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
res[i][j] = tmp[i][j] = ++cnt;
|
||||
|
||||
//读入魔法指令
|
||||
while (m--) { // m次指令
|
||||
cin >> x >> y >> r >> z;
|
||||
|
||||
//处理旋转
|
||||
if (z == 0) { //顺时针
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
tmp[x + j][y - i] = res[x + i][y + j];
|
||||
} else if (z == 1) { //逆时针
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
tmp[x - j][y + i] = res[x + i][y + j];
|
||||
}
|
||||
//复制回来
|
||||
memcpy(res, tmp, sizeof tmp);
|
||||
}
|
||||
//输出
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%2d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
60
TangDou/LuoGuBook/第8章模拟与高精度/P4924.md
Normal file
60
TangDou/LuoGuBook/第8章模拟与高精度/P4924.md
Normal file
@@ -0,0 +1,60 @@
|
||||
### P4924 [1007]魔法少女小Scarlet
|
||||
|
||||
#### 1、初始化数组的代码
|
||||
```c++
|
||||
int cnt = 0; //计数器
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
a[i][j] = ++cnt;
|
||||
```
|
||||
|
||||
....
|
||||
01 02 03 04 05 06 07 08 09
|
||||
10 11 12 13 14 15 15 17 18
|
||||
....
|
||||
|
||||
#### 2、%02d的格式控制
|
||||
```c++
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%02d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
```
|
||||
|
||||
#### 3、矩阵的旋转
|
||||
|
||||
我们模拟原位置(i,j)在第一象限,i>0,j>0
|
||||
逆时针90度,到达的位置是: (-j,i)
|
||||
顺时针90度,到达的位置是:(j,-i)
|
||||
180度,到达的位置:(-i,-j)
|
||||
|
||||
想方法背下来:
|
||||
|
||||
(1)1,3象限,i,j位置不变。
|
||||
2,4象限,i,j对调
|
||||
|
||||
(2)1象限是正的,3象限全是负的
|
||||
2象限x是负的,y是正的
|
||||
4象限x是正的,y是负的
|
||||
|
||||
#### 4、矩阵的遍历
|
||||
```c++
|
||||
//逆时针
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
f[x - j][y + i] = res[x + i][y + j];
|
||||
//顺时针
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
f[x + j][y - i] = res[x + i][y + j];
|
||||
```
|
||||
|
||||
#### 5、临时的矩阵变量
|
||||
防止出现自行覆盖的问题,采用的办法是做一个数组的拷贝,复制品。
|
||||
```c++
|
||||
memcpy(res, f, sizeof f);
|
||||
```
|
||||
|
||||
|
||||
|
||||
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_1.cpp
Normal file
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_1.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Rotate Image(二维数组逆时针旋转90度)
|
||||
const int N = 110;
|
||||
int n = 9, cnt;
|
||||
int res[N][N], f[N][N];
|
||||
|
||||
//矩阵旋转,需要额外开创建一个临时的二维数组(防止出现交换赋值时,出现覆盖的问题)
|
||||
void rotate(int x, int y, int r) { // x:第几行 y:以(x,y)点为中心,四个方向,向左r,向右r,转~
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++) //
|
||||
f[x - j][y + i] = res[x + i][y + j]; //副本修改
|
||||
|
||||
// 复制数组的正规写法
|
||||
memcpy(res, f, sizeof f); //将副本复制回到正本上
|
||||
|
||||
//复制
|
||||
// for (int i = 1; i <= n; i++)
|
||||
// for (int j = 1; j <= n; j++)
|
||||
// res[i][j] = f[i][j];
|
||||
}
|
||||
|
||||
int main() {
|
||||
//初始化填充数据
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
res[i][j] = f[i][j] = ++cnt;
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%02d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
//测试一下旋转
|
||||
rotate(6, 7, 1);
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%02d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_2.cpp
Normal file
50
TangDou/LuoGuBook/第8章模拟与高精度/P4924_2.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Rotate Image(二维数组顺时针旋转90度)
|
||||
const int N = 110;
|
||||
int n = 9, cnt;
|
||||
|
||||
int res[N][N], tmp[N][N];
|
||||
|
||||
//矩阵旋转,需要额外开创建一个临时的二维数组(防止出现交换赋值时,出现覆盖的问题)
|
||||
void rotate(int x, int y, int r) {
|
||||
for (int i = -r; i <= r; i++)
|
||||
for (int j = -r; j <= r; j++)
|
||||
//每一个位置都去了哪里
|
||||
tmp[x + j][y - i] = res[x + i][y + j];
|
||||
|
||||
//复制回来
|
||||
memcpy(res, tmp, sizeof tmp);
|
||||
// for (int i = 1; i <= n; i++)
|
||||
// for (int j = 1; j <= n; j++)
|
||||
// res[i][j] = tmp[i][j];
|
||||
}
|
||||
|
||||
int main() {
|
||||
//初始化数据
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
res[i][j] = tmp[i][j] = ++cnt;
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%2d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
//测试一下旋转
|
||||
rotate(6, 7, 1);
|
||||
|
||||
//输出结果
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
printf("%2d ", res[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
38
TangDou/LuoGuBook/第8章模拟与高精度/TD.cpp
Normal file
38
TangDou/LuoGuBook/第8章模拟与高精度/TD.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
typedef long long LL;
|
||||
/*
|
||||
5
|
||||
100 -1 1 -3 0 10
|
||||
|
||||
100x^5-x^4+x^3-3x^2+10
|
||||
*/
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n + 1; i++) {
|
||||
int a;
|
||||
cin >> a;
|
||||
if (a == 1) {
|
||||
if (i == 1)
|
||||
cout << "x^" << n - i + 1;
|
||||
else
|
||||
cout << "+x^" << n - i + 1;
|
||||
}
|
||||
|
||||
else if (a == -1)
|
||||
cout << "-"
|
||||
<< "x^" << n - i + 1;
|
||||
else if (a > 0) {
|
||||
if (i == 1)
|
||||
cout << a << "x^" << n - i + 1;
|
||||
else
|
||||
cout << "+" << a << "x^" << n - i + 1;
|
||||
} else
|
||||
cout << a << "x" << n - i + 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
3
TangDou/LuoGuBook/第8章模拟与高精度/Test.md
Normal file
3
TangDou/LuoGuBook/第8章模拟与高精度/Test.md
Normal file
@@ -0,0 +1,3 @@
|
||||
$f(x)=2x^2+2x+4$
|
||||
|
||||
$f(x)=x^5+3x^2+2x^2-9$
|
||||
Reference in New Issue
Block a user