题目相关
题目链接
AtCoder Beginner Contest 168 B题,https://atcoder.jp/contests/abc168/tasks/abc168_b。
Problem Statement
We have a string S consisting of lowercase English letters.
If the length of S is at most K, print SS without change.
If the length of S exceeds K, extract the first K characters in S, append ...
to the end of them, and print the result.
Input
Input is given from Standard Input in the following format:
K
S
Output
Print a string as stated in Problem Statement.
Samples1
Sample Input 1
7
nikoandsolstice
Sample Output 1
nikoand...
Explaination
nikoandsolstice
has a length of 15, which exceeds K=7.
We should extract the first 7 characters in this string, append ...
to the end of them, and print the result nikoand...
.
Samples2
Sample Input 2
40
ferelibenterhominesidquodvoluntcredunt
Sample Output 2
ferelibenterhominesidquodvoluntcredunt
Constraints
- K is an integer between 1 and 100 (inclusive).
- S is a string consisting of lowercase English letters.
- The length of S is between 1 and 100 (inclusive).
题解报告
本题含义
给一个数字 K,一个字符串 S。如果字符串 S 的长度小于 K,输出本身。如果字符串 S 的长度大于 K,输出前 K 个字符,后面加上 3 个点。
样例数据分析
本题是一个水题,我写这个报告,也只是水一下。所以不需要样例数据分析了。
数据范围分析
K 的最大值是 100,字符串 S 的最大长度为 100。
算法设计
一道非常简单的入门级模拟题。
本题可以使用 STL 的 string,也可以使用字符串数组。如果使用字符串数组,要注意长度的定义。100 是不够的。为什么?假设 S 输入长度为 100 个字符,K 的大小为 99,那么最终实际的字符长度为 99+3=102 个。
AC 参考代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
char s[106];
cin>>k>>s;
if (strlen(s)>k) {
s[k]='.';
s[k+1]='.';
s[k+2]='.';
s[k+3]='\0';
}
cout << s << endl;
return 0;
}