Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

POJ 3125 Printer Queue---模拟

Posted on 2010-02-06 03:52 Uriel 阅读(478) 评论(0)  编辑 收藏 引用 所属分类: POJ模拟
        看了题只想到模拟。。于是模拟之。。在模拟题中也算是水题了,不过不知何故做这类的模拟(模拟游戏过程,模拟约瑟夫之类的)有若干循环的题基本上不调试肯定是要死循环。。做的时候想的清清楚楚的代码写出来就挂了。。
        虽说是水题。。贴个代码纪念纪念~~
/*Problem: 3125  User: Uriel 
   Memory: 164K  Time: 16MS 
   Language: C++  Result: Accepted
*/
 

#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>

struct work
{
    
int p,g;
}
;

work P[
110];
int cse,n,m,tmp,tmg,res;
bool flag;

int main()
{
    
int i,j,k;
    scanf(
"%d",&cse);
    
while(cse--)
    
{
        scanf(
"%d %d",&n,&m);
        memset(P,
0,sizeof(P));
        
for(i=0;i<n;i++)
        
{
            scanf(
"%d",&P[i].p);
            
if(i==m)P[i].g=1;
            
else
                P[i].g
=0;
        }

        res
=0;
        i
=0;
        
while(1)
        
{
            flag
=false;
            
for(i=1;i<n;i++)
            
{
                
if(P[i].p>P[0].p)
                
{
                    tmp
=P[0].p;
                    tmg
=P[0].g;
                    
for(j=1;j<n;j++)
                    
{
                        P[j
-1].p=P[j].p;
                        P[j
-1].g=P[j].g;
                    }

                    P[n
-1].p=tmp;
                    P[n
-1].g=tmg;
                    flag
=true;
                    
break;
                }

            }

            
if(!flag)
            
{
                res
++;
                
if(P[0].g)break;
                
else
                
{
                    
for(i=1;i<n;i++)
                    
{
                        P[i
-1].p=P[i].p;
                        P[i
-1].g=P[i].g;
                    }

                    n
--;
                }

            }

        }

        printf(
"%d\n",res);
    }

//    system("PAUSE");
    return 0;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理