Contents
  1. 1. hdu 2000
  2. 2. hdu 2001
  3. 3. hdu 2002
  4. 4. hdu 2003
  5. 5. hdu 2004
  6. 6. hdu 2005
  7. 7. hdu 2006
  8. 8. hdu 2007
  9. 9. hdu 2008
  10. 10. java学习
    1. 10.1. 变量和常量命名规范
    2. 10.2. 常量和final
    3. 10.3. 基本数据类型
      1. 10.3.1. 逻辑类型
        1. 10.3.1.1. boolean
      2. 10.3.2. 整数类型
        1. 10.3.2.1. int
        2. 10.3.2.2. byte
        3. 10.3.2.3. short
        4. 10.3.2.4. long
      3. 10.3.3. 字符类型
        1. 10.3.3.1. char
      4. 10.3.4. 浮点类型
        1. 10.3.4.1. float(单精度)
        2. 10.3.4.2. double(双精度)
    4. 10.4. 类型转换
      1. 10.4.1. 自动类型转换
      2. 10.4.2. 强制类型转换
    5. 10.5. 方法
    6. 10.6. 类与对象
    7. 10.7. 类的UML图
    8. 10.8. this
    9. 10.9. static关键字【静态】
    10. 10.10. 访问权限
      1. 10.10.1. private【私有】
      2. 10.10.2. protected【受保护】
      3. 10.10.3. public【共有】
      4. 10.10.4. 【友好】
    11. 10.11. 类封装
    12. 10.12. super
    13. 10.13. 数组
    14. 10.14. 冒泡排序
    15. 10.15. 二分(折半)查找法
    16. 10.16. Integer类
    17. 10.17. 【并不考…】测试自动装箱,拆箱
    18. 10.18. try-catch捕获异常
    19. 10.19. throws声明异常
    20. 10.20. 手动抛出异常
    21. 10.21. 抽象【abstract】
    22. 10.22. 多态
    23. 10.23. final
    24. 10.24. 抽象类
    25. 10.25. 接口【interface】

做的还蛮好的(严格),以前的c语言练习平台,只写一丢丢java,0基础,写的可能又丑空间又大…
有趣的是,我大一练习c语言的时候那些id用三国里面的人的ACMer就无时无刻不在测试他们的代码,现在他们还是一直活跃在Realtime Status,真的很厉害很肝,敬佩orz
哈哈哈哈哈哈以前做C真的好菜,代码能力好重要哦

11.1
先做两道吧…然后还有其他东西要学呢

hdu 2000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
import java.util.Arrays;

public class Main { //注意提交的时候改成Main哦~
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String s = scan.next();
char[] a = s.toCharArray(); //字符串转换为字符数组
Arrays.sort(a);
for(int i = 0; i < 2; i++){
System.out.print(a[i] + " ");
}
System.out.println(a[2]);
}

}
}

hdu 2001

这个是copy别人的正确了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {

double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double d = scanner.nextDouble();


double x = Math.pow(a-c, 2);
double y = Math.pow(b-d, 2);
double z = Math.sqrt(x+y);
System.out.println(String.format("%.2f",z));
}
}
}

这个是我的…fo,示例都过的了…不晓得为啥过不去…居然报Wrong Answer,有点可怕哦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
import java.lang.Math; //sqrt

public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int x1, y1, x2, y2;
double l;
while(sc.hasNext()) {
x1 = sc.nextInt();
y1 = sc.nextInt();
x2 = sc.nextInt();
y2 = sc.nextInt();
l = (double)Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
String str = String.format("%.2f", l);
System.out.println(str);
}

}

}

11.3

hdu 2002

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;

public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
double r;
double PI = 3.1415927;
double v;
while(sc.hasNext()){
r = sc.nextDouble();
v = (4.0/3.0) * PI * Math.pow(r, 3);
System.out.println(String.format("%.3f", v));
}

}

}

hdu 2003

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;

public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
double a = sc.nextDouble();
a = Math.abs(a); //可以换成if语句
System.out.println(String.format("%.2f", a));
}
}

}

hdu 2004

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int score;
char level;
while (sc.hasNext()) {
score = sc.nextInt();
if (score > 100 || score < 0)
System.out.println("Score is error!");
else {
score = score / 10; //一样可以用if实现
switch (score) {
case 10:
level = 'A';
break;
case 9:
level = 'A';
break;
case 8:
level = 'B';
break;
case 7:
level = 'C';
break;
case 6:
level = 'D';
break;
default:
level = 'E';
}
System.out.println(level);
}
}

}
}

11.4

hdu 2005

介个我是真的不会哒…java的类也太多了吧,过于智能我不习惯!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;
import java.util.Calendar;

public class Main {
public static void main(String args[]) throws ParseException {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String bir = sc.next();
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); //校验日期的格式
//月份M大写,分钟m小写
Date date = format.parse(bir);
Calendar cal = Calendar.getInstance(); //Calendar的getInstance()方法返回用默认的地区和时区的当前日期和当前时间所初始化的GregorianCalendar(标准日历)
cal.setTime(date);
long res = cal.get(Calendar.DAY_OF_YEAR);
System.out.println(res);
}

}
}

hdu 2006

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int all = 1;
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
if (num % 2 == 1)
all *= num;
}
System.out.println(all);
}
}
}

一到九点,做题的人就特别多=。=

hdu 2007

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int ou = 0;
int ji = 0;
int start = sc.nextInt();
int end = sc.nextInt();
int temp = 0;
if(start > end){ //记得如果不满足由小到大,交换位置
temp = start;
start = end;
end = temp;
}
for(int i = start; i <= end; i++){
if(i % 2 == 0)
ou += Math.pow(i, 2);
else
ji += Math.pow(i, 3);
}
System.out.println(ou + " " + ji);
}
}
}

hdu 2008

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int count = sc.nextInt();
if(count == 0)
continue;
int a = 0, b = 0, c = 0;
for(int i = 0 ; i< count; i++){
float num = sc.nextFloat();
if(num > 0)
a++; //正数
else if(num == 0)
b++; //0
else
c++; //负数
}
System.out.println(c + " " + b + " " + a);
}
}
}

好久没有做hdu喽,最近学一些基础知识,考试要考基础选择什么的1551【只选取我所需的部分】

java学习

以下测试用到的包:

1
2
3
4
5
6
7
package Test;

import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;

变量和常量命名规范

1.所有变量、方法、类名:见名知意
2.类成员变量:首字母小写和驼峰原则,eg:monthSalary
3.局部变量:首字母小写和驼峰原则
4.常量:大写字母和下划线,eg:MAX_VALUE
5.类名:首字母大写和驼峰原则:Man,GoodMan
6.方法名:首字母小写和驼峰规则,eg:run(), runRun()

常量和final

【常量:固定的值以及被final修饰的变量,一旦被初始化之后不可更改的量,使用final修饰的称为符号常量】

1
2
3
4
5
6
7
8
9
10
11
12
public class test {
public static void main(String args[]){
int num = 123;
num = 456;
String name = "kk";
name = "p1Kk";
final String OTHER = "wang";
//OTHER = "yi"; //该句会报错,因为修饰后不可修改
System.out.println(num);
System.out.println(name);
}
}

基本数据类型

逻辑类型

boolean

常量:true false

整数类型

int

4字节内存,取值范围:-2^31 ~ 2^31-1

byte

1字节内存,占8位,取值范围:-2^7 ~ 2^7-1

short

2字节内存,占16位,取值范围:-2^15 ~ 2^15-1

long

8字节内存,占64位,取值范围:-2^63 ~ 2^63-1

字符类型

char

2字节内存,占16位,取值范围:0 ~ 2^15-1(65535)

1
2
char str = 'a';
char str2 = 97; //Unicode表中的排序位置

浮点类型

float(单精度)

4字节内存,占32位,取值范围:1.4E-45 ~ 3.4028235E38 && -3.4028235E38 ~ -1.4E-45
注意常量后面必须要有后缀f或F

double(双精度)

8字节内存,占64位,取值范围:4.9E-324 ~ 1.7976931348623157E308 && -1.7976931348623157E308 ~ -4.9E-324

类型转换

自动类型转换

1.容量小的数据类型可以自动转换为容量大的数据类型。容量指的是类型的范围,而不是所占的字节数。
2.整型常量直接赋值给byte、short、char等类型变量,而不需要进行强制类型转换,只要不超出其表数范围即可。

强制类型转换

1
2
3
4
5
double x = 3.55;
int nx = (int)x; //nx = 3。小数点后全部舍去

char c = 'a';
int d = c + 1; //d = 98, (char)d = 'b'

方法

方法就比较像c语言中的函数
java需要new一个对象来调用普通方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class test {
public static void main(String args[]){
test t = new test(); //通过对象调用普通方法
t.printSxt();
t.add(1, 2);
}

void printSxt(){
System.out.println("Hello, haker");
}

int add(int a, int b){
int sum = a + b;
System.out.println(sum);
return sum; //return什么都不跟,就只有结束方法运行的作用,如果有的话就是返回返回值。
}
}

方法的重载实际是完全不同的方法,只是名称相同,条件是:
1.不同的含义:形参类型,形参个数,形参的顺序不同
2.只有返回值不同不构成方法的重载
3.只有形参的名称不同,不构成方法的重载

类与对象

类:class
对象:Object, instance(实例)

类的UML图

由一个方框图构成,分为三层
第1层为类名
第2层为属性(成员变量)
第3层为方法

其中,抽象类的类名和抽象方法用斜体表示

关联关系用实线箭头表示
箭头由关联方指向被关联方

依赖关系用虚线箭头表示
箭头由依赖方指向被依赖方

依赖关系用实线箭头表示
箭头由继承方(子类)指向被继承方(父类)

依赖关系用虚线箭头表示
箭头由实现方(子类)指向被实现方(接口)

this

1.在程序中产生二义性,用this指明当前对象。构造方法中,this总是指向正要初始化的对象。普通方法中,this总指向调用该方法的对象。
2.this关键字调用重载的构造方法,避免相同的初始化代码。但是只能在构造方法中使用,并且必须位于构造方法的第一句。
3.this不能用于static方法中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class test {
int a, b, c;

test(int a, int b){
this.a = a;
this.b = b;
}
test(int a, int b, int c){
this(a, b); //重载的方法,使用this必须放在第一句
this.c = c;
}

void sing(){

}
void eat(){
this.sing(); //调用本类中的sing()
System.out.println("你妈叫你回家吃饭!");
}
public static void main(String args[]){
test hi = new test(2, 3);
hi.eat();
}
}

我还没有很理解…这种还是需要多练的吧…

static关键字【静态】

static修饰的成员变量和方法,从属于类。普通变量和方法从属于对象。
静态方法不可调用非静态,但是非静态可以调用静态。
静态初始化块执行先上溯到Object类,先执行Object的静态初始化块,再向下执行子类的静态初始化块,直到我们的类的静态初始化块为止。

访问权限

是指对象能否通过“.”运算符操作自己的变量或通过“.”运算符调用类中的方法。
访问限制修饰符private, protected, public

private【私有】

某类中创建了有private修饰的成员变量或方法,则其他类不能调用

protected【受保护】

某类中创建了有protected修饰的成员变量或方法,则只有在同一个包中的其他类可以调用

public【共有】

某类中创建了有public修饰的成员变量或方法,则其他类可以调用
可以修饰类,其他修饰符不能。

【友好】

不用以上修饰符修饰的成员变量和方法。

类封装

基本数据类型 包装
double Double
float Float
byte Byte
short Short
int Integer
long Long
char Character

super

直接父类对象的引用,可以通过super来访问父类中被子类覆盖的方法或属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class FatherClass{
public int value;
public void f(){
value = 100;
System.out.println("FatherClass.value = "+value);
}
}

class ChildClass extends FatherClass{
public int value;
public void f(){
super.f(); //调用父类对象的普通方法
value = 200;
System.out.println("ChildClass.value = "+value);
System.out.println(value);
System.out.println(super.value); //调用父类对象的成员变量
}
}

public class test {
public static void main(String args[]){
new ChildClass().f();
}

}

运行结果:

1
2
3
4
FatherClass.value = 100
ChildClass.value = 200
200
100

注意:所有的构造方法,第一句都会调用super(),去追溯继承树

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class test {
public static void main(String args[]){
Object[] a1 = {1001, "kk"};
Object[] a2 = {1002, "aa", "swpu"};
Object[][] emps = new Object[2][];
emps[0] = a1;
emps[1] = a2;

for(Object[] temp: emps){
System.out.println(Arrays.toString(temp));
}
}

}

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class test{
public static void main(String args[]){
int values[] = {3, 1, 6, 2, 9, 10, 4, 5, 8, 6};
int temp;
boolean flag = true;
for(int i = 0; i < values.length - 1; i++){
for(int j = 0; j < values.length -1 - i; j++) { //不减1的话访问数组越界
if (values[j] >= values[j + 1]) {
temp = values[j];
values[j] = values[j + 1];
values[j + 1] = temp;
flag = false;
}
}
if(flag){
break;
}
}
System.out.println(Arrays.toString(values));
}
}

二分(折半)查找法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class test{
public static void main(String args[]){
int values[] = {3, 1, 6, 2, 9, 10, 4, 5, 8, 6};
Arrays.sort(values);
int temp;
Scanner sc = new Scanner(System.in);
int num, low, high, index;
while(true) {
num = sc.nextInt();
low = 0;
high = values.length - 1;
boolean flag = false;
while (low <= high) {
index = (low + high) / 2;
if (num == values[index]) {
System.out.println("查找成功!位置为:" + (index + 1));
flag = true;
break;
}
else if (num < values[index])
high = index - 1;
else
low = index + 1;
}
if (!flag)
System.out.println("查找失败!");
}
}
}

Integer类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class test{
public static void main(String args[]){
//基本数据类型转换成包装类对象
Integer a = new Integer(3);
Integer b = Integer.valueOf(10);
//把包装类对象转换成基本数据类型
int c = a.intValue();
double d = b.doubleValue();
//把字符串转化成包装类对象
Integer e = new Integer("11111");
Integer f = Integer.parseInt("22222");
//把包装类对象转换成字符串
String str = f.toString();
String str2 = "" + e;
}
}

【并不考…】测试自动装箱,拆箱

1
2
3
4
5
6
7
8
9
public class test{
public static void main(String args[]){
//装箱
Integer a = 234; //自动调用Integer a = Integer.valueOf(234);
//拆箱
int b = a; //int b = a.intValue();
//他会自动缓存[-128, 127]之间的数字
}
}

try-catch捕获异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class test{
private static RandomAccessFile reader;

public static void main(String args[]){
try{
FileReader reader = new FileReader("D:/a.txt");
char a1 = (char)reader.read();
System.out.print(a1);
}catch(FileNotFoundException e){ //子类
e.printStackTrace();
}catch(IOException e){ //父类。子类异常在前,父类在后,否则只要抛出异常,都直接执行IO
e.printStackTrace();
}finally{
try{
if(reader != null)
reader.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}

throws声明异常

1
2
3
4
5
6
7
8
9
10
11
12
13
public class test{
public static void main(String args[]) throws IOException{ //抛出异常接收
readMyFile();
}

public static void readMyFile() throws IOException{ //抛出异常后,丢给调用他的main函数
FileReader reader = null;
reader = new FileReader("d:/b.txt");

if(reader != null)
reader.close();
}
}

手动抛出异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class test{
public static void main(String args[]){
Person p = new Person();
p.setAge(-10);
}

}
class Person{
private int age;
public int getAge(){
return age;
}
public void setAge(int age){
if(age < 0){
throw new IllegalAgeException("年龄不能为负数");
}
this.age = age;
}

}

class IllegalAgeException extends RuntimeException{
public IllegalAgeException(){

}
public IllegalAgeException(String msg){
super(msg);
}
}

抽象【abstract】

abstract修饰抽象方法,不需要方法体。
抽象方法的类必须是抽象类。抽象类可以有非抽象方法。
抽象类不能实例化,不能new,只能被继承,被子类调用,被子类实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//抽象类:为子类提供统一的规范,模板
public abstract class test{
//特点:1.没有实现 2.子类必须实现 //设计一个规范,控制子类
abstract public void shout();

public void run(){
System.out.println("Run Run Run!!!");
}
}

class Dog extends test{
public void shout(){
System.out.println("Shout!!!");

}
}

多态

同一个方法调用,由于对象不同,可能会有不同的行为。

final

修饰变量,见上文
修饰类,修饰的类不能被继承
修饰方法,方法不可被子类重写,但是可以被重载。

抽象类

具体实现,规范(抽象方法)

接口【interface】

接口不提供任何实现,接口中所有方法都是抽象类方法
接口类和实现类不是父子关系,是实现规则的关系
一个接口实现多继承
子类通过implements来实现接口中的规范

参考:b站【尚学堂】Java300集 - 高琪(零基础初学入门)
《Java2实用教程》——清华大学出版社
我校赖老师的ppt