Huawei OD computer test - intercepting strings (C++ & Java & JS & Python)

describe

Input a string and an integer k, intercept the first k characters of the string and output

Data range: The string length satisfies 1≤�≤1000 1≤n≤1000, 1≤�≤� 1≤k≤n 

Enter description:

1. Enter the string to be intercepted

2. Enter a positive integer k, representing the intercepted length

Output description:

The intercepted string

Example 1

enter:

abABCcDEF
6

Output:

abABCc

Example 2

enter:

bdxpkbhih6 
_

Output:

bdxPKB

Java:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
          // 输入字符串和k
            String str = sc.next();
            int k = sc.nextInt();
          // 直接使用String的substring方法输出结果
            System.out.println(str.substring(0,k));
        }
    }
}

python:

input_str,k = input(),int(input())

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/132805404