发文章只是为了督促自己做题,双非大二刚转科班的菜菜一枚,代码仅供参考,不足之处望理解。
P2670 [NOIP2015 普及组] 扫雷游戏
#include<iostream>
using namespace std;
int n, m, i = 0, j = 0;
char ch[105][105] = { 0 };
int dilei(int i, int j, int n, int m) {
int cnt = 0, flag = 0;
if (ch[i][j] == '*'){
flag = 1;
}
else {
if (ch[i - 1][j - 1] == '*')
cnt++;
if (ch[i - 1][j] == '*')
cnt++;
if (ch[i - 1][j + 1] == '*')
cnt++;
if (ch[i][j - 1] == '*')
cnt++;
if (ch[i][j + 1] == '*')
cnt++;
if (ch[i + 1][j - 1] == '*')
cnt++;
if (ch[i + 1][j] == '*')
cnt++;
if (ch[i + 1][j + 1] == '*')
cnt++;
}
if (flag == 0)
return cnt;
else
return -1;
}
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> ch[i][j];
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (dilei(i, j, n, m) >= 0)
cout << dilei(i, j, n, m);
else
cout << "*";
}
cout << endl;
}
return 0;
}
P1042 [NOIP2003 普及组] 乒乓球
#include<iostream>
#include<math.h>
using namespace std;
char ch[2501][25] = { '0' };
int i = 0, j = 0;
void eleven(char ch[2501][25]) {
int cntw = 0, cntl = 0;
for (i = 0; i < 2501; i++) {
for (j = 0; j < 25; j++) {
if (ch[i][j] == 'W')
cntw++;
else if (ch[i][j] == 'L')
cntl++;
else
goto flag1;
if ((cntw >= 11 || cntl >= 11)) {
if (abs(cntw - cntl) < 2) {
continue;
}
else {
cout << cntw << ":" << cntl << endl;
cntw = 0, cntl = 0;
}
}
}
}
flag1:
cout << cntw << ":" << cntl << endl;
}
void twentyone(char ch[2501][25]) {
int cntw = 0, cntl = 0;
for (i = 0; i < 2501; i++) {
for (j = 0; j < 25; j++) {
if (ch[i][j] == 'W')
cntw++;
else if (ch[i][j] == 'L')
cntl++;
else
goto flag1;
if ((cntw >= 21 || cntl >= 21)) {
if (abs(cntw - cntl) < 2) {
continue;
}
else {
cout << cntw << ":" << cntl << endl;
cntw = 0, cntl = 0;
}
}
}
}
flag1:
cout << cntw << ":" << cntl << endl;
}
int main() {
for (i = 0; i < 2501; i++) {
for (j = 0; j < 25; j++) {
flag1:
cin>>ch[i][j];
while (ch[i][j] == 'E')
goto flag2;
}
}
flag2:
eleven(ch);
cout << endl;
twentyone(ch);
return 0;
}
P1563 [NOIP2016 提高组] 玩具谜题
#include<iostream>
using namespace std;
struct toy {
int dir;
string name;
};
struct act {
int a, s;
};
toy to[100005];
act ac[100005];
int n, m, i = 0, j = 0, k = 0;
int main() {
cin >> n >> m;
for (i = 0; i < n; i++)
cin >> to[i].dir >> to[i].name;
for (i = 0; i < m; i++) {
cin >> ac[i].a >> ac[i].s;
if (to[k].dir == 0 && ac[i].a == 0)
k = (k + (n - ac[i].s)) % n;
else if (to[k].dir == 0 && ac[i].a == 1)
k = (k + ac[i].s) % n;
else if (to[k].dir == 1 && ac[i].a == 0)
k = (k + ac[i].s) % n;
else if (to[k].dir == 1 && ac[i].a == 1)
k = (k + (n - ac[i].s)) % n;
}
cout << to[k].name;
return 0;
}
P1601 A+B Problem(高精)
#include<iostream>
#include<vector>
using namespace std;
string A, B;
vector<int>a, b, c;
vector<int>add(vector<int>& a, vector<int>& b) {
int t = 0;
for (int i = 0; i < a.size() || i < b.size(); i++) {
if (i < a.size())
t += a[i];
if (i < b.size())
t += b[i];
c.push_back(t % 10);
t /= 10;
}
if (t)
c.push_back(1);
return c;
}
int main() {
cin >> 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');
auto c = add(a, b);
for (int i = c.size() - 1; i >= 0; i--)
cout << c[i];
return 0;
}
P1303 A*B Problem
#include<iostream>
#include<vector>
using namespace std;
string A, B;
vector<int>a, b;
vector<int>mul(vector<int>& a, vector<int>& b) {
vector<int>c(a.size() + b.size() + 5, 0);
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < b.size(); j++)
c[i + j] += a[i] * b[j];
int t = 0;
for (int i = 0; i < c.size(); i++) {
t += c[i];
c[i] = t % 10;
t /= 10;
}
while (c.size() > 1 && c.back() == 0)
c.pop_back();
return c;
}
int main() {
cin >> 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');
auto c = mul(a, b);
for (int i = c.size() - 1; i >= 0; i--)
cout << c[i];
return 0;
}
P1009 [NOIP1998 普及组] 阶乘之和
#include<iostream>
using namespace std;
const int value = 1e5;
void jiecheng(int* a, int n) {
a[0] = 1;
for (int k = 1; k < value; k++)
a[k] = 0;
int i, j;
for (i = 2; i <= n; i++) {
int jw = 0;
for (j = 0; j < value; j++) {
int s = a[j] * i + jw;
jw = s / 10;
a[j] = s % 10;
}
}
}
int sum[value];
void qiuhe(int* a) {
int jw = 0;
for (int i = 0; i < value; i++) {
sum[i] = sum[i] + a[i] + jw;
jw = sum[i] / 10;
sum[i] %= 10;
}
}
int main() {
int n, m; cin >> n;
int a[value] = { 0 };
for (int i = 1; i <= n; i++) {
jiecheng(a, i);
qiuhe(a);
}
int i;
for (i = value - 1; i >= 0; i--) {
if (sum[i] == 0)
continue;
break;
}
for (i; i >= 0; i--)
cout << sum[i];
return 0;
}
P4924 [1007] 魔法少女小Scarlet
#include<iostream>
using namespace std;
int n, m, k, q = 1;
int a[505][505] = { 0 };
int f[505] = { 0 }, s[505] = { 0 }, t[505] = { 0 }, v[505] = { 0 };
struct act{
int x, y, r, z;
}act[505];
void play(int x, int y, int r, int z) {
int x1 = x - r, y1 = y - r;
int xx = x - r, yy = y - r,k = 2 * r;
for (int i = 1; i <= k; i++, yy++)
f[i] = a[xx][yy];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, xx++)
s[i] = a[xx][yy + k];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, yy--)
t[i] = a[xx + k][yy + k];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, xx--)
v[i] = a[xx + k][yy];
xx = x1, yy = y1;
if (z == 0) {
for (int i = 1; i <= k; i++) {
int temp = f[i];
f[i] = v[i];
v[i] = t[i];
t[i] = s[i];
s[i] = temp;
}
}
else {
for (int i = 1; i <= k; i++) {
int temp = f[i];
f[i] = s[i];
s[i] = t[i];
t[i] = v[i];
v[i] = temp;
}
}
for (int i = 1; i <= k; i++, yy++)
a[xx][yy] = f[i];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, xx++)
a[xx][yy + k] = s[i];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, yy--)
a[xx + k][yy + k] = t[i];
xx = x1, yy = y1;
for (int i = 1; i <= k; i++, xx--)
a[xx + k][yy] = v[i];
xx = x1, yy = y1;
r--;
if (r > 0)
play(x, y, r, z);
}
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = q;
q++;
}
}
for (int i = 1; i <= m; i++) {
cin >> act[i].x >> act[i].y >> act[i].r >> act[i].z;
play(act[i].x, act[i].y, act[i].r, act[i].z);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
#include<iostream>
using namespace std;
int n, na, nb, cnta = 0, cntb = 0;
int a[205] = { 0 }, b[205] = { 0 };
int caiquan[5][5] = { {0,2,1,1,2},
{1,0,2,1,2},
{2,1,0,2,1},
{2,2,1,0,1},
{1,1,2,2,0}
};
int main(){
cin >> n >> na >> nb;
for (int i = 0; i < na; i++)
cin >> a[i];
for (int i = na; i < n; i++)
a[i] = a[i - na];
for (int i = 0; i < nb; i++)
cin >> b[i];
for (int i = nb; i < n; i++)
b[i] = b[i - nb];
for (int i = 0; i < n; i++) {
int x = a[i];
int y = b[i];
if (x == y)
continue;
if (caiquan[x][y] == 1)
cnta++;
if (caiquan[x][y] == 2)
cntb++;
}
cout << cnta << " " << cntb;
return 0;
}
P1067 [NOIP2009 普及组] 多项式输出
#include<iostream>
using namespace std;
int n, i = 0;
int a[105];
void fuhaomax(int a) {
if (a == 1)
return;
else if (a == -1)
cout << "-";
else
cout << a;
}
void fuhao1(int a) {
if (a == 1)
cout << "+";
else if (a == -1)
cout << "-";
else if (a > 0)
cout << "+" << a;
else
cout << a;
}
void fuhao0(int a) {
if (a > 0)
cout << "+" << a;
else
cout << a;
}
int main(){
cin >> n;
for (i = 0; i <= n; i++)
cin >> a[i];
if (n == 0) {
cout << a[0];
goto key1;
}
for (i = 0; i <= n; i++) {
if (a[i] != 0) {
if (n - i == n) {
fuhaomax(a[i]);
cout << "x^" << n;
}
else if (n - i == 1) {
fuhao1(a[i]);
cout << "x";
}
else if (n - i == 0) {
fuhao0(a[i]);
}
else {
fuhao1(a[i]);
cout << "x^" << n - i;
}
}
}
key1:
return 0;
}
P1098 [NOIP2007 提高组] 字符串的展开
这题太恶心了,看完题解发现三种情况没有考虑,后来给补上了,我的 if-else 思路可能写的不太好,但是能过
#include<iostream>
#include<cstring>
using namespace std;
int p1, p2, p3, i = 0, j = 0, len = 0;
char in[101];
char out[10000];
char shuzi[10] = { '0','1','2','3','4','5','6','7','8','9' };
char xiaoxie(char ch) {
if (ch >= 'A' && ch <= 'Z')
return ch + 32;
else
return ch;
}
char daxie(char ch) {
if (ch >= 'a' && ch <= 'z')
return ch - 32;
else
return ch;
}
int IsShuzi(char ch) {
for (int p = 0; p < 10; p++) {
if (ch == shuzi[p]) {
return 1;
break;
}
}
return 0;
}
int main() {
cin >> p1 >> p2 >> p3;
cin >> in;
len = strlen(in);
int flag = 0;
while (flag == 0 && in[i] == '-') {
out[j] = in[i];
i++;
j++;
}
flag = 1;
for (i; i < len; i++) {
if (in[i] != '-') {
out[j] = in[i];
j++;
}
else {
if (in[i + 1] == '-') {
while (in[i] == '-') {
out[j] = in[i];
j++;
i++;
}
out[j] = in[i];
j++;
}
else {
if (in[i + 1] == in[i - 1] + 1) {
out[j] = in[i + 1];
i++;
j++;
}
else if (in[i - 1] >= in[i + 1]) {
out[j] = in[i];
j++;
}
else if (in[i - 1] < in[i + 1]) {
if (IsShuzi(in[i - 1]) == 1 && IsShuzi(in[i + 1]) == 0) {
out[j] = in[i];
j++;
}
else {
int x = in[i + 1] - in[i - 1];
if (p1 == 1) {
if (p3 == 1) {
int t = in[i - 1] + 1;
char min = t;
char max = t + x - 1;
for (char ch = xiaoxie(min); ch < xiaoxie(max); ch += 1) {
for (int r = 0; r < p2; r++) {
out[j] = ch;
j++;
}
}
}
else {
int t = in[i - 1] + 1;
char min = t;
char max = t + x - 2;
for (char ch = xiaoxie(max); ch >= xiaoxie(min); ch -= 1) {
for (int r = 0; r < p2; r++) {
out[j] = ch;
j++;
}
}
}
}
else if (p1 == 2) {
if (p3 == 1) {
int t = in[i - 1] + 1;
char min = t;
char max = t + x - 1;
for (char ch = daxie(min); ch < daxie(max); ch += 1) {
for (int r = 0; r < p2; r++) {
out[j] = ch;
j++;
}
}
}
else {
int t = in[i - 1] + 1;
char min = t;
char max = t + x - 2;
for (char ch = daxie(max); ch >= daxie(min); ch -= 1) {
for (int r = 0; r < p2; r++) {
out[j] = ch;
j++;
}
}
}
}
else if (p1 == 3) {
for (int r = 0; r < (x - 1) * p2; r++) {
out[j] = '*';
j++;
}
}
}
}
}
}
}
cout << out << endl;
return 0;
}
P1591 阶乘数码
#include<iostream>
using namespace std;
int t, n, x;
int a[100000];
int ans[11];
void jiecheng(int* a, int n, int x) {
a[0] = 1;
for (int k = 1; k < 100000; k++)
a[k] = 0;
int i, j;
for (i = 2; i <= n; i++) {
int jw = 0;
for (j = 0; j < 100000; j++) {
int s = a[j] * i + jw;
jw = s / 10;
a[j] = s % 10;
}
}
int cnt = 0, flag = 0;
for (int i = 99999; i >= 0; i--) {
if (a[i] != 0)
flag = 1;
if (flag)
if (a[i] == x)
cnt++;
}
static int k = 0;
ans[k] = cnt; k++;
}
int main() {
cin >> t;
int flag = 0;
for (int i = 0; i < t; i++) {
cin >> n >> x;
jiecheng(a, n, x);
}
for (int i = 0; i < t; i++)
cout << ans[i] << endl;
return 0;
}
P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two
注意结构体在函数中的传参(下学期c语言II要好好跟着大一补):
point farmer;
void fun(point *p){ p-> }
int main () { fun(&farmer); }
#include<iostream>
using namespace std;
char ch[12][12];
int i = 0, j = 0;
struct point {
int x, y, dir;
};
point farmer, cow;
void movefun(point* p) {
switch (p->dir % 4) {
case 0:
if (ch[p->x - 1][p->y] == '*')
p->dir++;
else {
ch[p->x - 1][p->y] = ch[p->x][p->y];
ch[p->x][p->y] = '.';
p->x--;
}
break;
case 1:
if (ch[p->x][p->y + 1] == '*')
p->dir++;
else {
ch[p->x][p->y + 1] = ch[p->x][p->y];
ch[p->x][p->y] = '.';
p->y++;
}
break;
case 2:
if (ch[p->x + 1][p->y] == '*')
p->dir++;
else {
ch[p->x + 1][p->y] = ch[p->x][p->y];
ch[p->x][p->y] = '.';
p->x++;
}
break;
case 3:
if (ch[p->x][p->y - 1] == '*')
p->dir++;
else {
ch[p->x][p->y - 1] = ch[p->x][p->y];
ch[p->x][p->y] = '.';
p->y--;
}
break;
}
}
int main(){
for (i = 0; i <= 11; i++)
ch[0][i] = '*', ch[11][i] = '*';
for (i = 1; i <= 10; i++)
ch[i][0] = '*', ch[i][11] = '*';
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cin >> ch[i][j];
if (ch[i][j] == 'F')
farmer.x = i, farmer.y = j, farmer.dir = 0;
if (ch[i][j] == 'C')
cow.x = i, cow.y = j, cow.dir = 0;
}
}
int time = 0, flag = 0;
while (time <= 1000) {
movefun(&farmer);
movefun(&cow);
time++;
if (farmer.x == cow.x && farmer.y == cow.y) {
flag = 1;
break;
}
}
if (flag == 1)
cout << time;
else
cout << flag;
return 0;
}
P1065 [NOIP2006 提高组] 作业调度方案
这题看完题解后还没能理解......
#include<iostream>
using namespace std;
int m, n, ans = 0;
struct Information {
int id, cost;
} informa[21][21];
int queue[501];int machine_time[21][100001];int step[21];int last_time[21];
int main(){
cin >> m >> n;
for (int i = 1; i <= m * n; i++)
cin >> queue[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> informa[i][j].id;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> informa[i][j].cost;
for (int i = 1; i <= m * n; i++) {
int now = queue[i];
step[now]++;
int id = informa[now][step[now]].id;
int cost = informa[now][step[now]].cost;
int s = 0;
for (int j = last_time[now] + 1; ; j++) {
if (machine_time[id][j] == 0)
s++;
else
s = 0;
if (s == cost) {
for (int k = j - cost + 1; k <= j; k++)
machine_time[id][k] = 1;
if (j > ans)
ans = j;
last_time[now] = j;
break;
}
}
}
cout << ans;
return 0;
}
P1786 帮贡排序
#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct person {
string name, lastposition, nowposition;
long long money;
int grade, num;
}p[111];
int change(string s) {
if (s == "BangZhu") return 0;
if (s == "FuBangZhu") return 1;
if (s == "HuFa") return 2;
if (s == "ZhangLao") return 3;
if (s == "TangZhu") return 4;
if (s == "JingYing") return 5;
if (s == "BangZhong") return 6;
}
bool cmp(person p1, person p2) {
if (p1.money == p2.money)
return p1.num < p2.num;
else
return p1.money > p2.money;
}
bool cmpp(person p1, person p2) {
if (change(p1.nowposition) == change(p2.nowposition)) {
if (p1.grade == p2.grade)
return p1.num < p2.num;
else
return p1.grade > p2.grade;
}
else
return change(p1.nowposition) < change(p2.nowposition);
}
int main(){
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].name >> p[i].lastposition >> p[i].money >> p[i].grade;
p[i].num = i;
}
sort(p + 3, p + n, cmp);
for (int i = 0; i < n; i++) {
if (i == 0) p[i].nowposition = "BangZhu";
else if (i == 1 || i == 2) p[i].nowposition = "FuBangZhu";
else if (i == 3 || i == 4) p[i].nowposition= "HuFa";
else if (i >= 5 && i <= 8) p[i].nowposition = "ZhangLao";
else if (i >= 9 && i <= 15) p[i].nowposition = "TangZhu";
else if (i >= 16 && i <= 40) p[i].nowposition = "JingYing";
else p[i].nowposition = "BangZhong";
}
sort(p, p + n, cmpp);
for (int i = 0; i < n; i++)
cout << p[i].name << " " << p[i].nowposition << " " << p[i].grade << endl;
return 0;
}
P1249 最大乘积
这题有数学基础,思路会快,理解不了,记住就好:
正整数n分解成若干个互不相同的自然数的和,且使这些自然数的乘积最大:
1.先找最小k使 (sum=)2+3+4+...+k >= n
2.分类讨论: 1.相等时: 2 3 4 ... k 即为所求
2.sum=n+1时: 3 4 5 ... k k+1 即为所求
3.sum=n+x(x>1)时: 2 3 ... x-1 x+1 ... k 即为所求
#include<iostream>
using namespace std;
int n, s = 0, flag = 0;
int sum[100000];
void jiecheng(int* a, int n) {
a[0] = 1;
for (int k = 1; k < 100000; k++)
a[k] = 0;
int i, j;
for (i = 2; i <= n; i++) {
int jw = 0;
for (j = 0; j < 100000; j++) {
int s = a[j] * i + jw;
jw = s / 10;
a[j] = s % 10;
}
}
}
void chufa(int* a, int n) {
int r = 0;
for (int i = 100000 - 1; i >= 0; i--) {
r = r * 10 + a[i];
a[i] = r / n;
r %= n;
}
}
int main(){
cin >> n;
for (int i = 2; i < 200; i++) {
s += i;
int x = s - n;
if (x >= 0) {
flag = i;
if (x == 0) {
for (int i = 2; i <= flag; i++)
cout << i << " ";
jiecheng(sum, i);
}
else if (x == 1) {
for (int i = 3; i < flag; i++)
cout << i << " ";
cout << i + 1;
x = 2 * i;
jiecheng(sum, i + 1);
chufa(sum, x);
}
else {
for (int i = 2; i < x; i++)
cout << i << " ";
for (int i = x + 1; i <= flag; i++)
cout << i << " ";
jiecheng(sum, i);
chufa(sum, x);
}
break;
}
}
cout << endl;
int i;
for (i = 100000 - 1; i >= 0; i--) {
if (sum[i] == 0)
continue;
break;
}
for (i; i >= 0; i--)
cout << sum[i];
return 0;
}
P1045 [NOIP2003 普及组] 麦森数
这题第一问是数学问题: (2^p)+1的位数 == (2^p)的位数 == (p*lg2)+1的位数文章来源:https://www.toymoban.com/news/detail-826620.html
注意第二问一个一个的×2,会超时(得70points)文章来源地址https://www.toymoban.com/news/detail-826620.html
#include<iostream>
#include<math.h>
using namespace std;
int p, flag = 0;
int a[500] = { 1 };
void chengfa(int* a, int cnt, int x) {
for (int i = 0; i < cnt; i++) {
int jw = 0;
for (int j = 0; j < 500; j++) {
int s = a[j] * x + jw;
jw = s / 10;
a[j] = s % 10;
}
}
}
int main(){
cin >> p;
cout << (int)(p * log10(2)) + 1 << endl;
if (p > 10000) {
int t = pow(2, 20);
int m = p / 20;
int n = p - 20 * m;
chengfa(a, m, t);
chengfa(a, n, 2);
}
else {
chengfa(a, p, 2);
}
a[0] --;
for (int i = 500 - 1; i >= 0; i--) {
cout << a[i];
flag++;
if (flag % 50 == 0)
cout << endl;
}
return 0;
}
到了这里,关于洛谷题单算法1-1模拟与高精度的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!