输入一个字符串和一个整数 k ,截取字符串的前k个字符并输出
数据范围:字符串长度满足 ,
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str; int num; while (in.hasNext()) { str=in.next(); num=in.nextInt(); char[] arr=str.toCharArray(); int temp=0; int i=0; while (temp <num) { if (arr[i] > 128) { temp+=2; if (temp<=num) i++; }else { temp++; if (temp<=num) i++; } } System.out.println(str.substring(0,i)); } in.close(); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String s = in.next(); int n = in.nextInt(); StringBuilder sb = new StringBuilder(); for(int i=0,len=0;i<s.length() && len<n;i++){ char c = s.charAt(i); if((int)c>255){ if((len+2)>n) break; len+=2; sb.append(c); } if((int)c>=0 && (int)c<=255){ len++; sb.append(c); } } System.out.println(sb.toString()); } in.close(); } }
import java.util.Scanner; public class abc{ public static void getResult(int N,String str){ char[] temp=str.toCharArray(); int count=0; int i=0; for(int j=0;j<str.length()&&count<N;j++){ if(temp[j]>128){ count+=2; }else{ count++; } } System.out.println(str.substring(0,count)); } public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.next(); int N=sc.nextInt(); getResult(N,str); } sc.close(); } }
#include<iostream> #include<string> using namespace std; //还是吐槽输入,到底是str和count一行输入还是两行分开,神烦 int main() { string str; while (cin>>str) { int len = 0, count = 0; cin >> len; string res; for (unsigned i = 0; i < str.size(); i++) { if (str[i] < 0 && str[i + 1] < 0) { if (count + 2 > len) break; count += 1; res += str[i], res += str[i + 1]; i++; } else res += str[i]; if (++count == len) break; } cout << res << endl; } }
import java.util.Scanner; public class Main{ public static void main(String[] arg){ Scanner sc = new Scanner(System.in); System.out.println("遇见sc.next()开始第一次输入" ); // 这里,“首次”无论输入多少个空白符(空格、换行符(按回车)、制表符(tab键盘)都不会结束输入, // (即跳过首段连续的空白符) // 直到读取到想要的字符串后,读到输入的空白符表示当前输入结束 // (按了空格看起来没结束输入?当你按了回车,即换行符后程序才会将当前行输入的内容提交,然后继续执行) String str = sc.next(); System.out.println("str = " + str); System.out.println("sc.nextInt()开始第二次输入"); // 获取到到上次sc.next()行尾的空白符 // 一样跳过首段空白 int a = sc.nextInt(); System.out.println("a = " + a); // nextLine(),不跳过首段空白 // 表示会受到sc.nextInt()遗留的空白符影响 // 如果想消除,两种办法 // 1.可以写两次sc.nextLine();第一个用来消除遗留的空白符(连续的空白符一齐消掉),第二个接收下一个数据 // 2.以"asdasd 2 asdasd"的方式输入(第二个不妨输入一段连续的空白符试试),即空格作为第一次输入结束结束 String sr = sc.nextLine(); System.out.println("sr = " + sr+"测试sr"); // String sr2 = sc.nextLine(); // System.out.println("sr = " + sr2+"测试sr2"); char[] arr = str.toCharArray(); for(int i=0;i<a;i++){ System.out.print(arr[i]); } } }
import java.util.Scanner; /** * 按字节截取字符串 * * 编写一个截取字符串的函数,输入为一个字符串和字节数, * 输出为按字节截取的字符串。但是要保证汉字不被截半个, * 如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF"6, * 应该输出为"我ABC"而不是"我ABC+汉的半个"。 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ String s = sc.next(); int num = sc.nextInt(); System.out.println(interceptString(s,num)); } } /** * 返回截取后的字符串 * @param s 输入的字符串 * @param num 字节数 * @return */ public static String interceptString(String s,int num){ if(s == null||s.length()<=0){ return null; } if(num <=0){ return s; } char[] chars = s.toCharArray(); int count = 0; StringBuilder sb = new StringBuilder(); for(int i = 0;i<chars.length;i++){ char c = chars[i]; if((c>='A'&& c<='Z')||(c>='a'&&c<='z')){ count++; if(count <= num){ sb.append(c); } }else{ count = count +2; if(count < num -1){ sb.append(c); } } if(count >=num){ break; } } return sb.toString(); } }