45840-不要二
链接:45840-不要二
题目:
二货小易有一个W*H的网格盒子,网格的行编号为0~ H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
题目分析:
代码实现:
package Day6;
import java.util.Scanner;
public class Day6_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt(); //行
int col = scanner.nextInt(); //列
int count = 0; //计数
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(array[i][j] == 0) {
count++;
if(i+2 < row) {
array[i+2][j] = 1;
}
if(j+2 < col) {
array[i][j+2] = 1;
}
}
}
}
System.out.println(count);
}
}
23292-字符串转成整数
链接:23292-字符串转成整数
题目:
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为 0 或者字符串不是一个合法的数值则返回 0
注意:
①字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0
②字符串中可能出现 +/- 且仅可能出现在字符串首位。
题目分析:
代码实现:
package Day6;
public class Day6_2 {
public int StrToInt(String str) {
char[] chs = str.toCharArray();
if(str.isEmpty())
return 0;
int symbol = 1;
if(chs[0] == '-'){ //处理负号
symbol = -1;
chs[0] = '0'; //这里是字符'0',不是0
} else if(chs[0] == '+'){ //处理正号
symbol = 1;
chs[0] = '0';
}
int sum = 0;
for(int i=0;i < chs.length;++i) {
if(chs[i] < '0' || chs[i] > '9') {
sum = 0;
break;
}
sum = sum *10 + chs[i] - '0';
}
return symbol * sum;
}
}