7-8 Left-pad (20分)

根据新浪微博上的消息,有一位开发者不满NPM(Node Package Manager)的做法,收回了自己的开源代码,其中包括一个叫left-pad的模块,就是这个模块把javascript里面的React/Babel干瘫痪了。这是个什么样的模块?就是在字符串前填充一些东西到一定的长度。例如用去填充字符串GPLT,使之长度为10,调用left-pad的结果就应该是*****GPLT。Node社区曾经对left-pad紧急发布了一个替代,被严重吐槽。下面就请你来实现一下这个模块。

输入格式:
输入在第一行给出一个正整数N(≤10
​4
​​ )和一个字符,分别是填充结果字符串的长度和用于填充的字符,中间以1个空格分开。第二行给出原始的非空字符串,以回车结束。

输出格式:
在一行中输出结果字符串。

输入样例1:
15 _
I love GPLT

输出样例1:
____I love GPLT

输入样例2:
4 *
this is a sample for cut

输出样例2:
cut

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}
public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		char c = Reader.nextChar();
		String s = Reader.nextLine();
		if (s.length()>n) {
			System.out.println(s.substring(s.length()-n));
		}else {
			for (int i = 0; i < n-s.length(); i++) {
				System.out.print(c);
			}
			System.out.println(s);
		}

	}

}



发布了45 篇原创文章 · 获赞 8 · 访问量 1779

猜你喜欢

转载自blog.csdn.net/weixin_43888039/article/details/104009924
今日推荐