7-8 Left-pad

思路

注意读入和输出格式

如果用fgets读入的话会带上回车,输出的时候一定不要输出了双回车

并且此时的length也会比原始长度多了一,要注意长度比较,这里容易出错

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e6+10;
char s[maxn];
char ch;

int main()
{
    int n;
    scanf("%d %c\n",&n,&ch);
    fgets(s,maxn,stdin);
    int len=strlen(s);
    len--;
    if (n<=len) {
        for (int i=len-n;i<len;i++) {
            printf("%c",s[i]);
        }
        putchar('\n');
    }
    else {
        for (int i=0;i<n-len;i++) {
            putchar(ch);
        }
        printf("%s",s);
    }
    return 0;
}

posted @ 2020-02-18 16:26  xyee  阅读(207)  评论(0编辑  收藏  举报