【面向对象】SDUT 实验二实验三

本文解析了SDUT在线评测系统中的六道经典编程题目,包括最大公约数与最小公倍数计算、组合数计算、分数加减法、正方形判断、相似三角形判断以及手机键盘输入时间计算,提供了详细的代码实现。

1.SDUT 1131 最大公约数与最小公倍数

 

/*
Problem Description
输入两个正整数,求它们的最大公约数与最小公倍数。
Output
第一行输出最大公约数;第二行输出最小公倍数。答案保证在 int 范围内。
*/
import java.util.Scanner;
class node{
	int a,b;
	node(int x,int y)
	{
		a=x;
		b=y;
	}
	int f() {
		while(b!=0)
		{
			int r=a%b;
			a=b;
			b=r;
		}
		return a;
	}
}
public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		node jie;
		int x = s.nextInt();
		int y =s.nextInt();
		jie=new node(x,y);
		int c=jie.f();
		int d= (x*y)/c;
		System.out.println(c);
		System.out.println(d);
		s.close();
	}
}

2.SDUT 1586 计算组合数

/*
Problem Description
计算组合数。C(n,m),表示从n个数中选择m个的组合数。
计算公式如下:
若:m=0,C(n,m)=1
否则, 若 n=1,C(n,m)=1
             否则,若m=n,C(n,m)=1
                         否则 C(n,m) = C(n-1,m-1) + C(n-1,m).
Input
第一行是正整数N,表示有N组要求的组合数。接下来N行,每行两个整数n,m (0 <= m <= n <= 20)。
Output
输出N行。每行输出一个整数表示C(n,m)。
Sample Input
3
2 1
3 2
4 0
Sample Output
2
3
1
*/
import java.util.Scanner;
 
class lei {
	int f(int n, int m) {
		int sum;
		if (m == 0 || n == 1 || m == n)
			sum = 1;
		else
			sum = f(n - 1, m - 1) + f(n - 1, m);
		return sum;
	}
}
 
public class Main {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int c = s.nextInt();
		while (c>0) {c--;
			int n = s.nextInt();
			int m = s.nextInt();
			lei t = new lei();//首先在lei中建立对象t
			int summ = t.f(n, m);//通过实例对象调用类的方法
			System.out.println(summ);
		}
		s.close();
	}
}

3.sdut 2253 分数加减法

/*
Problem Description
编写一个C程序,实现两个分数的加减法
Input
输入包含多行数据
每行数据是一个字符串,格式是"a/boc/d"。
其中a, b, c, d是一个0-9的整数。o是运算符"+"或者"-"。
数据以EOF结束
输入数据保证合法
Output
对于输入数据的每一行输出两个分数的运算结果。
注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数
Sample Input
1/8+3/8
1/4-1/2
1/3-1/3
Sample Output
1/2
-1/4
0
*/

//(注意如果在eclipse里同包下面写f.java的话,在交代码的时候把class权限改成默认,不然报错)

import java.util.Scanner;
 
class f {
	int fm,fz;
	
	public f(int fz, int fm) {
		super();
		this.fm = fm;
		this.fz = fz;
	}
	public f() {};
	public f add(f x)
	{
		int z=fm*x.fz + x.fm*fz;
		int m=fm*x.fm;
		int gys=gys(z,m);
		return  new f(z/gys,m/gys);
	}
	public int gys(int a,int b)
	{
		int r,t;
		//System.out.println(a+" "+b);
		if(a<b)
		{
			t=a;
			a=b;
			b=t;
		}
		while(b!=0)
		{
			r=a%b;
			a=b;
			b=r;
		}
		return a;
	}
	public f del(f x)
	{
		int z=x.fm*fz-fm*x.fz;
		int m=fm*x.fm;
		int gys=gys(z,m);
		//System.out.println(z);//这里记得判断符号,一定要在分子前面
		if(z<0)
		{
			m=-m;
			z=-z;
		}
		return  new f(z/gys,m/gys);
	}
}
public class Main {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while(s.hasNext())
		{
			String str=s.nextLine();
			char a[]=str.toCharArray();
			int az=a[0]-'0';
			int am=a[2]-'0';
			int bz=a[4]-'0';
			int bm=a[6]-'0';
			
			f r1=new f(az, am);
			f r2=new f(bz, bm);
			
			f result=new f();
			if(a[3]=='+')
			{
				result=r1.add(r2);
			}
			else if(a[3]=='-')
			{
				result=r1.del(r2);
			}
			if(result.fz%result.fm==0)
				System.out.println(result.fz/result.fm);
			else System.out.println(result.fz+"/"+result.fm);
		}	
		s.close();
	}
}

4.SDUT 2444 正方形

/*
Problem Description
给出四个点,判断这四个点能否构成一个正方形。
Input
 输入的第一行包含一个整数T(T≤30)表示数据组数,每组数据只有一行,包括8个整数x1, y1, x2, y2,x3,y3,x4,y4(数据均在-1000,1000 之间)以逆时针顺序给出四个点的坐标。
Output
 每组数据输出一行,如果是正方形,则输出: YES, 否则,输出:NO。
Sample Input
2
0 0 1 0 1 1 0 1
-1 0 0 -2 1 0 2 0
Sample Output
YES
NO
*/
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		 int t=s.nextInt();
		 while(t-->0)
		 {
			pan x1= new pan(s.nextInt(),s.nextInt());
			pan x2= new pan(s.nextInt(),s.nextInt());
			pan x3=new pan(s.nextInt(),s.nextInt());
			pan x4=new pan(s.nextInt(),s.nextInt());
			if(x1.f(x2)==x2.f(x3)&&x1.f(x2)==x3.f(x4)&&x1.f(x2)==x4.f(x1)&&x1.f(x3)==x2.f(x4))
				System.out.println("YES");//对角线相等
			else 
				System.out.println("NO");
		 }
		 s.close();
	}
}
 
class pan {
	int x,y;
 
	public pan(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
 
	public int f(pan p) {
		return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);
	}
}

5.SDUT 2562 相似三角形

/*
Problem Description
给出两个三角形的三条边,判断是否相似。
Input
多组数据,给出6正个整数,a1,b1,c1,a2,b2,c2,分别代表两个三角形。(边长小于100且无序)
Output
如果相似输出YES,如果不相似输出NO,如果三边组不成三角形也输出NO。
Sample Input
1 2 3 2 4 6
3 4 5 6 8 10
3 4 5 7 8 10
Sample Output
NO
YES
NO
*/
//主类:
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while (s.hasNext()) {
			tri x = new tri(s.nextInt(), s.nextInt(), s.nextInt());
			tri y = new tri(s.nextInt(), s.nextInt(), s.nextInt());
			int a1 = x.pan1(x);
			int a2 = y.pan1(y);
			if (a1 == 1 || a2 == 1)
				System.out.println("NO");
			else
				x.pan2(y);
		}
		s.close();
	}
}
//tri类:
import java.util.Arrays;
 
public class tri {
	int a,b,c;
 
	public tri(int a, int b, int c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public tri() {}
	public int pan1(tri t)
	{
		if(a+b>c&&a+c>b&&b+c>a)
			return 0;
		else
			return 1;
	}
	public void pan2(tri t)
	{
		int[] p=new int[3];
		p[0]=a;
		p[1]=b;
		p[2]=c;
		Arrays.sort(p);
		a=p[0];
		b=p[1];
		c=p[2];
		//懒得建新方法了,复制粘贴
		p[0]=t.a;
		p[1]=t.b;
		p[2]=t.c;
		Arrays.sort(p);
		t.a=p[0];
		t.b=p[1];
		t.c=p[2];
		if(a*t.b==b*t.a&&b*t.c==c*t.b)
			System.out.println("YES");
		else System.out.println("NO");
	}
}

6.SDUT 2618 手机键盘

/*
Problem Description
大家应该都见过那种九键的手机键盘,键盘上各字母的分布如下图所示。
当我们用这种键盘输入字母的时候,对于有些字母,往往会需要按多次键才能输入。
比如:a, b, c 都在“2”键上,输入 a 只需要按一次,而输入 c 需要连续按三次。
连续输入多个字母的规则如下:
1、如果前后两个字母不在同一个按键上,则可在输入前一个字母之后直接输入下一个字母,如:ad 需要按两次键盘,kz 需要按 6 次。
2、如果前后两个字母在同一个按键上,则输入完前一个字母之后需要等待一段时间才能输入下一个字母,如ac,在输入完 a 之后,需要等一会儿才能输入 c。
现在假设每按一次键盘需要花费一个时间段,等待时间需要花费两个时间段。
现在给出一串只包含小写英文字母的字符串,计算出输入它所需要花费的时间。
Input
输入包含多组测试数据,对于每组测试数据:输入为一行只包含小写字母的字符串,字符串长度不超过100。
Output
对于每组测试数据,输出需要花费的时间。
Sample Input
bob
www
Sample Output
7
7
*/
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while (s.hasNext()) {
			String str[] = { new String("abc"), new String("def"), new String("ghi"), new String("jkl"),
					new String("mno"), new String("pqrs"), new String("tuv"), new String("wxyz") };
			String v = s.nextLine();
			char[] a = v.toCharArray();
			int n = v.length();
			int sum = 0, f = -1;
			for (int i = 0; i < n; i++)
				for (int j = 0; j < 8; j++)
					for (int k = 0; k < str[j].length(); k++)
//这里!要关注有的键盘管3个字母,有的是四个,所以k不能设置成定值。
						if (a[i] == str[j].charAt(k)) 
						{
							sum += k + 1;
							if (f == j)
								sum += 2;
							f = j;
							break;
						}
			System.out.println(sum);
		}
		s.close();
	}
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值