is a problem of flipping the brand. It is a little cumbersome to write, because it is only a laboratory water problem, so it is not optimized. L is flipped from the far left and overlaid on the second pile, and so on, the simulation is just fine.
- #include”stdio.h”
- #include “stack”
- #include”queue “
- using namespacestd;
- intmain(intargc,charconst*argv[])
- {
- intn;
- int< span style="margin:0px; padding:0px; border:none; background-color:inherit">cas=1;
- while(~scanf(“%d”,&n)&&n)
- {}< /span>
- getchar();
- intlnum=1;
- intrnum=n; /span>
- intPoker [105];
- stack<int> s1,s2,s3;
- for(inti = 1; i < = N; ++i)
- {
- scanf(“%c”,&sta);
- if span>(sta==‘D’) /span>
- Poker[i]=0;
- else
- Poker[i]=1;
- < /li>
- getchar();
- s1.push(1); < /li>
- s2.push(n);
- for< span style="margin:0px; padding:0px; border:none; background-color:inherit"> (inti=1;i
- {
- scanf(< span class="string" style="margin:0px; padding:0px ; border:none; color:red; background-color:inherit">“%c”,&ope );
- if(ope= =‘L’)
- {
- for( int< /span>j=1;j<=lnum;++j) li>
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- lnum++;
- if(lnum==rnum)
- while(!s2.empty())< /li>
- inttemp2=s2.top();
- s2.pop(); s2.pop(); padding:0px; border:none; color:black; background-color:inherit”>
- s1.push(temp2);
- }
- {
- int temp1=s1.top();
- s1.pop();
- s3.push(temp1);
- }
- break;
- }
- queue <int> temp;
- temp.push(lnum);
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- temp.push(temp1);
- }
- while(!temp.empty())
- {
- int temp1=temp.front();
- temp.pop();
- s1.push(temp1);
- }
- }
- if(ope==‘R’)
- {
- for (int j = n; j >= rnum; –j)
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- }
- rnum–;
- if(lnum==rnum)
- {
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- s2.push(temp1);
- }
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- s3.push(temp2);
- }
- break;
- }
- queue <int> temp;
- temp.push(rnum);
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- temp.push(temp2);
- }
- while(!temp.empty())
- {
- int temp2=temp.front();
- temp.pop();
- s2.push(temp2);
- }
- }
- }
- printf(“Pile %d
“,cas++); - int num;
- int a[1000];
- int b[1000];
- int bnum=1;
-
while (!s3.empty()) - {
- int temp=s3.top();
- b[bnum++]=temp;
- < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"> s3.pop();
- }
- scanf(“%d”,&num);
- for (int i = 0; i < num; ++i)
- {
- scanf(“%d”,&a[ i]);
- }
- for (int i = 0; i < num; ++i)
- {
- printf(“Card %d is a face “,a[i]);
- if(Poker[b[a[i]]])
- printf(“up “);
- else
- printf(“down “);
- printf(“%d.
“,b[a[i]]); - }
- }
- return 0;
- }
< li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-top:none; border-right:none; border-bottom:none; border-left:3px solid rgb(153,153,153); list- style:decimal-leading-zero outside; color:rgb(85,85,85); line-height:18px; background-color:rgb(245,250,226)"> charsta,ope;
< li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-top:none; border-right:none; border-bottom:none; border-left:3px solid rgb(153,153,153); list- style:decimal-leading-zero outside; color:rgb(85,85,85); line-height:18px; background-color:rgb(245,250,226)"> while(!s1.empty())
就是一个翻牌子的问题,写的略繁琐,因为只是实验室水题,所以不优化了,L是从最左翻面并且覆盖到第二堆上,以此类推,模拟就好。
- #include “stdio.h”
- #include “stack”
- #include “queue”
- using namespace std;
- int main(int argc, char const *argv[])
- {
- int n;
- int cas=1;
- while(~scanf(“%d”,&n) && n)
- {
- getchar();
- int lnum=1;
- int rnum=n;
- int Poker[105];
- stack <int> s1,s2,s3;
- char sta,ope;
- for (int i = 1; i <= n; ++i)
- {
- scanf(“%c”,&sta);
- if(sta==‘D’)
- Poker[i]=0;
- else
- Poker[i]=1;
- }
- getchar();
- s1.push(1);
- s2.push(n);
- for (int i = 1; i < n; ++i)
- {
- scanf(“%c”,&ope);
- if(ope==‘L’)
- {
- for (int j = 1; j <= lnum; ++j)
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- }
- lnum++;
- if(lnum==rnum)
- {
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- s1.push(temp2);
- }
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- s3.push(temp1);
- }
- break;
- }
- queue <int> temp;
- temp.push(lnum);
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- temp.push(temp1);
- }
- while(!temp.empty())
- {
- int temp1=temp.front();
- temp.pop();
- s1.push(temp1);
- }
- }
- if(ope==‘R’)
- {
- for (int j = n; j >= rnum; –j)
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- }
- rnum–;
- if
(lnum==rnum) - {
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- s2.push(temp1);
- }
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- s3.push(temp2);
- }
- brea k;
- }
- queue <int> temp;
- temp.push(rnum);
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
-
temp.push(temp2); - }
- while(!temp.empty())
- {
- int temp2=temp.front();
- temp.pop();
- s2.push(temp2);
- }
- }
- }
- printf(“Pile %d
“,cas++); - int num;
- int a[1000];
- int b[1000];
- int bnum=1;
- while(!s3.empty())
- {
- int temp=s3.top();
- b[bnum++]=temp;
- s3.pop();
- }
- scanf(“%d”,&num);
- for (int i = 0; i < num; ++i)
- {
- scanf(“%d”,&a[i]);
- }
- for (int i = 0; i < num; ++i)
- {
- printf(“Card %d is a face “,a[i]);
- if(Poker[b[a[i]]])
- printf(“up “);
- else
- printf(“down “);
- printf(“%d.
“,b[a[i]]); - }
- }
- return 0;
- }
- #include “stdio.h”
- #include “stack”
- #include “queue”
- using namespace std;
- int main(int argc, char const *argv[])
- {
- int n;
- int cas=1;
- while(~scanf(“%d”,&n) && n)
- {
- getchar();
- int lnum=1;
- int rnum=n;
- int Poker[105];
- stack <int> s1,s2,s3;
- char sta,ope;
- for (int i = 1; i <= n; ++i)
- {
- scanf(“%c”,&sta);
- if(sta==‘D’)
- Poker[i]=0;
- else
- Poker[i]=1 ;
- }
- getchar();
- s1.push(1);
- s2.push(n);
- for (int i = 1; i < n; ++i)
- {
- scanf(“%c”,&ope);
- if(ope==‘L’)
- {
- for (int j = 1; j <= lnum; ++j)
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- }
- lnum++;
- if(lnum==rnum)
- {
- while(!s2 .empty())
- {
- int temp2=s2.top();
- s2.pop();
- s1.push(temp2);
- }
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- s3.push(temp1);
- }
- break;
- }
- queue <int> te mp;
- temp.push(lnum);
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- temp.push(temp1);
- }
- while(!temp.empty())
- {
- int temp1=temp.front();
- temp.pop();
- s1.push(temp1);
- }
- }
- if(ope==‘R’)
- {
- for (int j = n; j >= rnum; –j)
- {
- if(Poker[j])
- Poker[j]=0;
- else
- Poker[j]=1;
- }
- rnum–;
- if(lnum==rnum)
- {
- while(!s1.empty())
- {
- int temp1=s1.top();
- s1.pop();
- s2.push(temp1);
- }
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- s3.push(temp2);
- }
- break;
- }
- queue <int> temp;
- temp.push(rnum);
- while(!s2.empty())
- {
- int temp2=s2.top();
- s2.pop();
- temp.push(temp2);
- }
- while(!temp.empty())
- {
- int temp2=temp.front();
- temp.pop();
- s2.push(temp2);
- }
- }
- }
- printf(“Pile %d
“,cas++); - int num;
- int a[1000];
- int b[1000];
- int bnum=1;
- while(!s3.empty())
- {
- int temp=s3.top();
- b[bnum++]=temp;
- s3.pop();
- }
- scanf(“%d”,&num);
- for (int i = 0; i < num; ++i)
- {
- scanf(“%d”,&a[i]);
- }
- for (int i = 0; i < num; ++i)
- {
- printf(“Card %d is a face “,a[i]);
- if(Poker[b[a[i]]])
- printf(” up “);
- else
- printf(“down “);
- printf(“%d.
“,b[a[i]]); - }
- }
- return 0;
- }
print ?
[cpp] view plain copy
print ?