天梯赛补题
1.L2-4 哲哲打游戏
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<vector> 7 using namespace std; 8 const int maxx=1e5+10; 9 const int maxp=1e6+10; 10 11 int main(){ 12 int n,m; 13 scanf("%d %d",&n,&m); 14 int c[n+1]={0}; 15 vector<int> a[n+1]; 16 for(int i=1;i<=n;i++){ 17 int k; 18 scanf("%d",&k); 19 a[i].push_back(k); 20 for(int j=1;j<=k;j++){ 21 int s; 22 scanf("%d",&s); 23 a[i].push_back(s); 24 } 25 } 26 int now=1; 27 c[1]=1; 28 int mp=0; 29 for(int i=0;i<m;i++){ 30 int sign,j; 31 /*0.在当前剧情点做出了第j个选择 32 1.进行存档,存档在了j位置上 33 2.进行了一次读取存档的操作,读取了第j个位置的存档 34 */ 35 scanf("%d %d",&sign,&j); 36 if(sign==0){ 37 now=a[now][j]; 38 }else if(sign==1){ 39 printf("%d\n",now); 40 c[j]=now; 41 42 }else if(sign==2){ 43 now=c[j]; 44 } 45 } 46 printf("%d\n",now); 47 48 49 50 }
wa掉:
1.二维数组太大时,要用vector数组进行存储.并且vector的数组存的时候:
eg.vector<int> s[1000005];
s[i].push_back(num);
num=s[i][j]
2.数组的大小是一个很玄学的事情,就可以直接开成n+1就行
2.L2-1 包装机 (25 分)
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<stack> 8 #include<vector> 9 using namespace std; 10 int main(){ 11 int n,m,smax; 12 scanf("%d %d %d",&n,&m,&smax); 13 queue<char>a[n+1];//模拟队列,n+1是因为后面更加方便 14 stack<char>b;//用栈模拟筐子 15 for(int i=1;i<=n;i++) 16 { 17 for(int j=0;j<m;j++)//读取n条轨道的m个元素 18 { 19 char tem;cin>>tem; 20 a[i].push(tem); 21 } 22 23 } 24 25 while(1) 26 { 27 int x; 28 cin>>x; 29 if(x==-1)//如果输入-1正常退出 30 break; 31 if(x==0&&!b.empty())//为零且存在元素就出栈,如果没有判空会出现段错误!! 32 { 33 cout<<b.top(); 34 b.pop(); 35 } 36 if(x>0&&b.size()<smax&&a[x].size())//入栈 37 { 38 b.push(a[x].front()); 39 a[x].pop(); 40 } 41 else if(x>0&&b.size()>=smax&&a[x].size())//如果栈满还继续加入元素,就先拿出一个,再加入元素 42 //这里的是else if语句,不然会和上一条重复操作。 43 { 44 cout<<b.top(); 45 b.pop(); 46 b.push(a[x].front()); 47 a[x].pop(); 48 } 49 } 50 }
wa掉:
1.就是数组的问题,一般大部分代码可以过的时候,就要想想是不是数组的问题,试着用stack和vector来存储元素。