一.编程题
1.读懂题意:
任意两块蛋糕的欧几里得距离不能等于2
两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根说明:限制条件是( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) )==4
2.把 (x1-x2) * (x1-x2)看作一个整体,(y1-y2) * (y1-y2)看作一个整体
我们可以有以下情况:1 + 3=4;
3 + 1=4;
2+2=4;
0+4=4;
4+0=4;
但是没有哪个两个整数相乘是=1,3,2的
所以符合条件的就只有:0+4 and 4+0这两种情况
所以进一步得出:x1-x2=0 , y1-y2=2;
说明:x1=x2 , y1=y2+2
x1-x2=2 , y1-y2=0;
说明:x1=x2+2 , y1=y2
得出两种情况:当我们遍历到一个值,这两个地方是不可以放蛋糕的
array[ i ] ,array[ j+2 ]
array[ j ] , array[ i+2 ]
注意点:需要判断j+2 and i+2数组越界情况
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int row=scanner.nextInt();
int col=scanner.nextInt();
int[][]array=new int[row][col];//
int count=0;
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);
}
}
把字符串转换成整数_牛客题霸_牛客网 (nowcoder.com)
1.首先需要了解ASCIIm码对照表
假设字符‘0’是:40
那么字符‘1’就是41,字符'2'就是42
我们需要得到数字2:就用字符'2'(当前字符)-'0'——>42-40==2
2.其次我们需要得到总数,公式是sum=sum*10+sum.——>sum=sum*10+charAt(i)-'0'
sum是当前数字,先将他初始化为0
3.注意点:正负数怎样区分,使用标志位
判断是否是合法数值,里面有第一个字符是'+' and '-'的情况,怎样解决
public class Main {
public static int main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
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;
}
}
二.选择题错题和遗忘点
题目1:
题目2:
阅读下列程序,选择哪一个是正确的输出结果
class HelloA{
public HelloA()
{
System.out.println("I’m A class ");
}
static
{
System.out.println("static A");
}
}
public class HelloB extends HelloA{
public HelloB()
{
System.out.println("I’m B class");
}
static{
System.out.println("static B");
}
public static void main (String[] args){
new HelloB();
}
}
答案:
考点:静态代码块
静态代码块永远按照先后次序先执行>构造方法
使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量。
如果一个类中包含多个静态代码块,在编译代码时,编译器会按照定义的先后次序依次合并
题目3:
执行下列代码的输出结果是( )
public class Demo{
public static void main(String args[]){
int num = 10;
System.out.println(test(num));
}
public static int test(int b){
try
{
b += 10;
return b;
}
catch(RuntimeException e)
{
A 10
B 20
C 30
D 40
题目4:![](https://i-blog.csdnimg.cn/blog_migrate/b18824d25699e755147433574b968f6f.png)
题目5:
abstract 和 final不可以同时出现