模拟小练

2024-06-07 03:58
文章标签 模拟 小练

本文主要是介绍模拟小练,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

昨天和今天做了一下模拟。有些感触。总结一下。

第一题:

Poj 1068 点击打开链接

题目和括号配对有点联系,从而就想到了用栈。一开始没有想到用,突然就来了灵感A了。算是用了正确的数据结构吧。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;const int N=45;
struct Bracksts{char c;int d;
}b[N];int main(){int T;scanf("%d",&T);while(T--){int n,p[N],top=0;char s[N];scanf("%d",&n);p[0]=0;for(int i=1;i<=n;i++)scanf("%d",&p[i]);for(int i=1;i<=n;i++){for(int j=1;j<=p[i]-p[i-1];j++)s[top++]='(';s[top++]=')';}s[top]='\0';for(int i=0;i<2*n;i++){b[i].c=s[i];b[i].d=i;}stack<Bracksts> st;st.push(b[0]);for(int i=1;i<2*n;i++){if(b[i].c=='(') st.push(b[i]);else {Bracksts deom=st.top();st.pop();printf("%d ",(b[i].d-deom.d+1)/2);}}printf("\n");}return 0;
}

第二题:

Poj 2632 点击打开链接

这道题,让我写了不少时间,很是费力,冗余也特别多。我想模拟应该及时的调试应该有好的效果吧!

#include <cstdio>
#include <cstring>
#include <algorithm>
#define CLR(arr,val) memset(arr,val,sizeof(arr))
using namespace std;const int N=105;
const char d[]={'N','W','S','E'};
int Map[N][N];
int n,m,a,b;
struct Robot{int x,y;int dir;
}robot[N];int  Move(int q,char p,int r){if(p=='L') {robot[q].dir=(robot[q].dir+r)%4;
//        printf("%d %d %d\n",robot[q].x,robot[q].y,robot[q].dir);return -1;}if(p=='R') {robot[q].dir=(robot[q].dir-r+4000)%4;return -1;}if(p=='F'){if(robot[q].dir==0){//Nfor(int i=1;i<=r;i++){if(robot[q].x+i>b) return 0;if(Map[robot[q].x+i][robot[q].y])return Map[robot[q].x+i][robot[q].y];}Map[robot[q].x][robot[q].y]=0;robot[q].x=robot[q].x+r;}if(robot[q].dir==1){//Wfor(int i=1;i<=r;i++){if(robot[q].y-i<1) return 0;if(Map[robot[q].x][robot[q].y-i])return Map[robot[q].x][robot[q].y-i];}Map[robot[q].x][robot[q].y]=0;robot[q].y=robot[q].y-r;}if(robot[q].dir==2){//Sfor(int i=1;i<=r;i++){if(robot[q].x-i<1) return 0;if(Map[robot[q].x-i][robot[q].y])return Map[robot[q].x-i][robot[q].y];}Map[robot[q].x][robot[q].y]=0;robot[q].x=robot[q].x-r;}if(robot[q].dir==3){//Efor(int i=1;i<=r;i++){if(robot[q].y+i>a) return 0;if(Map[robot[q].x][robot[q].y+i])return Map[robot[q].x][robot[q].y+i];}Map[robot[q].x][robot[q].y]=0;robot[q].y=robot[q].y+r;}Map[robot[q].x][robot[q].y]=q;}return -1;
}int main(){
//    freopen("1.txt","r",stdin);int T;scanf("%d",&T);while(T--){CLR(Map,0);scanf("%d%d",&a,&b);scanf("%d%d",&n,&m);int i;char demo;for(i=1;i<=n;i++){scanf("%d%d %c",&robot[i].y,&robot[i].x,&demo);for(int j=0;j<4;j++){if(d[j]==demo)robot[i].dir=j;}Map[robot[i].x][robot[i].y]=i;}int No_Robot[N],rep[N];char action[N];for(int j=1;j<=m;j++)scanf("%d %c%d",&No_Robot[j],&action[j],&rep[j]);for(i=1;i<=m;i++){int flag=Move(No_Robot[i],action[i],rep[i]);if(flag==0){printf("Robot %d crashes into the wall\n",No_Robot[i]);break;}//0代表撞到墙了啊.else if(flag>0){printf("Robot %d crashes into robot %d\n",No_Robot[i],flag);break;//>0其他代表其他的Robot上了.}}if(i>m) printf("OK\n");}return 0;
}

该题需要注意的是:方向的正确性,一定要保证转弯时候正确性。


第三题:

Poj 1573 点击打开链接

这道题在hd上A过,又A了一次。比较简单。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;const int N=13;
int Vis[N][N],R,C,start;
char map[N][N];void solve(){int nowx=1,nowy=start,step=0;Vis[1][start]=0;while(1){if(map[nowx][nowy]=='N') nowx=nowx-1;else if(map[nowx][nowy]=='S') nowx=nowx+1;else if(map[nowx][nowy]=='W') nowy=nowy-1;else if(map[nowx][nowy]=='E') nowy=nowy+1;step++;
//        for(int i=1;i<=R;i++){
//            for(int j=1;j<=C;j++)
//            printf("%d ",Vis[i][j]);
//            printf("\n");
//        }
//        printf("-----------\n");if(nowx<1||nowx>R||nowy<1||nowy>C){printf("%d step(s) to exit\n",step);break;}else if(Vis[nowx][nowy]!=-1) {printf("%d step(s) before a loop of %d step(s)\n",Vis[nowx][nowy],step-Vis[nowx][nowy]);break;}Vis[nowx][nowy]=step;}
}
int main(){while(scanf("%d%d%d",&R,&C,&start)&&(R||C||start)){memset(Vis,-1,sizeof(Vis));for(int i=1;i<=R;i++){for(int j=1;j<=C;j++)cin>>map[i][j];}solve();}return 0;
}

第四题:

Poj 2993&&1993 点击打开链接

这两道题的输入输出交换了一下,处理繁杂。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;const int N=100;
const char Type[]={'K','Q','R','B','N','P'};
struct Pieces{int x;char y,type;
}W[N],B[N];bool cmpW(Pieces x1,Pieces y1){int numberx,numbery;for(int i=0;i<=5;i++){if(x1.type==Type[i]) numberx=i;if(y1.type==Type[i]) numbery=i;}if(numberx<numbery) return true;else if(numberx==numbery){if(x1.x<y1.x) return true;else if(x1.x==y1.x&&x1.y<y1.y) return true;else return false;}else return false;
}bool cmpB(Pieces x1,Pieces y1){int numberx,numbery;for(int i=0;i<=5;i++){if(x1.type==Type[i]) numberx=i;if(y1.type==Type[i]) numbery=i;}if(numberx<numbery) return true;else if(numberx==numbery){if(x1.x>y1.x) return true;else if(x1.x==y1.x&&x1.y<y1.y) return true;else return false;}else return false;
}int main(){char demo;int topW=0,topB=0;for(int i=17;i>=1;i--){for(int j=0;j<33;j++){cin>>demo;if(demo>='A'&&demo<='Z'){W[topW].x=i/2;W[topW].y=j/4+'a';W[topW].type=demo;topW++;}else if(demo>='a'&&demo<='z'){B[topB].x=i/2;B[topB].y=j/4+'a';B[topB].type=demo-32;topB++;}}}sort(W,W+topW,cmpW);sort(B,B+topB,cmpB);printf("White: ");for(int i=0;i<topW;i++){if(W[i].type!='P')printf("%c%c%d%c",W[i].type,W[i].y,W[i].x,i==topW-1?'\n':',');else printf("%c%d%c",W[i].y,W[i].x,i==topW-1?'\n':',');}printf("Black: ");for(int i=0;i<topB;i++){if(B[i].type!='P')printf("%c%c%d%c",B[i].type,B[i].y,B[i].x,i==topB-1?'\n':',');else printf("%c%d%c",B[i].y,B[i].x,i==topB-1?'\n':',');}
}

通过上面的几道题。可以发现

第一,模拟的题意都是不难理解的,而且思路比较清晰;

第二,模拟写起来比较繁杂,考验代码能力;

第三。写模拟,好的数据结构+强的代码能力,对于模拟就不会有任何畏惧。

模拟需要多写,才能锻炼出来。恩,以后要多些这些。

这篇关于模拟小练的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1038104

相关文章

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

hdu4431麻将模拟

给13张牌。问增加哪些牌可以胡牌。 胡牌有以下几种情况: 1、一个对子 + 4组 3个相同的牌或者顺子。 2、7个不同的对子。 3、13幺 贪心的思想: 对于某张牌>=3个,先减去3个相同,再组合顺子。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOExcepti

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

每日一题|牛客竞赛|四舍五入|字符串+贪心+模拟

每日一题|四舍五入 四舍五入 心有猛虎,细嗅蔷薇。你好朋友,这里是锅巴的C\C++学习笔记,常言道,不积跬步无以至千里,希望有朝一日我们积累的滴水可以击穿顽石。 四舍五入 题目: 牛牛发明了一种新的四舍五入应用于整数,对个位四舍五入,规则如下 12345->12350 12399->12400 输入描述: 输入一个整数n(0<=n<=109 ) 输出描述: 输出一个整数

【算法专场】模拟(下)

目录 前言 38. 外观数列 算法分析 算法思路 算法代码 1419. 数青蛙 算法分析 算法思路 算法代码  2671. 频率跟踪器 算法分析 算法思路 算法代码 前言 在前面我们已经讲解了什么是模拟算法,这篇主要是讲解在leetcode上遇到的一些模拟题目~ 38. 外观数列 算法分析 这道题其实就是要将连续且相同的字符替换成字符重复的次数+

模拟实现vector中的常见接口

insert void insert(iterator pos, const T& x){if (_finish == _endofstorage){int n = pos - _start;size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;reserve(newcapacity);pos = _start + n;//防止迭代

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT

1 模拟——67. 二进制求和

1 模拟 67. 二进制求和 给你两个二进制字符串 a 和 b ,以二进制字符串的形式返回它们的和。 示例 1:输入:a = "11", b = "1"输出:"100"示例 2:输入:a = "1010", b = "1011"输出:"10101" 算法设计 可以从低位到高位(从后向前)计算,用一个变量carry记录进位,如果有字符没处理完或者有进位,则循环处理。两个字符串对