Java面向对象1(A~F)

QWQ请假一节课,错过一章内容,只能求助qsh了。

C/C++训练1---最大公约数与最小公倍数(SDUT 1131)

import java.util.*;

class Number {
	int a, b;

	Number(int n, int m) {
		a = n;
		b = m;
	}

	int getGcd() {
		int n = a, m = b;
		while (m > 0) {
			int x = n;
			n = m;
			m = x % m;
		}
		return n;
	}

	int getLcm() {
		int x = getGcd();
		return a * b / x;
	}

	void Print() {
		System.out.println(getGcd() + "
" + getLcm());
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Number num = new Number(sc.nextInt(), sc.nextInt());
		num.Print();
		sc.close();
	}
}

C/C++经典程序训练3---模拟计算器

import java.util.Scanner;
class Number
{
	int a, b;
	String c;
	Number(int n, int m, String k)
	{
		a = n;
		b = m;
		c = k;
	}
	int getAns()
	{
		int ans = 0;
		if(c.equals("+"))ans = a + b;
		else if(c.equals("-"))ans = a - b;
		else if(c.equals("*"))ans = a * b;
		else ans = a / b;
		return ans;
	}
	void Print()
	{
		System.out.println(getAns());
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a, b;
		String c;
		a = sc.nextInt();
		b = sc.nextInt();
		sc.nextLine();
		c = sc.nextLine();
		Number p = new Number(a, b, c);
		p.Print();
		sc.close();
	}
}

面向对象程序设计上机练习一(函数重载)(SDUT 1140)

import java.util.Scanner;
class Max
{
	
	 static int getMax(int a[])
	{
		int ans = -1;
		for(int i = 0; i < 5; i ++) if(a[i] > ans) ans = a[i];
		return ans;
	}
	 static float getMax(float b[])
	{
		float ans = -1;
		for(int i = 0; i < 5; i ++) if(b[i] > ans) ans = b[i];
		return ans;
	}
	 static  long  getMax(long  c[])
	{
		long ans = -1;
		for(int i = 0; i < 5; i ++) if(c[i] > ans) ans = c[i];
		return ans;
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[10];
		float b[] = new float[10];
		long c[] = new long[10];
		for(int i = 0; i < 5; i ++) a[i] = sc.nextInt();
		for(int i = 0; i < 5; i ++) b[i] = sc.nextFloat();
		for(int i = 0; i < 5; i ++) c[i] = sc.nextLong();
		Max max = new Max();
		System.out.print(max.getMax(a)+"
"+max.getMax(b)+"
"+max.getMax(c)+"
");
	}
}

D    圆的面积 (SDUT 1588)

import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
	double x;

	Sum(double n) {
		x = n;
	}

	double getAns() {
		return (x * x * 3.141592653);
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		DecimalFormat df = new DecimalFormat(".00");
		int t;
		t = sc.nextInt();
		for (int i = 1; i <= t; i++) {
			Sum n = new Sum(sc.nextDouble());
			System.out.println("Case" + " " + i + ":" + " " + df.format(n.getAns()));
		}
	}
}

E    正方形面积(SDUT 2101)

import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
	long x;

	Sum(long n) {
		x = n;
	}

	long getAns() {
		return (x * x);
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// df = new DecimalFormat(".00");
		while (sc.hasNext()) {
			Sum n = new Sum(sc.nextLong());
			System.out.println(n.getAns());
		}
	}
}

F    回文时间 (SDUT 2174)

import java.util.*;
class Time{
	String s;
	int a,b,c,d;
	Time(String s){
		this.s = s;
		a = s.charAt(0) - '0';
		b = s.charAt(1) - '0';
		c = s.charAt(3) - '0';
		d = s.charAt(4) - '0';
	}
	void getAns() {
		if(a == d && b == c)d ++;
		while(a != d || b != c) {
			d ++;
			if(d == 10) {
				d = 0;
				c ++;
			}
			if(c == 6) {
				c = 0;
				b ++;
			}
			if(b == 10) {
				b = 0;
				a ++;
			}
			if(b + a * 10 >= 24) {
				a = 0;
				b = 0;
			}
		}
		System.out.println(a +""+ b + ":" + c +""+ d);
	}
}
public class Main{
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		String s;
		while(sc.hasNext()) {
			s = sc.nextLine();
			Time p = new Time(s);
			p.getAns();
		}
	}
}
原文地址:https://www.cnblogs.com/lcchy/p/10139507.html