赞
踩
读入大量数据:Scanner sc = new Scanner (new BufferedInputStream(System.in))
数组翻转:int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
进制转换:Integer.valueOf(“str”,x); ///可以为任意进制的字符串str转换成x进制的10进制数
String str1 = Integer.toHexString(x) ;
String str2 = Integer.toOctalString(x) ; ///10进制转换成8进制的字符串
读入String,存为char数组
Scanner sc = new Scanner(System.in);
String string=sc.next();
string = String.valueOf(int a)
char[]a=string.toCharArray();
Character.isUpperCase(a[1]);
a[1]>=‘0’;
字符与数字转换
a.charAt(i)-‘a’->int
(char)(‘a’+index)
去重
HashSet set=new HashSet();
for(int j=i;j<a.length;j++) {
set.add(a[j]);
num+=set.size();
}
浮点数的比较可以使用:BigDecimal.compareTo(f2) == 0
BigDecimal bigDecimal = new BigDecimal(12.52);
BigInteger []aBigIntegers=new BigInteger[2030];
aBigIntegers[0]=BigInteger.ZERO;
aBigIntegers[1]=BigInteger.ONE;
aBigIntegers[2]=BigInteger.ONE;
for(int i=3;i<2030;i++) {
aBigIntegers[i]=aBigIntegers[i-1].add(aBigIntegers[2]);
}
计算星期几:
Calendar anCalendar=Calendar.getInstance();
anCalendar.set(2022, 3, 7);#月份从0开始
System.out.println(anCalendar.get(Calendar.DAY_OF_WEEK) - 1);
日期格式化:
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date dat1=format.parse("1921-7-23");
int b=(int)dat1.getTime();
有年/月/日的,有采用月/日/年的,还有采用日/月/年的000
Scanner cin = new Scanner(System.in);
String a[] = cin.next().split("/");
String t[] = new String[3];
t[0] = ((Integer.valueOf(a[0]) >= 60) ? "19" + a[0] : "20" + a[0]) + "-" + a[1] + "-" + a[2];
t[1] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[0] + "-" + a[1];
t[2] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[1] + "-" + a[0];
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
sf.setLenient(false);
Set<String> s = new TreeSet<String>();
for (String T : t) {
try {
sf.parse(T);
} catch (ParseException e) {
continue;
}
s.add(T);
}
for(String T:s)
System.out.println(T);
}
import java.io.BufferedInputStream;
import java.util.Scanner;
public class _04试剂问题 {
static int sum=0 ,cnt=Integer.MAX_VALUE;
static int []arr=new int[15];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
for(int i=0;i<15;i++) {
arr[i]=scanner.nextInt();
sum+=arr[i];
}
dfs(0,0);
System.out.println(cnt);
}
private static void dfs(int d,int v) {
// TODO Auto-generated method stub
if(d==15)
cnt=Math.min(cnt, Math.abs(sum-v-v));
else {
dfs(d+1, v+arr[d]);
dfs(d+1,v);
}
}
}
‘L’+0
Arrays.fill(v,n);
Set去重StringBuffer.append( )/.toString();
Set< String> set=new HashSet< String>()
Floyd算法:
for(int i,j,k;<2025;++)
if (gragh[i][k] + gragh[k][j] < gragh[i][j])
gragh[i][j] = gragh[i][k] + gragh[k][j];
求最小公倍数:i/gcd(i,j)*j
求最小公因数:if(j==0)
return i;
return gcd(j, i%j);
格式化输出: System.out.format(“%02d”, a);
System.out.format(“%o\n”,i);//“o"表示格式化输出八进制整数
System.out.format(”%x\n",i);//"x"表示格式化输出十六进制整数
BigInteger
浮点数的比较可以使用:BigDecimal.compareTo(f2) == 0
BigDecimal bigDecimal = new BigDecimal(12.52);
System.out.println(bigDecimal);
BigInteger []aBigIntegers=new BigInteger[2030];
aBigIntegers[0]=BigInteger.ZERO;
aBigIntegers[1]=BigInteger.ONE;
aBigIntegers[2]=BigInteger.ONE;
for(int i=3;i<2030;i++) {
aBigIntegers[i]=aBigIntegers[i-1].add(aBigIntegers[2]);
}
BigInteger mBigInteger=aBigIntegers[2020];
BigInteger nBigInteger=aBigIntegers[520];
System.out.println(f(mBigInteger,nBigInteger));
括号匹配问题
Map.get(‘(’)
Map.containsValue/containsKey
Map.put(‘(’),‘(’)
Stack.empty()/pop()/peek()
public class isMatch {
public static void main(String[] args) {
String string ="([a+b]-(rr{}))";
boolean res = match(string);
System.out.println(res);
}
public static boolean match(String str) {
Map<Character,Character> map = new HashMap<>();
map.put(')', '(');
map.put(']', '[');
map.put('}', '{');
Stack<Character> stack =new Stack<>();
for(int i=0;i<str.length();i++) {
Character c =str.charAt(i);
if(map.containsValue(c)) {//左括号入栈
stack.push(c);
}
else if(map.containsKey(c)) {//右括号出栈匹配
if(stack.empty()) {
return false;
}
if(stack.peek()==map.get(c)) {
stack.pop();
}else {
return false;
}
}
}
return stack.empty()?true:false;
}
}
import java.util.Scanner;
public class Main {
static int MOD = 1000000007;
static int ans = 0;
static int n, m, k;
// 方向数组
static int[] xx = new int[] {1, 1, -1, -1, 2, 2, -2, -2};
static int[] yy = new int[] {2, -2, 2, -2, 1, -1, 1, -1};
static int[][] cnt;
// 是否有马
// static boolean[][] vis; 不能用vis数组来标记不能放马的位置,因为棋盘上某一点可能有多个马共同进行限制,需要对限制数计数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
m = scan.nextInt();
k = scan.nextInt();
cnt = new int[n][m];
// 从左上角第一个格子开始放,初始已放马的个数 = 0
dfs(0, 0, 0);
System.out.println(ans);
}
static void dfs (int x, int y, int horse) {
if (horse == k) {
ans = (ans + 1) % MOD;
return;
}
// 切换到下一行第一个元素
if (y >= m) {
y = 0;
x++;
if (x >= n) return;
}
// 当前(x,y)位置不放马
dfs(x, y + 1, horse);
// 当前(x,y)位置放马
// 先判断能否放马
if (cnt[x][y] == 0) {
cnt[x][y]++;
// 遍历当前位置的马能够跳到的棋盘位置,标记为true
for (int i = 0; i < 8; i++) {
int tmpx = x + xx[i];
int tmpy = y + yy[i];
if (tmpx < 0 || tmpy < 0 || tmpx >= n || tmpy >= m) {
continue;
}
cnt[tmpx][tmpy]++;
}
// 放了马之后继续遍历
dfs(x, y + 1, horse + 1);
// 别忘了回溯
// 回溯:一切在之前change过的变量,全都要恢复
cnt[x][y]--;
for (int i = 0; i < 8; i++) {
int tmpx = x + xx[i];
int tmpy = y + yy[i];
if (tmpx < 0 || tmpy < 0 || tmpx >= n || tmpy >= m) {
continue;
}
cnt[tmpx][tmpy]--;
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。