PAT L1-032. Left-pad
来源:互联网 发布:烈焰功勋心法数据库端 编辑:程序博客网 时间:2024/06/06 00:41
PAT L1-032. Left-pad
根据新浪微博上的消息,有一位开发者不满NPM(Node Package Manager)的做法,收回了自己的开源代码,其中包括一个叫left-pad的模块,就是这个模块把javascript里面的React/Babel干瘫痪了。这是个什么样的模块?就是在字符串前填充一些东西到一定的长度。例如用“*”去填充字符串“GPLT”,使之长度为10,调用left-pad的结果就应该是“******GPLT”。Node社区曾经对left-pad紧急发布了一个替代,被严重吐槽。下面就请你来实现一下这个模块。
输入格式:
输入在第一行给出一个正整数N(<=104)和一个字符,分别是填充结果字符串的长度和用于填充的字符,中间以1个空格分开。第二行给出原始的非空字符串,以回车结束。
输出格式:
在一行中输出结果字符串。
输入样例1:
15 _
I love GPLT
输出样例1:
____I love GPLT
输入样例2:
4 *
this is a sample for cut
输出样例2:
cut
题目链接:https://www.patest.cn/contests/gplt/L1-032
题解:
cnt=n-line.length()计算要输出的占位符个数,剩下的字符从line.length()-n+i的位置开始输出。 注意,输入的字符串带空格,所以用getline,getline会先读入第一行,要两次getline.
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<algorithm>#include<string.h>#include<string>#include<stack>#include<queue>#include<sstream>#include<iostream>#include<set>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;int main(){ int n; char c; while(scanf("%d\n%c", &n, &c) == 2){ string line; getline(cin, line); getline(cin, line); int cnt = n-line.length(); if(cnt<0) cnt = 0; for(int i=0; i<n; i++){ if(i<cnt) printf("%c", c); else printf("%c", line[line.length()-n+i]); } printf("\n"); } return 0;}
0 0
- PAT L1-032. Left-pad
- PAT L1-032. Left-pad
- pat:L1-032. Left-pad
- PAT-L1-032. Left-pad(字符处理)
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- L1-032. Left-pad
- LeetCode
- 闭包中的内存泄漏
- Qt学习笔记5-修改exe名称
- 我的算法9
- JS基础算法总结
- PAT L1-032. Left-pad
- h5c3内容05
- 从Mac的GUID分区谈起
- L1-032. Left-pad
- NS2相关学习——创建Xgraph的输出文件
- 3Sum Closest
- Windows 环境下为vim安装YouCompleteMe
- QT中tcp/Ip通讯
- Linux环境下线程的互斥