栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

每日一题:Day6

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

每日一题: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
            for(int j=0;j
                array[i][j]=0;
            }
        }
        for(int i=0;i         {
             for(int j=0;j             {
            if(array[i][j]==0)
            {
                 count++;
                 if(i+2                  {
                     array[i+2][j]=1;
                 }
                 if(j+2                  {
                     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         {
            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        {
           sum=sum*10+str.charAt(i)-'0';
       } 
        return sum*num;
}
    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/933506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号