zoukankan      html  css  js  c++  java
  • Java面向对象5(V~Z)

    计算各种图形的周长(接口与多态)(SDUT 3338)

    import java.util.Scanner;
     
    public class Main {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		while(in.hasNext()){
    			String str = in.nextLine();
    			String[] strs = str.split(" ");
    			double []a = new double [100];
    			int i;
    			for(i = 0; i < strs.length; i++)
    				a[i] = Integer.parseInt(strs[i]);
    			double x,y,z;
    			if(i == 1){
    				x = a[0];
    				if(x <= 0)
    					 System.out.println("0.00");
    				else{
    					Circle c = new Circle(x);
    					System.out.printf("%.2f
    ",c.length());
    				}
    			}
    			else if(i == 2){
    				x = a[0];
    				y = a[1];
    				if(x <= 0)
    					 System.out.println("0.00");
    				else{
    					Rectangle r = new Rectangle(x,y);
    					System.out.printf("%.2f
    ",r.length());
    				}
    			}
    			else if(i == 3){
    				for(i = 0; i < 3; i++){
    					for(int j = 0; j < i; j++){
    						if(a[j] > a[j+1]){
    							double t = a[j];
    							a[j] = a[j+1];
    							a[j+1] = t;
    						}
    					}
    				}
    				x = a[0];
    				y = a[1];
    				z = a[2];
    				if(x + y > z ){
    					if(x <= 0)
    					 System.out.println("0.00");
    				else{
    					Triangle T = new Triangle(x,y,z) ;
    					System.out.printf("%.2f
    ",T.length());
    				}
    			}
    			else
    					System.out.println("0.00"); 
    			}
    		}
    		in.close();
    	}
    }
    interface Shape{
    	//public void shape();
    	double length();
     
    }
    class Triangle implements Shape{
    	double x1,y1,z1;
    	Triangle(double x1, double y1 , double z1){
    		this.x1 = x1;
    		this.y1 = y1;
    		this.z1 = z1;	
    	}
    	
    	public double length(){
    		return x1 + y1 + z1;
    	}
    }
    class Rectangle implements Shape{
    	double x,y;
    	 Rectangle(double x, double y){
    		this.x = x;
    		this.y = y;	
    	}
    	
    	public double length(){
    		return (x + y) * 2;
    	}
    }
    class Circle implements Shape{
    	double z;
        Circle(double z){
    		this.z = z;
    	}
    	public double length(){
    		return 2 * 3.14 * z;
    	}
    }

    计算长方形的周长和面积(类和对象)(SDUT 3339)

    import java.util.*;
     
    class Rect{
    	public int n;
    	public int m;
    	Rect(int n){
    		System.out.println(n+" "+n+" "+4*n+" "+n*n);
    	}
    	Rect(int n, int m){
    		System.out.println(n+" "+m+" "+(2*n+2*m)+" "+m*n);
    	}
    }
     
    public class Main{
    	public static void main(String args[]){
    		Scanner scanner = new Scanner(System.in);
    		while ( scanner.hasNext() ){
    			String h = scanner.nextLine();
    			char[] a = h.toCharArray();
    			int temp = 0;
    			int i;
    			for ( i = 0;i < a.length; i++ ){
    				if ( a[i] == ' ' )
    					temp = 1;
    			}
    			if ( temp == 0 ){
    				int b = Integer.parseInt(h);
    				if ( b <= 0 )
    					System.out.println("0 0 0 0");
    				else{
    				    Rect h1 = new Rect(b);}
    			}
    			else {
    				String[] h3 = h.split(" ");
    				int e = Integer.parseInt(h3[0]);
    				int r = Integer.parseInt(h3[1]);
    				if ( e <= 0||r <= 0 )
    					System.out.println("0 0 0 0");
    				else{
    				    Rect h1 = new Rect(e,r);}
    			}
    		}
    	}
    }

    X 答答租车系统(面向对象综合练习)(SDUT 3349)

    import java.util.*;
    
    class Car
    {
        int name, money, passenger;
        double weight;
    
        public Car(int name, int money, double weight,  int passenger)
        {
            this.name = name;
            this.money = money;
            this.passenger = passenger;
            this.weight = weight;
        }
    
    }
    
    public class Main
    {
    
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            Car [] c =
            {
                new Car(0, 5, 0, 800),
                new Car(1, 5, 0, 400),
                new Car(2, 5, 0, 800),
                new Car(3, 51, 0, 1300),
                new Car(4, 55, 0, 1500),
                new Car(5, 5, 0.45, 500),
                new Car(6, 5, 2, 450),
                new Car(7, 0, 3, 200),
                new Car(8, 0, 25, 1500),
                new Car(9, 0, 35, 2000)
            };
            int peoplesum = 0;
            int moneysum = 0;
            double weightsum = 0;
            int order = sc.nextInt();
            if(order == 1)
            {
                int num = sc.nextInt();
                for(int i = 0; i < num; i++)
                {
                    int j = sc.nextInt() - 1;
                    int day = sc.nextInt();
                    moneysum += c[j].money * day;
                    peoplesum += c[j].passenger * day;
                    weightsum += c[j].weight * day;
                }
                System.out.println(moneysum + " " + String.format("%.2f", weightsum) + " " + peoplesum);
            }
            else
            {
                System.out.println(moneysum + " " + String.format("%.2f", weightsum) + " " + peoplesum);
            }
            sc.close();
        }
    }
    
    

    Y   分数四则运算 (SDUT 3849)

    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    class Sum {
    	int x1, y1, x2, y2;
    	char str;
    
    	Sum(int n1, int m1, int n2, int m2, char op) {
    		x1 = n1;
    		x2 = n2;
    		y1 = m1;
    		y2 = m2;
    		str = op;
    	}
    
    	int getGcd(int a, int b) {
    		int n = a, m = b;
    		while (m > 0) {
    			int x = n;
    			n = m;
    			m = x % m;
    		}
    		return n;
    	}
    
    	void getAns() {
    		int x = getGcd(y1, y2);
    		int a, b, c, d, ans1 = 0, ans2 = 0;
    		a = x1;
    		b = y1;
    		c = x2;
    		d = y2;
    		if (str == '+' || str == '-') {
    			int lcm = b * d / x;
    			a = a * d / x;
    			c = c * b / x;
    			if (str == '+') {
    				ans1 = a + c;
    			} else if (str == '-') {
    				ans1 = a - c;
    			}
    			ans2 = lcm;
    			if (ans1 < 0)
    				x = -ans1;
    			else
    				x = ans1;
    			x = getGcd(x, ans2);
    			if (ans1 % x == 0 && ans2 % x == 0) {
    				ans1 /= x;
    				ans2 /= x;
    			}
    		} else if (str == '*') {
    			a = a * c;
    			b = b * d;
    			ans1 = a;
    			ans2 = b;
    			int temp = getGcd(ans1, ans2);
    			if (ans1 % temp == 0 && ans2 % temp == 0) {
    				ans1 /= temp;
    				ans2 /= temp;
    			}
    		} else if (str == '\') {
    			int temp = c;
    			c = d;
    			d = temp;
    			a = a * c;
    			b = b * d;
    			ans1 = a;
    			ans2 = b;
    			int te = getGcd(ans1, ans2);
    			if (ans1 % te == 0 && ans2 % te == 0) {
    				ans1 /= te;
    				ans2 /= te;
    			}
    		}
    
    		if (ans1 == 0 && ans1 != ans2 || ans2 == 1)
    			System.out.println(ans1);
    		else if (ans1 == ans2)
    			System.out.println(1);
    		else
    			System.out.println(ans1 + "/" + ans2);
    	}
    }
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		// DecimalFormat df = new DecimalFormat(".00");
    		Sum p;
    		String s;
    		char op = 0;
    		while (sc.hasNext()) {
    			s = sc.next();
    			int temp = 0;
    			int xx[] = new int[5];
    			int top = 0;
    			int x1, x2, y1, y2;
    			int len = s.length();
    			for (int i = 0; i < len; i++) {
    				if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
    					xx[top] = xx[top] * 10 + (s.charAt(i) - '0');
    				} else {
    					top++;
    					if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '\')
    						op = s.charAt(i);
    				}
    			}
    			x1 = xx[0];
    			y1 = xx[1];
    			x2 = xx[2];
    			y2 = xx[3];
    			// System.out.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + op);
    			p = new Sum(x1, y1, x2, y2, op);
    			p.getAns();
    		}
    	}
    }

    Z    Shift Dot   (SDUT 3848)

    import java.util.*;
    
    class Dot {
    	int x, y;
    
    	Dot() {
    
    	}
    
    	Dot(int n, int m) {
    		x = n;
    		y = m;
    	}
    
    	void getAns(int a, int b) {
    		x += a;
    		y += b;
    	}
    
    	void Print() {
    		System.out.println("(" + x + "," + y + ")");
    	}
    }
    
    public class Main {
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    		int x, y, n, a, b;
    		while (sc.hasNext()) {
    			x = sc.nextInt();
    			y = sc.nextInt();
    			n = sc.nextInt();
    			Dot p = new Dot(x, y);
    			for (int i = 0; i < n; i++) {
    				a = sc.nextInt();
    				b = sc.nextInt();
    				p.getAns(a, b);
    			}
    			p.Print();
    		}
    	}
    }
  • 相关阅读:
    Ynoi2016 这是我自己的发明
    Luogu P5268 [SNOI2017]一个简单的询问
    Ynoi2017 由乃的玉米田
    BZOJ4320 [Shanghai2006]Homework
    JOISC2014C 歴史の研究
    莫队
    LOJ6119 「2017 山东二轮集训 Day7」国王
    Luogu P3295 [SCOI2016]萌萌哒
    10.1 进程间通信--消息队列
    9.2 网络协议理论
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139512.html
Copyright © 2011-2022 走看看