按单词下标区间翻转文章内容
标题:按单词下标区间翻转文章内容 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
给定一段英文文章片段,由若干单词组成,单词间以空格间隔,单词下标从0开始。
请翻转片段中指定区间的单词顺序并返回翻转后的内容。
import re while True: try: arr = re.split(r"\s", input()) left = max(int(input()), 0) right = min(int(input()), len(arr) - 1) if left == right&nbs***bsp;left > len(arr) - 1&nbs***bsp;right < 0: print(arr) else: ls = list() ls.extend(arr[:left]) for i in range(right, left - 1, -1): ls.append(arr[i]) ls.extend(arr[right + 1:len(arr)]) res = "" for i in ls: res += i + " " print(res) except: break
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
int left = Integer.parseInt(in.nextLine());
int right = Integer.parseInt(in.nextLine());
String[] strArr = input.trim().split("\\s+");
left = Math.max(left, 0);
right = Math.min(right, strArr.length - 1);
if (right == left || left > strArr.length - 1 || right < 0) {
System.out.println("EMPTY");
} else {
ArrayList<String> list = new ArrayList<>();
list.addAll(Arrays.asList(strArr).subList(0, left));
for (int i = right; i >= left; i--) {
list.add(strArr[i]);
}
list.addAll(Arrays.asList(strArr).subList(right + 1, strArr.length));
StringBuilder sb = new StringBuilder();
for (String word : list) {
sb.append(word).append(" ");
}
String result = sb.toString().trim();
System.out.println(result);
}
}
}
// manfen
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
int start = Integer.parseInt(scanner.nextLine());
int end = Integer.parseInt(scanner.nextLine());
String[] strings = string.split(" ");
//如果存在以下情况,输出原有字符串
if (end < 0 || start > strings.length || end<start){
System.out.println(string);
return;
}
if (start < 0){
start = 0;
}
if (end >= strings.length){
end = strings.length-1;
}
for (int i = start, j = end;i<j; i++,j--) {
String tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < strings.length; i++) {
stringBuffer.append(strings[i] + " ");
}
stringBuffer.deleteCharAt(stringBuffer.length()-1);
System.out.println(stringBuffer.toString());
}
}// manfen
def solve(str, l, r)
arr = str.split(' ').collect(&:chomp)
length = arr.length
if length == 1 || l >= length || l >= r || r < 0
puts 'EMPTY'
else
l = [l, 0].max
r = [r, length - 1].min
puts (arr[0, l] + arr[l, r - l + 1].reverse + arr[r + 1, length - r - 1]).join(' ')
end
end
while str = STDIN.gets
l = STDIN.gets.to_i
r = STDIN.gets.to_i
solve(str, l, r)
end

