不要二_把字符串转换成整数

不要二

不要二

image-20220430142727639

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

把字符串转换成整数

把字符串转换成整数

image-20220430145152834

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;
    }
}
全部评论
学到了,感谢
点赞 回复 分享
发布于 2022-08-22 10:11 江苏

相关推荐

2025-11-26 09:37
山西大学 测试工程师
我要娶个什么名:学长你电脑闹鬼了
点赞 评论 收藏
分享
活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务