SDMUOJ-P22sdutoj奇怪的星球
(这个题怎么在新生赛的时候就没做出来,好水)狗头保命
开学第一次写博客,其实昨天正式开学已经做了两道题了,只不过很水,这个题还是比较有学习意义的,其实是模拟题,思路较为清晰:
如果碰到'.‘就与'a'交换位置就好了,这就需要不断的交换上下的位置,此处需要一个while来完成,以满足不断交换的需求。
参考代码及注释如下(目前还未找到优化方法)
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 char ch[50000][10]; 6 int r,c; 7 ios::sync_with_stdio(false); 8 cin>>r>>c; 9 for(register int i=0;i<r;i++) 10 { 11 for(register int j=0;j<c;j++) 12 { 13 cin>>ch[i][j]; 14 } 15 } 16 for(register int i=0;i<r;i++) 17 { 18 for(register int j=0;j<c;j++) 19 { 20 if(ch[i][j]=='.')//如果是'.'并且下一层是'a',其实可以合并为一个if,只不过我看着这样舒服 21 { 22 if(ch[i+1][j]=='a') 23 { 24 int temp=i;//需要用一个临时的变量来储存i,否则就会乱套 25 while(temp>=0&&ch[temp][j]=='.')//循环到第0层并且如果这个坐标的字符是'.' 26 { 27 ch[temp][j]='a'; 28 ch[temp+1][j]='.';//交换位置 29 temp--;//到上一层 30 } 31 } 32 } 33 } 34 } 35 for(register int i=0;i<r;i++) 36 { 37 for(register int j=0;j<c;j++) 38 { 39 cout<<ch[i][j]; 40 } 41 cout<<endl; 42 } 43 return 0; 44 }
本文来自博客园,作者:江上舟摇,转载请注明原文链接:https://www.cnblogs.com/LQS-blog/p/15916576.html