输入一个字符串和一个整数 k ,截取字符串的前k个字符并输出
数据范围:字符串长度满足 ,
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(); } }
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input = sc.nextLine(); String str = input.split(" ")[0]; int num = Integer.parseInt(input.split(" ")[1]); int end = 0; for (int i = 0; i < str.length(); i++) { char a = str.charAt(i); if (19968 <= a && a <40869) { // 汉字ASCII码范围 end += 2; } else { end++; } if (end == num) { System.out.println(str.substring(0, i+1)); break; } else if (end > num) { System.out.println(str.substring(0, i)); break; } else { continue; } } } } }
#include <bits/stdc++.h> using namespace std; void fun(string str,int N){ for(int i=0;i<N;i++) cout<<str[i]; cout<<endl; } int main(){ string a; int n; while(cin>>a){ cin>>n; int sum=0; if(a[n-1]<0){ for(int i=0;i<n;i++){ if(a[i]<0) sum++; } if(sum%2) fun(a,n-1); else fun(a,n); } else fun(a,n); } return 0; }
#include <stdio.h> #include <string.h> int main(){ char str[1024]; while(scanf("%s", str) != EOF){ int catLen, i, acLen; scanf("%d", &catLen); acLen = catLen; for(i = 0; i < catLen; i++){ if(str[i] < 0){ if(i+1 < catLen){ i+=1; }else { acLen = i; break; } } } str[acLen] = '\0'; printf("%s\n", str); } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.next(); char[] ch=str.toCharArray(); int num=sc.nextInt(); int i=0; while(num>0){ if(ch[i]>128){ num-=2; i++; } else{ num--; i++; } } System.out.println(str.substring(0,i)); } } }
按字节数来新建字符串即可,解码不成功会得到一个特殊字符。比较坑的地方是输入数据是同一行的,跟样例不同(习惯了。
import java.io.BufferedInputStream; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(new BufferedInputStream(System.in)); try{ while (scanner.hasNext()) { byte[] bytes = scanner.next().getBytes(); int n = Integer.parseInt(scanner.next()); if (n > bytes.length) n = bytes.length; if(n<0){ n=0; } String string = new String(bytes, 0, n); if (string.length() > 0 && string.charAt(string.length() - 1) == '�') string = string.substring(0, string.length() - 1); System.out.println(string); } } catch (Exception e){ e.printStackTrace(); } } }