poj 3032(水題,排隊,出列問題)
阿新 • • 發佈:2021-08-17
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int data[110]; int main(){ int k,n,i,j,p,pos,count; scanf("%d",&k); while(k--){ scanf("%d",&n); count = 1; memset(data,0,sizeof data); if(n==1){ printf("1\n"); continue; } for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ data[count++] = -1; } data[count++] = i; } pos = -1; for(i=n;i>=1;i--){ if(data[i]!=-1){ pos = data[i]; j= i; break; } } p = -1; while(true){ do{ j++; if(j==n+1){ j = 1; } }while(data[j]!=-1); p++; if(p==pos+1){ data[j] = p;if(data[j]==n)break; pos++; p = -1; } } for(i=1;i<n;i++){ cout<<data[i]<<" "; } cout<<data[n]<<endl; } return 0; }