最近由于要准备面试,就在牛客网刷刷题,并且在这记录一下,适当也会做一些扩展,下面的几点需要阅者知悉:
【答案参考】- 代码通过,提交成功
import java.util.Scanner;
public class Main{
public static void main(String[] args){
do {
Scanner in = new Scanner(System.in);
String input=in.nextLine();
if (!input.isEmpty() && input.length() < 5000) {
String[] splitStr = input.split(" ");
System.out.print(splitStr[splitStr.length - 1].length());
break;
} else {
System.out.println("输入字符不能为空且长度不能超过5000,请重新输入");
}
} while (true);
}
}
【答案参考】- 代码通过,提交成功
import java.util.Scanner;
public class Main{
public static void main(String[] args){
do {
Scanner in = new Scanner(System.in);
String inputStr = in.nextLine();
int count = 0;
if (!inputStr.matches("[\\da-zA-Z]+")) {
System.out.print("请重新输入:");
} else {
String input=in.next();
String inLower = input.toLowerCase();
char[] chars = inputStr.toLowerCase().toCharArray();
for (int i = 0; i < chars.length; i++) {
if (inLower.charAt(0)==chars[i]) {
count++;
}
}
System.out.println(count);
break;
}
} while (true);
}
}
【答案参考】- 用例全部通过,但格式错误,提交失败
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String inputStr = sc.nextLine();
int len=inputStr.length();
int num;
if (len<=8){
num=1;
} else {
num = len/8+1;
}
int count=0;
for (int j=0; j0){
for(int j=0;j<8-len%8;j++){
str+="0";
}
}
System.out.println(str);
}
}
}
【答案参考】- 提交通过
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()) {
System.out.println(Integer.parseInt(in.nextLine().substring(2), 16));
}
}
}
-- 第二种方法- [ 因不符合牛客网输入输出格式,所以提交失败],但IDE编译通过,自测通过。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String num = in.nextLine();
toTen(num);
}
public static void toTen(String num){
int value=Integer.parseInt(num.substring(2),16);
System.out.println(value);
}
}
扩展:
将十进制转换程十六进制
int value=211;
System.out.println(Integer.toHexString(value)):
【答案参考】- 用例通过,但格式错误,所以提交失败
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num =in.nextInt();
int k=2;
while(num!=k){
if (num%k==0){
num=num/k;
System.out.print(k+" ");
}else{
k++;
}
}
System.out.print(num);
}
}
【答案参考】- 提交通过
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while (in.hasNextFloat()){
float input = in.nextFloat();
String inputStr = Float.toString(input);
int num = Integer.parseInt(inputStr.substring(0,inputStr.indexOf(".")));
if(input-num >= 0.5){
System.out.println((int)input+1);
}else{
System.out.println((int)input);
}
}
}
}
【答案参考】- 通过IDE和页面自测,但提交失败
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
Map map = new HashMap();
while(num>0) {
String keyVue = in.nextLine();
String[] keys = keyVue.trim().split(" ");
if (map.keySet().contains(keys[0])) {
map.put(keys[0],Integer.parseInt(keys[1])+Integer.parseInt(map.get(keys[0]))+"");
}else{
map.put(keys[0],keys[1]);
}
num--;
}
String[] arr = map.keySet().toArray(new String[0]);
Arrays.sort(arr);
for (String str: arr) {
System.out.println(str + " " + map.get(str));
}
}
}
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in =new Scanner(System.in);
int num = in.nextInt();
int result = 0;
while(num>=10){
if(!(result+"").contains((num%10)+"")){
result=result*10+(num%10);
}
num=num/10;
}
if(!(result + "").contains(num % 10 + "")) {
result = result * 10 + num;
}
System.out.println(result);
}
}
10.【题目描述】-【字符个数统计】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String line = in.nextLine();
char[] array = line.toCharArray();
StringBuffer sbf = new StringBuffer();
int count = 0;
for (char c : array) {
if((int)c<127&&(int)c>0&&(sbf.indexOf(c+""))==-1){
sbf.append(c);
count++;
}
}
System.out.println(count);
}
}