WY16 不要二
1、二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
因为欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根 且不能位2
即可以知道:(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)!=4
既可以知道不能发蛋糕的位置分为两种情况:x1=x2&&y1-y2=2 和y1=y2&&x1-x2=2
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int rol=sc.nextInt();
int wol=sc.nextInt();
int count=0;
int[][] array=new int[rol][wol];
for(int i=0;i<rol;i++){
for(int j=0;j<wol;j++){
array[i][j]=0;
}
}
for(int i=0;i<rol;i++)
{
for(int j=0;j<wol;j++)
{
if(array[i][j]==0)
{
count++;
if(i+2<rol)
{
array[i+2][j]=1;
}
if(j+2<wol)
{
array[i][j+2]=1;
}
}
}
}
System.out.print(count);
}
}
2、把字符串转换成整数
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为 0 或者字符串不是一个合法的数值则返回 0
①字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0
②字符串中可能出现 +/- 且仅可能出现在字符串首位。
public class Solution {
public int StrToInt(String str) {
int sum=0;
int num=1;
if(str.isEmpty())
{
return 0;
}
if(str.charAt(0)=='-')
{
num=-1;
}
if(str.length()==1&&str.charAt(0)-'0'<0||str.charAt(0)-'0'>9)
{
return 0;
}
for(int i=1;i<str.length();i++)
{
if(str.charAt(i)-'0'<0||str.charAt(i)-'0'>9)
{
return 0;
}
}
if(str.length()>=2&&str.charAt(0)-'0'<0||str.charAt(0)-'0'>9)
{
str=str.substring(1);
}
for(int i=0;i<str.length();i++)
{
sum=sum*10+str.charAt(i)-'0';
}
return sum*num;
}
}