首页 > 其他分享 >3.1 模拟 参考代码

3.1 模拟 参考代码

时间:2023-07-25 18:56:38浏览次数:36  
标签:ch cur int 代码 printf ++ 3.1 include 模拟

P2670 [NOIP2015 普及组] 扫雷游戏

#include <cstdio>
char mine[105][105];
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%s", mine[i]);
    for (int i = 0; i < n; i++) {
    	for (int j = 0; j < m; j++) {
    		if (mine[i][j] == '*') continue; // 跳过这一次循环
    		int cnt = 0;
    		for (int k = 0; k < 8; k++) {
    			int x = i + dx[k], y = j + dy[k];
    			if (x >= 0 && x < n && y >= 0 && y < m && mine[x][y] == '*')
					cnt++;
			}
			mine[i][j] = '0' + cnt;
		}
	}
	for (int i = 0; i < n; i++) printf("%s\n", mine[i]);
    return 0;
}

P1042 [NOIP2003 普及组] 乒乓球

#include <cstdio>
#include <string>
using namespace std;
int score[2] = {11, 21};
int main() {
	char ch = getchar();
	string record;
	while (ch != 'E') {
		if (ch == 'W' || ch == 'L')	record += ch;
		ch = getchar();
	}
	int len = record.length();
	for (int i = 0; i < 2; i++) {
		int x = 0, y = 0;
		for (int j = 0; j < len; j++) {
			if (record[j] == 'W') x++;
			else y++;
			if (max(x, y) >= score[i] && abs(x - y) >= 2) {
				printf("%d:%d\n", x, y);
				x = y = 0;
			}
		}
		printf("%d:%d\n", x, y);
		if (i == 0) printf("\n");
	}
	return 0;
}

P1563 [NOIP2016 提高组] 玩具谜题

#include <cstdio>
struct Toy {
    int d;
    char name[15];  
};
Toy t[100005];
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d%s", &t[i].d, t[i].name);
    }
    int cur = 0;
    for (int i = 0; i < m; i++) {
        int a, s;
        scanf("%d%d", &a, &s);
        int flag = -1;
        if (a + t[cur].d == 1) flag = 1;
        cur = (cur + n + flag * s) % n; 
    }
    printf("%s\n", t[cur].name);
    return 0;
}

P4924 [1007] 魔法少女小Scarlet

#include <cstdio>
using namespace std;
int a[505][505], tmp[505][505];
int n;
void print() {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j)
            printf("%d ", a[i][j]);
        printf("\n");
    }
}
void rotate(int x, int y, int len, int d) {
    for (int i = 0; i < len; ++i)
        for (int j = 0; j < len; ++j)
            if (d == 1) tmp[x + i][y + j] = a[x + j][y + len - i - 1];
            else tmp[x + i][y + j] = a[x + len - j - 1][y + i];
    for (int i = 0; i < len; ++i)
        for (int j = 0; j < len; ++j)
            a[x + i][y + j] = tmp[x + i][y + j];
}
int main()
{
    int m;
    scanf("%d%d", &n, &m);
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            a[i][j] = ++cnt;
    for (int i = 0; i < m; i++) {
        int x, y, r, z;
        scanf("%d%d%d%d", &x, &y, &r, &z);
        rotate(x - r, y - r, 2 * r + 1, z);
    }
    print();
    return 0;
}

P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布

#include <cstdio>
int res[5][5] = {
    {0, -1, 1, 1, -1}, 
    {1, 0, -1, 1, -1}, 
    {-1, 1, 0, -1, 1},
    {-1, -1, 1, 0, 1},
    {1, 1, -1, -1, 0}};
int a[205], b[205];
int main()
{
    int n, na, nb;
    scanf("%d%d%d", &n, &na, &nb);
    for (int i = 0; i < na; ++i) scanf("%d", &a[i]);
    for (int i = 0; i < nb; ++i) scanf("%d", &b[i]);
    int sa = 0, sb = 0;
    for (int i = 0; i < n; ++i) {
        int r = res[a[i % na]][b[i % nb]];
        if (r == 1) ++sa;
        else if (r == -1) ++sb;
    }
    printf("%d %d\n", sa, sb);
    return 0;
}

P1067 [NOIP2009 普及组] 多项式输出

#include <cstdio>
#include <cmath>
using namespace std;
int a[105];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i <= n; ++i) scanf("%d", &a[i]);
    int bg = 0;
    while (bg <= n && a[bg] == 0) ++bg;
    if (bg == n + 1) printf("0");
    for (int i = bg; i <= n; ++i) {
        if (a[i] == 0) continue;
        if (i != bg) {
            if (a[i] > 0) printf("+");
                else printf("-");
        } else if (a[i] < 0) printf("-");
        if (abs(a[i]) != 1 || i == n) printf("%d", abs(a[i]));
        if (i < n) {
            if (i == n - 1) printf("x");
            else printf("x^%d", n - i);
        }
    }
    return 0;
}

P1098 [NOIP2007 提高组] 字符串的展开

#include <iostream>
#include <string>
using namespace std;
string ans, s;
int p1, p2, p3;
bool is_lower(char ch) {
    return ch >= 'a' && ch <= 'z';
}
bool is_digit(char ch) {
    return ch >= '0' && ch <= '9';
}
bool can_expand(char l, char r) {
    if (is_lower(l) && is_lower(r) && l < r) return true;
    if (is_digit(l) && is_digit(r) && l < r) return true;
    return false;
}
string expand(int l, int r) {
    string ret;
    if (p1 == 3) {
        for (char ch = s[l] + 1; ch < s[r]; ++ch)
            for (int i = 0; i < p2; ++i) ret += '*';
    } else if (is_digit(s[l]) || p1 == 1) {
        if (p3 == 2) {
            for (char ch = s[r] - 1; ch > s[l]; --ch)
                for (int i = 0; i < p2; ++i) ret += ch;
        } else {
            for (char ch = s[l] + 1; ch < s[r]; ++ch)
                for (int i = 0; i < p2; ++i) ret += ch;
        }
    } else if (p1 == 2) {
        if (p3 == 2) {
            for (char ch = s[r] - 1; ch > s[l]; --ch)
                for (int i = 0; i < p2; ++i) ret += char(ch - 32);
        } else {
            for (char ch = s[l] + 1; ch < s[r]; ++ch)
                for (int i = 0; i < p2; ++i) ret += char(ch - 32);
        }
    }
    return ret;
}
int main()
{
    cin >> p1 >> p2 >> p3 >> s;
    int bg = 0;
    int len = s.length();
    int cur = -1;
    while (bg < len && cur < len) {
        cur = s.find('-', cur + 1);
        if (cur == -1) {
            ans += s.substr(bg);
            break;
        }
        if (cur != 0 && cur + 1 != s.size()) {
            if (can_expand(s[cur - 1], s[cur + 1])) {
                ans += s.substr(bg, cur - bg);
                ans += expand(cur - 1, cur + 1);
                ans += s[cur + 1];
                bg = cur + 2;
                ++cur;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two

#include <cstdio>
char s[15][15];
//      	 0   1  2  3
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool vis[15][15][4][15][15][4];
bool valid(int x) {
	return x>=0 && x<=9;
}
void one_minute(int &x, int &y, int &d) {
	int new_x = x + dx[d], new_y = y + dy[d];
	if (valid(new_x) && valid(new_y) && s[new_x][new_y]!='*') {
		x = new_x; y = new_y;
	} else {
		d = (d + 1) % 4;
	}
}
int main()
{
	int fx, fy, fd, cx, cy, cd;
	for (int i = 0; i < 10; i++) scanf("%s", s[i]);
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++) {
			if (s[i][j] == 'C') {
				cx = i; cy = j;
			}
			if (s[i][j] == 'F') {
				fx = i; fy = j;
			}
		}
	fd = cd = 0;
	int ans = 0;
	vis[fx][fy][fd][cx][cy][cd]=true;
	while (fx!=cx || fy!=cy) {	// 	!(fx==cx && fy==cy)
		one_minute(fx, fy, fd); 
		one_minute(cx, cy, cd);
		if (vis[fx][fy][fd][cx][cy][cd]) {
			ans=0; break;
		}
		vis[fx][fy][fd][cx][cy][cd]=true;
		ans++;
	}
	printf("%d\n", ans);
	return 0;
}

标签:ch,cur,int,代码,printf,++,3.1,include,模拟
From: https://www.cnblogs.com/ronchen/p/17580680.html

相关文章

  • 如何编写难以维护的React代码?——滥用useEffect
    如何编写难以维护的React代码?——滥用useEffect在许多项目中,我们经常会遇到一些难以维护的React代码。其中一种常见的情况是滥用useEffect钩子,特别是在处理衍生状态时。让我们来看一个例子:constComponentA=({list})=>{const[filteredList,setFilteredList]=useSta......
  • 使用gcc-13.1.0编译安装thrift-0.18.1
    执行configure生成Makefile,排除掉不需要的语言支持和测试等:./configure--prefix=/usr/local/thrift-0.18.1--with-boost=/usr/local/boost--with-libevent=/usr/local/libevent--with-openssl=/usr/local/openssl--with-cpp=yes--with-go=no--with-java=no--with-pyth......
  • 频繁FullGC的原因竟然是“开源代码”
    前言首先java语言的特性是不需像C和C++那样自己手动释放内存,因为java本身有垃圾回收机制(垃圾回收称为GC),顾名思义就是释放垃圾占用的空间,防止内存泄露。JVM运行时占用内存最大的空间就是堆内存,另外栈区和方法区也会占用空间但是占用有限本章就不探究了。那么堆中的空间又分为年轻......
  • CSP模拟4
    悲,昨天存本地忘发了,今天又不想写模拟5的。考了四道ARC就离谱。A.LIStoOriginalSequence首先考虑\(k=1\),唯一的方案就是倒序输出\(1\)到\(n\)。我们可以想到,这道题的方法是向已经确定的序列\(A\)中插入其他数。对于一个数\(x(x<A_i)\),是不能把它放在\(A_i......
  • python使用ctypes调用gcc编译的dll之g++编译c++代码
    1、在windows系统将cpp代码编译成可供python调用的dll1.1新建header.h代码如下#pragmaonce#defineDllExport__declspec(dllexport)extern"C"{DllExportvoidhello_world(void);}/*#pragmaonce用来防止某个头文件被多次include,这条指令就能够保证头文件被......
  • 分享一段禁止右键、ctrl+u、f12的代码
    //按键触发document.onkeydown=function(){//禁止ctrl+uif(event.ctrlKey&&window.event.keyCode==85){returnfalse;}//禁止F12......
  • vba-常用代码记录
    一些代码优化:1.Range("B5:C6").CopyDestination:=Range("B8")2.使用mid$函数而不是mid3.把整个sheet保护,需要操作时,后台先关闭保护。4.Excel中自带的Sum函数是将整个区域转换为数组,在内存中进行求和,速度快多了。DimresultAsDoubleresult=Application.Worksh......
  • 手写数字识别代码学习笔记
    图像预处理importtorchvision.transformsastransforms#定义数据预处理步骤【compose->组成】transform=transforms.Compose([transforms.Resize((128,128)),#将图像大小调整为128x128像素transforms.RandomCrop(100),#随机裁剪图像为10......
  • 链表的头插法(代码+内存图)
    头插法代码示例publicclassLinkDemo{publicstaticvoidmain(String[]args){//将这个数组按头插的方式插入列表int[]arr={1,2,3,4,5,6,7,8,9};headIndert(arr);}publicstaticvoidheadIndert(int[]arr){Nodeli......
  • uni-app 中模拟器真机运行项目
    1.安装模拟器MuMu 2.配置环境变量找到HbuilderX的安装目录,查找adb.exe文件,复制serve.exe所在文件目录的路径,配置到系统变量的Path中         3.在模拟器中进行配置注意:端口为7555不同模拟器的默认端口是不一样的adb的路径一定是HbuilderX的adb路径,使......