题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
	// TODO Auto-generated method stub
      Scanner scan = new Scanner(System.in);
      
      while(scan.hasNext())
      {
    	  int num= scan.nextInt();
    	  String cmd = scan.next();
    	  char[] arr=cmd.toCharArray();
    	  
    	  int pageSize = Math.min(4, num);
    	  int pageIndex=1;
    	  int songIndex=1;
    	  
    	  for(int i=0; i< arr.length; i++)
    	  {
    		  char curCh = arr[i];
    		  if(curCh=='U')
    		  {
    			  if(pageIndex!=1)
    			  {
    				  pageIndex--;
    			  }else
    			  {
    				  if(songIndex==1)
    				  {
    					  pageIndex= pageSize;
    				  }
    			  }
    			  
    			  if(songIndex==1)
    			  {
    				  songIndex=num;
    			  }else
    			  {
    				  songIndex--;
    			  }
    			  
    			
    			  
    		  }
    		  else//cmd D
    		  {
    			  if(pageIndex!=pageSize)
    			  {
    				  pageIndex++;
    			  }else
    			  {
    				  if(songIndex==num)
    				  {
    					  pageIndex=1;
    				  }
    			  }
    			  
    			  if(songIndex==num)
    			  {
    				  songIndex = 1;
    			  }else
    			  {
    				  songIndex++;
    			  }
    			  
    		
    				  
    		  }
    	  }
    	  
    	  
    	  int songBefore = pageIndex-1;
    	  int songAfter = pageSize-pageIndex;
    	  
    	  int StartIndex = songIndex-songBefore;
    	  int EndIndex = songIndex+songAfter;
    	  
    	  for(int i = StartIndex; i<=EndIndex; i++)
    	  {
    		  System.out.print(i+" ");
    	  }
    	  System.out.println();
    	  System.out.println(songIndex);
    	  
      }
}

}

public class Main {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

          Scanner scan = new Scanner(System.in);

          

          while(scan.hasNext())

          {

              int numscan.nextInt();

              String cmd = scan.next();

              char[] arr=cmd.toCharArray();

              

              int pageSize = Math.min(4, num);

              int pageIndex=1;

              int songIndex=1;

              

              for(int i=0; i< arr.length; i++)

              {

                  char curCh = arr[i];

                  if(curCh=='U')

                  {

                      if(pageIndex!=1)

                      {

                          pageIndex--;

                      }else

                      {

                          if(songIndex==1)

                          {

                              pageIndex= pageSize;

                          }

                      }

                      

                      if(songIndex==1)

                      {

                          songIndex=num;

                      }else

                      {

                          songIndex--;

                      }

                      

                    

                      

                  }

                  else//cmd D

                  {

                      if(pageIndex!=pageSize)

                      {

                          pageIndex++;

                      }else

                      {

                          if(songIndex==num)

                          {

                              pageIndex=1;

                          }

                      }

                      

                      if(songIndex==num)

                      {

                          songIndex = 1;

                      }else

                      {

                          songIndex++;

                      }

                      

                

                          

                  }

              }

              

              

              int songBefore = pageIndex-1;

              int songAfter = pageSize-pageIndex;

              

              int StartIndex = songIndex-songBefore;

              int EndIndex = songIndex+songAfter;

              

              for(int i = StartIndex; i<=EndIndex; i++)

              {

                  System.out.print(i+" ");

              }

              System.out.println();

              System.out.println(songIndex);

              

          }

    }

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务