📄 05. core java exercise.txt
字号:
8,完成九宫格程序 在井字形的格局中(只能是奇数格局),放入数字(数字由),使每行每列以及斜角线的和都相等 经验规则:从 1 开始按顺序逐个填写; 1 放在第一行的中间位置;下一个数往右上角45度处填写; 如果单边越界则按头尾相接地填;如果有填写冲突,则填到刚才位置的底下一格; 如果有两边越界,则填到刚才位置的底下一格。 个人认为,可以先把最中间的数填到九宫格的最中间位置;再按上面的规则逐个填写,而且 填的时候还可以把头尾对应的数填到对应的格子中。(第 n 个值跟倒数第 n 个值对应,格局上以最中 间格为轴心对应) 这样就可以同时填两个数,效率比之前更高;其正确性有待数学论证(但多次实验之后都没发现有错)。 九宫格的 1 至少还可以填在另外的三个位置,只是接下来的填写顺序需要相应改变; 再根据九宫格的对称性,至少可以有8种不同的填写方式import java.util.Scanner;class NinePalace{ public static void main(String[] args){ // 定义 N 为九宫格的行列数,需要输入 System.out.println("请输入九宫格的行列规模(只能是奇数的)"); Scanner n = new Scanner(System.in); int N; //判断格局是否奇数 (可判断出偶数、负数 及小数) double d; while (true){ d = n.nextDouble(); N = (int)d; if ((d-N)>1.0E-4||N%2==0||N<0) {System.out.println("输入出错,格局只能是正奇数。请重新输入");} else break; } //老师的九宫格填写方法 int[][] result = new int[N][N]; //定义保存九宫格的数组 int row = 0; //行 初始位置 int col = N/2; //列 初始位置,因为列由0开始,故N/2是中间位置 for (int i=1; i<=N*N; i++){ result [row][col] = i; row--; col++; if (row<0&&col>=N){col--;row+=2;} //行列都越界 else if (row<0){ row = N-1;} //行越界 else if (col>=N){col = 0;} //列越界 else if (result[row][col] != 0){col--;row+=2;} //有冲突 } //打印出九宫格 for (int i=0; i<N; i++){ for(int j=0; j<N; j++){System.out.print(result[i][j]+"\t");} System.out.println(); } //我个人的填格方式 int[][] result2 = new int[N][N]; //为免冲突,重新 new 一个数组 result2[N/2][N/2] = (N*N+1)/2; //先把中间值赋予中间位置 row = 0; //定义行及列的初始赋值位置。之前赋值的for对两个值有影响,故需重新定位 col = N/2; for (int i=1; i<=N*N/2; i++){ result2[row][col] = i; //下面这句是把跟 i 对应的值放到格局对应的位置上 result2[N-row-1][N-col-1] = N*N+1-i; row--; col++; if (row<0){ row = N-1;} //行越界 else if (col>=N){col = 0;} //列越界 else if (result2[row][col] != 0){col--;row+=2;} //有冲突 //这方法不可能出现行列两边都越界的情况,详情需要数学论证 } System.out.println(); //再次打印出九宫格,以对比验证 for (int i=0; i<N; i++){ for(int j=0; j<N; j++){System.out.print(result2[i][j]+"\t");} System.out.println(); } }}9,求一个3*3矩阵对角线元素之和 10,打印杨辉三角11. 约梭芬杀人法 把犯人围成一圈,每次从固定位置开始算起,杀掉第7个人,直到剩下最后一个。11_2、用数组实现约瑟夫出圈问题。 n个人排成一圈,从第一个人开始报数,从1开始报,报到m的人出圈,剩下的人继续开始从1报数,直到所有的人都出圈为止。对于给定的n,m,求出所有人的出圈顺序。12. 判断随机整数是否是素数产生100个0-999之间的随机整数,然后判断这100个随机整数哪些是素数,哪些不是?
public class PrimeTest{
public static void main(String args[]){
for(int i=0;i<100;i++){
int num = (int)(Math.random()*1000);
PrimeTest t = new PrimeTest();
if(t.isPrime(num)){
System.out.println(num+" 是素数!");
}else{
System.out.println(num+" 不是素数!");
}
System.out.println();
}
}
public boolean isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num%i==0){
System.out.println(num+"第一个被"+i+"整除!");
return false;
}
}
return true;
}
}
冒泡排序法://按从大到小的排序int tmp = a[0];for (int i=0; i < a.length; i++){ for (int j=0; j < a.length - i -1; j++){ if (a[j] < a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } }} day06 练习某公司的雇员分为以下若干类:Employee:这是所有员工总的父类,属性:员工的姓名和生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放属性:每小时的工资、每月工作的小时数SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定属性:月销售额、提成率BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分 属性:底薪。public class TestEmployee{
public static void main(String[]args){
Employee[] es = new Employee[5];
es[0] = new Employee("赵君",2);
es[1] = new SalariedEmployee("宋婕", 1, 8000);
es[2] = new HourlyEmployee("王超", 5, 10, 300);
es[3] = new SalesEmployee("秋娥", 2, 200000, 0.05);
es[4] = new BaseSalarySalesEmployee("郭镫鸿", 1, 1000000, 0.1, 10000);
int month = 2;//本月为2月
System.out.println("宇宙集团"+month+"月工资表:");
for(int i=0; i<es.length; i++){
System.out.println(es[i].getName()+":"+es[i].getSalary(month));
}
}
}
class Employee{
private String name;
private int birth;
public String getName(){
return name;
}
public Employee(String name, int birth){
this.name = name;
this.birth = birth;
}
public double getSalary(int month){
if(month==birth){
return 100;
}
return 0;
}
}
class SalariedEmployee extends Employee{
private double salary;
public SalariedEmployee(String name, int birth, double salary){
super(name, birth);
this.salary = salary;
}
public double getSalary(int month){
return salary + super.getSalary(month);
}
}
class HourlyEmployee extends Employee{
private double hourSalary;
private int hour;
public HourlyEmployee(String name, int birth, double hourSalary, int hour){
super(name, birth);
this.hourSalary = hourSalary;
this.hour = hour;
}
public double getSalary(int month){
if(hour<=160){
return hourSalary*hour+super.getSalary(month);
}else{
return 160*hourSalary+(hour-160)*hourSalary*1.5+super.getSalary(month);
}
}
}
class SalesEmployee extends Employee{
private double sales;
private double pre;
public SalesEmployee(String name, int birth, double sales, double pre){
super(name, birth);
this.sales = sales;
this.pre = pre;
}
public double getSalary(int month){
return sales*pre+super.getSalary(month);
}
}
class BaseSalarySalesEmployee extends SalesEmployee{
private double baseSalary;
public BaseSalarySalesEmployee(String name, int birth, double sales, double pre, double baseSalary){
super(name, birth, sales, pre);
this.baseSalary = baseSalary;
}
public double getSalary(int month){
return baseSalary+super.getSalary(month);
}
}
/**
* 在原有的雇员练习上修改代码
* 公司会给SalaryEmployee每月另外发放2000元加班费,给
* BasePlusSalesEmployee发放1000元加班费
* 改写原有代码,加入以上的逻辑
* 并写一个方法,打印出本月公司总共发放了多少加班费
* @author Administrator
*
*/
public class EmployeeTest {
/**
* @param args
*/
public static void main(String[] args) {
Employee e[] = new Employee[4];
e[0] = new SalariedEmployee("魏威",10,5000);
e[1] = new HourlyEmployee("段利峰",8,80,242);
e[2] = new SalesEmployee("林龙",11,300000,0.1);
e[3] = new BasedPlusSalesEmployee("华溪",1,100000,0.15,1500);
for(int i=0;i<e.length;i++){
System.out.println(e[i].getName()+": "+e[i].getSalary(11));
}
//统计加班费
int result = 0;
// for(int i=0;i<e.length;i++){
// if(e[i] instanceof SalariedEmployee){
// SalariedEmployee s = (SalariedEmployee)e[i];
// result += s.getAddtionalSalary();
// }
// if(e[i] instanceof BasedPlusSalesEmployee){
// BasedPlusSalesEmployee b = (BasedPlusSalesEmployee)e[i];
// result += b.getAddtionalSalary();
// }
// }
for(int i=0;i<e.length;i++){
result += e[i].getAddtionalSalary();
}
System.out.println("加班费: "+result);
}
}
interface AddtionalSalary{
int getAddtionalSalary();
}
class Employee implements AddtionalSalary{
private String name;//员工姓名
private int birth;//员工生日月份
public Employee(String name,int birth){
this.name = name;
this.birth = birth;
}
public int getSalary(int month){
int result = 0;
if(month==birth)
result = 100;
return result;
}
public String getName(){
return name;
}
public int getAddtionalSalary(){
return 0;
}
}
class SalariedEmployee extends Employee{
private int salaryPerMonth;
public SalariedEmployee(String name,int birth,int salaryPerMonth){
super(name,birth);
this.salaryPerMonth = salaryPerMonth;
}
public int getSalary(int month){
return this.salaryPerMonth + super.getSalary(month)+
this.getAddtionalSalary();
}
public int getAddtionalSalary(){
return 2000;
}
}
class HourlyEmployee extends Employee{
private int salaryPerHour;
private int hoursPerMonth;
public HourlyEmployee(String name,int birth,int salaryPerHour,int hoursPerMonth){
super(name,birth);
this.salaryPerHour = salaryPerHour;
this.hoursPerMonth = hoursPerMonth;
}
public int getSalary(int month){
int result = 0;
if(this.hoursPerMonth<=160){
result = hoursPerMonth*salaryPerHour;
}else{
result = 160*salaryPerHour +
(int)((hoursPerMonth-160)*1.5*salaryPerHour);
}
return result+super.getSalary(month);
}
}
class SalesEmployee extends Employee{
private int sales;
private double rate;
public SalesEmployee(String name,int birth,int sales,double rate){
super(name,birth);
this.sales = sales;
this.rate = rate;
}
public int getSalary(int month){
return (int)(sales*rate)+super.getSalary(month);
}
}
class BasedPlusSalesEmployee extends SalesEmployee{
private int basedSalary;
public BasedPlusSalesEmployee(String name,int birth,int sales,double rate,int basedSalary){
super(name,birth,sales,rate);
this.basedSalary = basedSalary;
}
public int getSalary(int month){
return this.basedSalary+super.getSalary(month) +
this.getAddtionalSalary();
}
public int getAddtionalSalary(){
return 1000;
}
}
经典算法:1. 某学校为学生分配宿舍,每6个人一间房(不考虑性别差异),问需要多少房?答案: (x+5)/6注意理解int类型数值。2. 让数值在 0~9 之间循环。public class test{ public static void main(String[] args){ int i=0; while(true){ i = (i+1)%10; System.out.println(i); } }}作业:1. 写一个数组类(放对象): 功能包括:添加(添加不限制多少项)、修改、插入、删除、查询 class MyArray{
private Object[] os = new Object[10];
public void add(Object o);
public void set(int index, Object o);
public void insert(int index, Objecto);
public void remove(int index);
public void remove(Object o);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -