题解 | #小苯的数字滚轮#

小苯的数字滚轮

https://ac.nowcoder.com/acm/contest/130222/C

解法:模拟 核心代码:

public static String f(int n,int m,String a,String s) {
  char[] A = a.toCharArray();
  int idx=0;
  for(char S:s.toCharArray()) {
    if(S=='L') {
      if(idx-1>=0)idx--;
    }
    if(S=='R') {
      if(idx+1<=n-1)idx++;
    }
    if(S=='U') {
      int tem = A[idx]-'0';
      tem = (tem+1)%10;
      A[idx] = (char)(tem+'0');
    }
    if(S=='D') {
      int tem = A[idx]-'0';
      tem = (tem+9)%10;
      A[idx] = (char)(tem+'0');
    }
  }
  return new String(A);
}

用ASCII进行字符与数字的转化 完整代码: 使用快读进行输入优化

import java.io.IOException;
import java.io.InputStream;
 
public class Main {
    //快读
    static public class FastScanner{
        private final InputStream in;
        private final byte[] buf = new byte[1<<16];//64kb缓冲区
        private int ptr=0,len=0;
        //构造函数
        public FastScanner(InputStream is){in=is;}
         
        private byte read() throws IOException {
            if(ptr >= len) {
                ptr = 0;
                len = in.read(buf);
                if (len <= 0) return -1;
            }
            return buf[ptr++];
        }
         
        public long nextLong() throws IOException {
            long x=0;
            int sign=1;
            byte c;
            //跳过非数字和空白
            do {
                c=read();
            }while(c<=' ');
            if(c=='-') {
                sign = -1;
                c = read();
            }
            do {
                x = x*10 + c-'0';
                c = read();
            }while(c>='0'&&c<='9');
            return x*sign;
        }
        public int nextInt() throws IOException {
            return (int)nextLong();
        }
        public String next() throws IOException {
            StringBuilder sb = new StringBuilder();
            byte c;
            do {
                c = read();
            }while(c<=' ');
            do {
                sb.append((char)c);
                c = read();
            }while(c>' ');
            return sb.toString();
        }
    }
    public static void main(String[] args) throws IOException {
        FastScanner fs = new FastScanner(System.in);
        int T = fs.nextInt();
        String[] res = new String[T];
        int k=0;
        while(T-- >0) {
            int n = fs.nextInt();
            int m = fs.nextInt();
            String a = fs.next();
            String s = fs.next();
            res[k++]=f(n,m,a,s);
        }
        for(int i=0;i<res.length;i++) {
            System.out.println(res[i]);
        }
    }
    public static String f(int n,int m,String a,String s) {
        char[] A = a.toCharArray();
        int idx=0;
        for(char S:s.toCharArray()) {
            if(S=='L') {
                if(idx-1>=0)idx--;
            }
            if(S=='R') {
                if(idx+1<=n-1)idx++;
            }
            if(S=='U') {
                int tem = A[idx]-'0';
                tem = (tem+1)%10;
                A[idx] = (char)(tem+'0');
            }
            if(S=='D') {
                int tem = A[idx]-'0';
                tem = (tem+9)%10;
                A[idx] = (char)(tem+'0');
            }
        }
        return new String(A);
    }      
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务