Java课堂练习

第41题 编程求数组最大值及其下标

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
             int n=cin.nextInt();
             int ans=cin.nextInt();
             int pos=0;
             for(int i=1;i<n;i++)
             {
                 int x=cin.nextInt();
                 if(x>ans)
                 {
                     ans=x;
                     pos=i;
                 }
             }
             System.out.println(ans+" "+pos);
        }
    }

}

第42题 编程找x在某数组中的下标
 

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int []a= new int[2005];
        while(cin.hasNext())
        {
             int n=cin.nextInt();
             int pos=-1;
             for(int i=0;i<n;i++)
             {
                 a[i]=cin.nextInt();
             }
             int x=cin.nextInt();
             for(int i=0;i<n;i++)
             {
                 if(x==a[i]) {
                     pos=i;
                     break;
                 }
             }
             System.out.println(pos);
        }
    }
}

 第43题 编程实现最大最小数的互换
 

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int []a= new int[2005];
        while(cin.hasNext())
        {
             int n=cin.nextInt();
             for(int i=0;i<n;i++)
             {
                 a[i]=cin.nextInt();
             }
             int min=a[0],max=a[0];
             int minp=0,maxp=0;
             for(int i=1;i<n;i++)
             {
                 if(a[i]<min)
                 {
                     min=a[i];
                     minp=i;
                 }
                 if(a[i]>max)
                 {
                     max=a[i];
                     maxp=i;
                 }
             }
             int tmp=a[minp];
             a[minp]=a[maxp];
             a[maxp]=tmp;
             for(int i=0;i<n;i++)
             {
                 System.out.print(a[i]);
                 if(i!=n-1) System.out.print(" ");
             }
        }
    }
}

第44题 编写. 计算某日是星期几

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int W;
            int y=cin.nextInt();
            int m=cin.nextInt();
            int d=cin.nextInt();
            W=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1;
            System.out.println("星期="+W);
        }
        System.out.println("The Main class is end.");
    }
}

第45题 编程求若干个整数之最大值,最小值,和 
 

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int []a= new int[2005];
        while(cin.hasNext())
        {
             int n=cin.nextInt();
             for(int i=0;i<n;i++)
             {
                 a[i]=cin.nextInt();
             }
             int min=a[0],max=a[0];
             int sum=a[0];
             for(int i=1;i<n;i++)
             {
                 if(a[i]<min)
                 {
                     min=a[i];
                 }
                 if(a[i]>max)
                 {
                     max=a[i];
                 }
                 sum+=a[i];
             }
             System.out.println(max);
             System.out.println(min);
             System.out.println(sum);
        }
    }
}

第46题 编程:求Excel表中矩形数

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            String a= cin.next();
            String b=cin.next();
            char []s=a.toCharArray();
            char []t=b.toCharArray();
            int lens=s.length;
            int lent=t.length;
            char chs,cht;
            int ss,tt;
            int i=0,tmp=0;
            while(s[i]==' ') i++;
            chs=s[i];
            i++;
            tmp=0;
            while(i<lens)
            {
                tmp=tmp*10+s[i]-'0';
                i++;
            }
            ss=tmp;
            
            i=0;
            while(t[i]==' ') i++;
            cht=t[i];
            i++;
            tmp=0;
            while(i<lent)
            {
                tmp=tmp*10+t[i]-'0';
                i++;
            }
            tt=tmp;
            int n=cht-chs+1;
            int m=tt-ss+1;
            System.out.println(n*(n+1)*m*(m+1)/4);
        }
    }
}


 

第47题 填写代码. Point类例子(1)

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point pt = new Point(1, n);// 创建对象
        System.out.println("pt.x=" + pt.x);// 取
        pt.x = 5;// 修改
        System.out.println("pt.x=" + pt.x);
        pt.move(3, 3);// 移动
        System.out.println("pt.x=" + pt.x);
        System.out.println("pt.y=" + pt.y);
        Point pt2 = new Point();// 声明对象并new
        System.out.println("pt2.x=" + pt2.x);
        pt2 = new Point(9, 2);
        System.out.println("pt2.x=" + pt2.x);
    }
}

class Point {
    int x, y;// 成员变量,属性

    public Point() {// 无参构造方法
        x = 0;
        y = 0;
    }

    public Point(int ix, int iy) {// 有参构造方法
        x = ix;
        y = iy;
    }

    void move(int ix, int iy) {// 方法
        x += ix;// x=x+ix
        y += iy;
    }
}

第48题 填写代码. 类的基本操作简单例子

public class Main {
    public static void main (String args[ ]) {
       System.out.println("教学活动从教室开始");//命令行窗口输出"教学活动从教室开始"
       Teacher zhang = new Teacher();
       Student jiang = new Student();//创建对象
       zhang.introduceSelf();
       jiang.introduceSelf(); //调用它的方法
    }
}
class Teacher {
  void introduceSelf() {
     System.out.println("我是李老师");    //命令行窗口输出"我是李老师"
   }
}
 
class Student {
  void introduceSelf() {
     System.out.println("我是学生,名字是:奖励");//命令行窗口输出"我是学生,名字是:奖励"
   }
} 

第49题 填写代码. 关于Point类的操作(2)

public class Main {

    public static void main(String[] args) {
        Point Point1 = new Point(3, 4);
        System.out.println("Point1:" + "(" + Point1.x + "," + Point1.y + ")");

        Point Point2 = Point1.getPoint();
        System.out.println("Point2:" + "(" + Point2.x + "," + Point2.y + ")");

        Point Point3 = new Point(5, 6);
        Point1.setPoint(Point3);
        System.out.println("Point1:" + "(" + Point1.x + "," + Point1.y + ")");
    }
}

class Point {
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point getPoint() {
        Point tempPoint = new Point(x, y);
        return tempPoint;
    }

    public void setPoint(Point point) {
        this.x = point.x;
        this.y = point.y;
    }

}

 第50题 填写代码. 关于Point类的操作(3)

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Point origin = new Point(10, 10);
        origin.getPoint();
        origin.setPoint(20, 20);
        origin.getPoint();
    }
}

class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setPoint(int x1, int y1) {
        x = x1;
        y = y1;
    }

    public void getPoint() {
        System.out.println("Point x: " + x + ",y: " + y);
    }

}

第51题 编写部分代码. 按面向对象要求编程在Employee加入身份证

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);
        String name = reader.nextLine();
        String ID = reader.nextLine();
        int salary = reader.nextInt();
        int year = reader.nextInt();
        int month = reader.nextInt();
        int day = reader.nextInt();
        Employee e = new Employee(name, ID, salary, year, month, day);
        e.raiseSalary(5);
        System.out.println("姓名:" + e.getName() + " 身份证:" + e.getID() + " 工资:" + e.getSalary());
        System.out.println("The Main class is end.");
    }
}

class Employee {
    private String name;// 私有域,姓名
    private String ID;// 私有域,身份证
    private double salary;// 私有域,工资
    private Date hireDay; // 私有域,录用日期
    // 构造方法

    public Employee(String n, String id, double s, int year, int month, int day) {
        name = n;// 参数,(局部)变量
        salary = s;// 参数, (局部)变量
        ID = id;
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        // GregorianCalendar uses 0 for January(1月份是0)
        hireDay = calendar.getTime();
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public Date getHireDay() {
        return hireDay;
    }

    // "涨工资"计算
    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
    
    public String getID() {
        return ID;
    }

}

第52题 代码填空. 通过类完成加法运算

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println(Plus.add(5));// 显示5+2的结果
        Scanner reader = new Scanner(System.in);
        double y = reader.nextDouble();
        System.out.println(Plus.add(y));// 显示1+y的结果
        Plus aPlus = new Plus(3, 4);
        System.out.println(aPlus.add());// 显示3+4的结果
    }
}

class Plus {
    static int a = 1, b = 2;// 域
    int x, y;

    public Plus() {
        x = 0;
        y = 0;
    }

    public Plus(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static int add(int x) {
        return (x + b);
    }

    public int add() {
        return (x + y);
    }

    public static double add(double y) {
        return (a + y);
    }

    public static int add(int x, int y) {
        return (x + y);
    }
}

 第53题 填写代码,以Employee类为例的静态域和静态方法

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
   {
      Employee[] staff = new Employee[3];
 
      staff[0] = new Employee("Tom", 40000);
      staff[1] = new Employee("Dick", 60000);
      staff[2] = new Employee("Harry", 65000);
      Scanner reader=new Scanner(System.in);
      for (Employee e: staff)
      {
        int n=reader.nextInt();
        e.setId(n);
        System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
               + e.getSalary());
      }
      int n = Employee.getNextId(); // calls static method
      System.out.println("Next available id=" + n);
      System.out.println("The Main class is end.");
 
   }
}

class Employee {
    private static int nextId = 1;
    private String name;
    private double salary;
    private int id;

    public Employee(String n, double s) {
        name = n;
        salary = s;
        id = 0;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int n) {
        id = nextId; // set id to next available id
        nextId += n;
    }

    public static int getNextId() // 静态方法
    {
        return nextId; // returns static field
    }
}

第54题 编写程序:完美(回文串)的代价

import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
 
		while (scanner.hasNext()) {
			int n = Integer.parseInt(scanner.nextLine());
 
			String str = scanner.nextLine();
			char[] chs = str.toCharArray();
 
			int[] count = new int[26];
			char ch = '0';
			int oddchar = 0;
 
			for (int j = 0; j < chs.length; j++) {
				int index = chs[j] - 'a';
				count[index]++;
			}
 
			for (int i = 0; i < 26; i++) {
				if (count[i] % 2 != 0) {
					oddchar++;
					ch = (char) (i + 'a');
				}
			}
 
			if (oddchar > 1) {
				System.out.println("Impossible");
			} else {
				int result = exchange(chs, n, ch);
				System.out.println(result);
			}
		}
	}
 
	private static int exchange(char[] chs, int n, char ch) {
		int count = 0, i, j, k;
		for (i = 0; i < n / 2; i++) {
			if (chs[i] == ch) {
				for (j = i; j < n - i - 1; j++) {
					if (chs[j] == chs[n - i - 1]) {
						break;
					}
				}
 
				count += j - i;
 
				for (k = j; k > i; k--) {
					chs[k] = chs[k - 1];
				}
				chs[i] = chs[n - i - 1];
 
			} else {
				for (j = n - i - 1; j >= i; j--) {
					if (chs[j] == chs[i]) {
						break;
					}
				}
 
				count += n - i - 1 - j;
 
				for (k = j; k < n - i - 1; k++) {
					chs[k] = chs[k + 1];
				}
				chs[n - i - 1] = chs[i];
			}
		}
		return count;
	}
}

第55题 编写程序:字符串对比
 

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            String s=cin.nextLine();
            String t=cin.nextLine();
            int lens=s.length();
            int lent=t.length();
            if(lens!=lent) System.out.println("1");
            else 
            {
                boolean bo=true;
                for(int i=0;i<lens;i++)
                {
                    if(s.charAt(i)!=t.charAt(i)) 
                    {
                        bo=false;
                        break;
                    }
                }
                if(bo) System.out.println("2");
                else 
                {
                    boolean bo2=true;
                    for(int i=0;i<lens;i++)
                    {
                        if(s.charAt(i)!=t.charAt(i)) 
                        {
                            if(Math.abs(s.charAt(i)-t.charAt(i))!=32)
                            {
                                bo2=false;
                                break;
                            }
                        }
                    }
                    if(bo2) System.out.println("3");
                    else System.out.println("4");
                }
            }
        }
    }
}

第57题 填写代码,以Point类为例的类继承

import java.util.Scanner;

class Point {
    public int x, y;// 域(成员属性)
    // 无参构造器(方法)

    public Point() {
        x = 0;
        y = 0;
    }

    // 有参构造器(方法)
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // 成员方法
    protected void move(int ix, int iy) {
        x += ix;
        y += iy;
    }
}

class Son extends Point {
    private int x, y;// 隐藏父亲的x,y
    // 无参

    public Son() {
        this.x = 0;
        this.y = 0;
    }

    // 有参
    public Son(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

class Son1 extends Point {
    public Son1() {
        // this.x=0;
        // this.y=0;
        super();// 调用父类的构造器(方法)Point()
    }

    // 有参
    public Son1(int x, int y) {
        // this.x=x;
        // this.y=y;
        super();// 调用父类的构造器(方法)Point(int x,int y)
    }

    public void move(int ix, int iy) {
        super.move(ix, iy);// 调用父类的move(...)
    }
}

public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Point pt = new Point();// 声明对象变量,创建(实例化)
        pt.x = 1;// pt中x赋值1
        System.out.println("pt.x=" + pt.x + "\npt.y=" + pt.y);
        Son son = new Son(1, 2);
        Son1 son1 = new Son1();
        son1.x = 1;
        son1.move(2, 3);// 继承
        System.out.println("son1.x=" + son1.x + "\nson1.y=" + son1.y);
        son.move(2, 3);// 继承
        System.out.println("son.x=" + son.getX());// 显示son中x的值
    }
}

 

第58题   填写代码。运用继承进行两个银行存款计算利息的例子.

class Bank {
   int savedMoney;
   int year;
   double interest;
   double interestRate = 0.29;
   public double computerInterest() {
      interest=year*interestRate*savedMoney;
      return  interest;//返回利息
   }
   public void setInterestRate(double rate) {
      interestRate = rate;
   }
}
class ConstructionBank extends Bank {
   double year;
   public double computerInterest() {
      super.year=(int)year;
      double r = year-(int)year;
      int day=(int)(r*1000);
      double yearInterest = super.computerInterest(); //super调用隐藏的computerInterest()方法
      double dayInterest = day*0.0001*savedMoney;
      interest= yearInterest+dayInterest;
      System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
                         savedMoney,super.year,day,interest);
      return interest;
   }
}
class BankOfDalian extends Bank {  //继承
   double year;
   public double computerInterest() {
      super.year=(int)year;
      double r = year-(int)year;
      int day=(int)(r*1000);
      double yearInterest = super.computerInterest();// super调用隐藏的computerInterest()方法
      double dayInterest = day*0.00012*savedMoney;
      interest= yearInterest+dayInterest;
      System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",
                         savedMoney,super.year,day,interest);
      return interest;
   }
}
public class Main {
   public static void main(String args[]) {
      int amount=8000;
      ConstructionBank bank1 = new ConstructionBank();
      bank1.savedMoney = amount;
      bank1.year = 8.236;
      bank1.setInterestRate(0.035);
      double interest1 = bank1.computerInterest();//计算利息
      BankOfDalian bank2 = new BankOfDalian();
      bank2.savedMoney = amount;
      bank2.year = 8.236;
      bank2.setInterestRate(0.035);
      double interest2=bank2.computerInterest();//计算利息
      System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
   }
}

  

第59题 填写代码,基类Person

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Student[] aStudent = new Student[3];
        Scanner reader = new Scanner(System.in);
        aStudent[0] = new Student("Tom", reader.nextLine());
        aStudent[1] = new Student("Dick", reader.nextLine());
        aStudent[2] = new Student("Harry", reader.nextLine());
        for (Student e : aStudent) {
            System.out.println("name=" + e.getName() + ",id=" + e.getDescription());
        }
        System.out.println("The Main class is end.");
    }
}

abstract class Person {
    private String name;

    public String getName() {
        return name;
    }

    public Person(String n) {
        name = n;
    }

    public abstract String getDescription();
}

class Student extends Person {
    private String major;

    public Student(String n, String m) {
        super(n);
        major = m;
    }

    @Override
    public String getDescription() {
        return "a student majoring in " + major;
    }
}

第60题 代码填空题, 对象传递参数

  

public class Main {
    public void tryChange(OnlyTest form) {
        int t = form.getX(); // 获取对象form的成员变量x的值
        form.setX(t * 2); // 改变对象form的成员变量x的值
    }

    public void showDiffer() {
        OnlyTest actual = new OnlyTest();
        actual.setX(100);
        System.out.println("调用tryChange方法之前,x=" + actual.getX());
        // 测试是否能改变actual的成员变量值
        tryChange(actual);
        System.out.println("调用tryChange方法之后,x=" + actual.getX());
    }

    public static void main(String args[]) {
        Main va = new Main();
        va.showDiffer();
    }
}

class OnlyTest {
    private int x = 0;

    // 设置成员变量x的值
    public void setX(int ix) {
        x = ix;
    }

    // 获取成员变量x的值
    public int getX() {
        return x;
    }
}

第61题 代码填空题,隐式调用无参数的构造方法

class Main {
    private int x = 0;

    // 定义一个无参数的构造方法,它必须和类同名
    public Main() {
        System.out.println("这是无参数的构造方法");
        setX(100); // 为成员变量赋值
    }

    // 设置成员变量x的值
    public void setX(int x) {
        this.x = x;
    }

    // 获取成员变量x的值
    public int getX() {
        return x;
    }

    public static void main(String args[]) {
        Main oa = new Main(); // 隐式调用无参数的构造方法
        System.out.println("x=" + oa.getX()); // 输出成员变量x的值
    }
}

 

第62题 填写代码,接口Colors

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        BaseColors bC = new RainbowColorsImp();// 需要使用实现类进行实例化
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();
        String colorName = bC.getColorName(n);// 获取值为n=1,2,..,7的颜色名称.
        System.out.println(colorName + "色");// 显示颜色名称
        System.out.println("The Main class is end.");
    }
}

interface BaseColors {
    // 这里都是静态公共常量
    int RED = 1, BLUE = 4; // 红、蓝
    // 这是一个抽象的公共方法

    String getColorName(int color);// 获取颜色名称
}

interface RGBColors extends BaseColors {
    // 这是静态常量
    int GREEN = 2;// 增加 绿色
}

class RGBColorsImp implements RGBColors {
    @Override
    public String getColorName(int color) {
        switch (color) {
        case 1:
            return "红";
        case 2:
            return "绿";
        case 4:
            return "蓝";
        default:
            return "未知";
        }
    }
}

// 彩虹七种颜色:红色、黄色、绿色、蓝色、粉色、棕色、紫色
// red,yellow,green,blue,pink,brown,purple
interface RainbowColors extends RGBColors {
    // 新增加了4个成员常量
    int YELLOW = 3, PINK = 5, BROWN = 6, PURPLE = 7;
}

// 彩虹七种颜色:红色、黄色、绿色、蓝色、粉色、棕色、紫色
class RainbowColorsImp extends RGBColorsImp implements RainbowColors {
    @Override
    public String getColorName(int color) {
        switch (color) {
        case 3:
            return "黄";
        case 5:
            return "粉";
        case 6:
            return "棕";
        case 7:
            return "紫";
        default:
            return super.getColorName(color);
        }
    }
}

第63题 代码填空, Colors接口基本使用操作

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Baseclass b = new Baseclass();
        int n = b.getColorValue();
        System.out.println("n=" + n);
    }
}

// 定义接口BaseColors
interface BaseColors {
    // 这里都是静态公共常量
    int RED = 1, GREEN = 2, BLUE = 4;

    // 这是一个抽象的公共方法
    int getColorValue(int color);
}

// 接口RainbowColors继承了BaseColors
interface RainbowColors extends BaseColors {
    // 新增加了4个成员常量
    int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7;
    // 还自动继承了父接口的3个成员常量和1个抽象方法

}

// 接口PrintColors继承了BaseColors
interface PrintColors extends BaseColors {
    // 增加3个成员常量
    int YELLOW = 8, CYAN = 16, MAGENTA = 32;

    // 覆盖了父接口的成员方法,仍然是抽象的
    int getColorValue(int color);

    // 还可以重载,和其它方法名相同,但参数有差异
    int getColorValue();
}

// 接口PrintColors继承了BaseColors
interface LotsOfColors extends RainbowColors, PrintColors {
    // 这是多重继承,增加3个成员常量
    int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED + 90;
}

class Baseclass implements LotsOfColors {
    @Override
    public int getColorValue() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getColorValue(int color) {
        // TODO Auto-generated method stub
        return 0;
    }
}

第64题 代码填空题, 子类的变量覆盖父类变量

import java.util.*;

public class Main extends Ancestor { // 定义一个子类
    private int defVar = 100; // 将父类的defVar隐藏起来

    public int getValue() {
        return defVar;
    }

    public static void main(String args[]) {
        Main oa = new Main();
        System.out.println("oa.defVar=" + oa.defVar);
        System.out.println("oa.getValue()=" + oa.getValue());
        oa.proShow();
        oa.pubShow();
    }
}

class Ancestor {
    private int priVar = 1;
    int defVar = 2;
    protected int proVar = 3;
    public int pubVar = 4;

    private void priShow() {
        System.out.println("This is a private method");
    }

    void defShow() {
        System.out.println("This is a default method");
    }

    protected void proShow() {
        System.out.println("This is a protected method,defVar=" + defVar);
    }

    public void pubShow() {
        System.out.println("This is a public method,pubVar=" + pubVar);
    }
}

第65题 编写部分代码, 使用内部类判断一个long型整数n是否为回文数

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		long n = reader.nextLong();
		Calc a = new Calc();
		boolean b = a.judge(n);
		if (b)
			System.out.println("yes");
		else
			System.out.println("no");
		System.out.println("The Main class is end.");
	}
}

class Calc {
	public Calc() {
	}

	public boolean judge(long n) {
		/*-----请编写代码-----*/
		int cnt = 0;
		int tmp[] = new int[50];
		while (n > 0) {
			tmp[++cnt] = (int) (n % 10);
			n /= 10;
		}
		for (int i = 1, j = cnt; i <= j; i++, j--) {
			if (tmp[i] != tmp[j])
				return false;
		}
		return true;
		/*-----编写的代码结束-----*/
	}
}

 第83题 编写程序:关联矩阵

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int [][]a = new int[2005][2005];
        while(cin.hasNext())
        {
            int n=cin.nextInt();
            int m=cin.nextInt();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    a[i][j]=0;
                }
            }
            for(int i=1;i<=m;i++)
            {
                int u=cin.nextInt();
                int v=cin.nextInt();
                a[u][i]=1;
                a[v][i]=-1;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    System.out.print(a[i][j]);
                    if(j!=m) System.out.print(" ");
                }
                System.out.println("");
            }
        }
    }
}

  第84题 逆序数(小数据)

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int a[] = new int[1005]; 
        int t=cin.nextInt();
        while(t-- >0)
        {
            int ans=0;
            int n = cin.nextInt();
            for(int i=1;i<=n;i++) a[i]=cin.nextInt();
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(a[i]>a[j]) ans++;
                }
            }
            System.out.println(ans);
        }
    }
}

 第85题 编写程序:D_Prim(特殊质数)

import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class Main {

	public static boolean pd(int x)
	{
		for(int i=2;i*i<=x;i++)
		{
			if(x%i==0) return false;
		}
		return true;
	}
	public static void main(String[] args) throws IOException {
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		in.nextToken();
		int T=(int)in.nval;
		
		while(T-- >0)
		{
			in.nextToken();
			int n=(int)in.nval;
			boolean bo=false;
			for(int i=2;i*i<=n;i++)
			{
				if(n%i==0&&i!=n/i&&pd(i)&&pd(n/i))
				{
					bo=true;
					out.println("Yes");
				}
			}
			if(!bo) out.println("No");
		}
		out.flush();
	}
}

猜你喜欢

转载自blog.csdn.net/QuincyTan/article/details/84518934