pintia算法

it2024-04-04  61

这里写自定义目录标题

L1-003 个位数统计 (15分)next和nextline压制警告@SuppressWarningsjava写算法中常用的函数1、split2、切割字符串 L1-034 点赞 javahasNext()的用法nextline()的作用?吃掉\n符号

L1-003 个位数统计 (15分)

next和nextline

next()不会吸取字符前/后的空格/Tab键,只吸取字符,开始吸取字符(字符前后不算)直到遇到空格/Tab键/回车截止吸取; nextLine()吸取字符前后的空格/Tab键,回车键截止。

import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); //long n = scan.nextLong(); String str = scan.nextLine(); char[] ch = new char[1001]; int[] arr = new int[1001]; ch = str.toCharArray();//转换成字符数组; for(int i=0;i<str.length();i++) { if(ch[i]=='0') arr[0]++; else if(ch[i]=='1') arr[1]++; else if(ch[i]=='2') arr[2]++; else if(ch[i]=='3') arr[3]++; else if(ch[i]=='4') arr[4]++; else if(ch[i]=='5') arr[5]++; else if(ch[i]=='6') arr[6]++; else if(ch[i]=='7') arr[7]++; else if(ch[i]=='8') arr[8]++; else if(ch[i]=='9') arr[9]++; } for(int i=0;i<=1000;i++) { if(arr[i]!=0) System.out.println(i+":"+arr[i]); } } }

压制警告@SuppressWarnings

Suppress 抑制;镇压;废止 Warnings警告

@SuppressWarnings(“resource”)是J2SE 提供的一个批注。该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。

@SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法)中的警告。其中的想法是当您看到警告时,您将调查它,如果您确定它不是问题,您就可以添加一个 @SuppressWarnings 批注,以使您不会再看到警告。虽然它听起来似乎会屏蔽潜在的错误,但实际上它将提高代码安全性,因为它将防止您对警告无动于衷 — 您看到的每一个警告都将值得注意。

java写算法中常用的函数

1、split

Scanner s=new Scanner(System.in); String str=s.nextLine(); String[] a=str.split("😊;

2、切割字符串

substring public String substring(int beginIndex, int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 示例:

“hamburger”.substring(4, 8) returns “urge” “smiles”.substring(1, 5) returns “mile”

参数: beginIndex - 起始索引(包括)。 endIndex - 结束索引(不包括)。 返回: 指定的子字符串。 抛出: IndexOutOfBoundsException - 如果 beginIndex 为负,或 endIndex 大于此 String 对象的长度,或 beginIndex 大于 endIndex。

L1-034 点赞 java

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; public class Main{ static int[] tag=new int[1010]; public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int number=Integer.parseInt(in.readLine()); for(int i=0;i<number;i++) { String[] k=in.readLine().split(" "); for(int j=1;j<k.length;j++) { int temp=Integer.parseInt(k[j]); tag[temp]++; } } int id=0; int max=tag[0]; for(int i=1;i<=1000;i++) { if(max<=tag[i]) { max=tag[i]; id=i; } } System.out.printf("%d %d\n",id,max); } }

hasNext()的用法

while(s.hasNext()) { String str=s.nextLine(); } s.hasNext()输入下一行。 nextline读取。

nextline()的作用?吃掉\n符号

int n = scan.nextInt();// int count=0;//计数器; scan.nextLine();//吃掉\n符;

最新回复(0)