发布网友 发布时间:2022-04-23 09:04
共3个回答
热心网友 时间:2023-10-09 00:45
经测试,完全合格!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Temp {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
String s = null;
byte i = -1;
System.out.println("请输入一个字符串:");
for (;;) {
s = scanner.next();
if (s == null || s.length() == 0) {
System.out.println("你输入的字符串不合法,请重新输入:");
continue;
}
break;
}
System.out.println("请输入你要截取字符串的字节数:");
for(;;) {
i = scanner.nextByte();
if(i <= 0) {
System.out.println("你输入的数字不合法,请输入一个大于 0 的整数:");
continue;
}
break;
}
resolve(s, i);
}
public static void resolve(String s, byte b) {
byte [] temp = s.getBytes();
if(temp.length < b) {
System.out.println("你输入的字符串过短或者要截取的字节数过大,程序即将推出...");
}
byte [] by = Arrays.copyOf(temp, b);
byte [] by2 = Arrays.copyOf(temp, b+1);
String str = new String(by);
String str2 = new String(by2);
if(str.length() == str2.length()) {
byte [] by3 = Arrays.copyOf(temp, b-1);
str = new String(by3);
}
if(!Character.isLetterOrDigit(str.charAt(str.length()-1))) {
str = str.substring(0, str.length());
}
System.out.println(str);
}
}
热心网友 时间:2023-10-09 00:45
import java.util.*;
import java.io.*;
class Main
{
static Scanner in;
static PrintWriter out;
public static void main(String[] args)throws IOException
{
in=new Scanner(System.in);
out=new PrintWriter(System.out);
String str=in.next();
int x=in.nextInt();
byte[] by=str.getBytes();
int count=0;
for(int i=0;i<x;i++)
{
if(by[i]<0)
count++;
}
if(count%2==0)
{
byte[] an=new byte[x];
for(int i=0;i<x;i++)
{
an[i]=by[i];
}
out.println(new String(an));
}
else
{
byte[] an=new byte[x-1];
for(int i=0;i<x-1;i++)
{
an[i]=by[i];
}
out.println(new String(an));
}
in.close();
out.close();
}
}
热心网友 时间:2023-10-09 00:46
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String sen = s.next();
int i = s.nextInt() - 1;
Pattern p = Pattern.compile("[\u4E00-\u9FB0]");
Matcher m = p.matcher(sen);
int j = 0;
while (m.find()) {
int start = m.start();
if (0 != j) {
start += j;
}
if (i == start) {
i -= 1;
}
if (i == start + 1) {
i -= 2;
}
j++;
}
byte[] from = sen.getBytes();
String out = new String(from, 0, i + 1);
System.out.println(out);
}
}
简单测试了一下应该没问题,又不明白的可以加我百度hi,我再给你解释
楼上的有bug啊,输入“我是一个好人 4”就不好使了