每日一题:Day6

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;
}
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值