不要二_把字符串转换成整数
不要二
import java.util.*;
public class Main{
//( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 算数平方根不等于 2!
// (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ! = 4
// 0 + 4 = 4;
// 1 + 3 = 4;
// 2 + 2 = 4;
// 4 + 0 = 4;
//也就是说两个相同的数相乘要等于 {0,1,2,3,4}
//整数相乘, 故只有 4 + 0 或者 0 + 4!
// x1-x2!= 2 ! y1-y2!= 0;
//即 x1!=x2+2; y1!=y2!
//当 (x2,y2)坐标放了蛋糕时, (x2+2,y2) 就不能放蛋糕!
// 同理 (x2,y2+2) 也不能放蛋糕!
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int h = sc.nextInt();
int[][] array = new int[w][h];
int count = 0;
for(int i = 0;i < w; i++){
for(int j = 0;j < h; j++){
if(array[i][j] == 0){
count++;
if(i+2 < w){
array[i+2][j] = 1;
}
if(j+2 < h){
array[i][j+2] = 1;
}
}
}
}
System.out.println(count);
}
} 把字符串转换成整数
public class Solution {
public int StrToInt(String str) {
//字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0
//字符串中可能出现 +/- 且仅可能出现在字符串首位
if(str==null||str.length()==0){
return 0;
}
//
if((str.charAt(0)=='+'||str.charAt(0)=='-')&&str.length()==1){
return 0;
}
boolean flag = false;
int sum = 0;
int i = 0;
for( i = 0; i< str.length();i++){
char ch = str.charAt(i);
if(ch>'9'||ch<'0'){//不是数字!
//字符首是否为 +/-
if(i==0){
if(ch=='+'||ch=='-'){
if(ch=='-'){//负号保留,正号舍去!
flag = true;
}
}else{//字符串开始位置 为其他非数字字符
sum = 0;
break;
}
}else{//非数字字符!
sum = 0;
break;
}
}else{//数字字符
sum = sum*10 + (ch-'0');
}
}
if(flag){
sum = -sum;
}
return sum;
}
}
阿里云成长空间 786人发布