⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 华为.cpp

📁 中过科学技术大学历年复试机试题
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>

#define N 20

int mode(int n){
  if((n=n%20)==0)n=20;
  return n;
}
//华为笔试编程题1
main(){
  int fsecrect,Nsecrect;//初始密码
  int i,j,k,a[N+1],secrect[N+1];
  FILE *fp,*fpp;

  if((fp=fopen("input01.txt","r"))==NULL){
     printf("input file open error!\n");
     exit(0);  
  }
  if((fpp=fopen("output02.txt","w"))==NULL){
     printf("output file open error!\n");
     exit(1);  
  }
  	for(i=1;i<=N;i++){
     a[i]=1;//在队列中
     fscanf(fp,"%d",&secrect[i]);
  }
 
  while(!feof(fp)){
   
	
	for(i=1;i<=N;i++){
     a[i]=1;//在队列中
    }
  
	
   do{  
       fscanf(fp,"%d",&fsecrect);
	   if(fsecrect>0){
		   fsecrect=mode(fsecrect);
		   fprintf(fpp,"first secrect=%d :  ",fsecrect);
		   printf("first secrect=%d :  ",fsecrect);
		   break;}
	   
	   fprintf(fpp,"first secrect=%d is error!\n",fsecrect);
	   printf("first secrect=%d is error!\n",fsecrect);
	 }while(fsecrect<=0);
 
  
    a[fsecrect]=0;//出队  
    fprintf(fpp,"%d ",fsecrect);
    printf("%d ",fsecrect);
  
    Nsecrect=secrect[fsecrect];   
    k=fsecrect;

    for(j=2;j<N;j++){//出列N-2个人
	 i=0;
     while(i<Nsecrect){
		 k=mode(k);
		 if(a[k]==1){
			 k++;
			 while(a[k]==0){k++;k=mode(k);}
			 i++;
			 
		 }	 
		 
	     k++;
	 }
     k=k-1;   
	 k=mode(k);
	 a[k]=0; 
	 
	 fprintf(fpp,"%d ",k);
     printf("%d ",k);
	 //system("pause");
     Nsecrect=secrect[k];	  
	 
	} 
	
      for(i=1;i<=N;i++)
		  if(a[i]==1){fprintf(fpp,"%d",a[i]);
		              printf("%d",a[i]);
		  }
      fprintf(fpp,"\n");
      printf("\n");
    
      
	 
  } 
      fclose(fp);
	  fclose(fpp);  
	  system("pause");
} 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -