java题册

更新时间:2023-12-06 09:07:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

E.面积最大的直角三角形(作业3) Description

小明家里有很多长度不一的木棍,他想用这些木棍中的三根组成一个直角三角形。可能有多种组合结果,但小明想要一个面积最大的直角三角形。 Input

若干组数据,每组数据由两行组成,第1行表示木棍的数量,第2行表示每根木棍的长度。木棍最多不超过100个。长度都是整数。 Output

面积最大的直角三角形的面积,保留两位小数。如果所给的数据不能组成直角三角形,则输出“NONE”。 Sample Input 4

4 1 2 3 5

6 5 3 4 2 6

6 3 5 4 10 8 2 1 1

Sample Output NONE 6.00 24.00 NONE

import java.util.Scanner; classTrangle { inta[]; intn;

Trangle(int m,int b[]) { n=m; a=b;

}

void are()

{

int i,j,k,t,p; float mj=0; p=0;

for (i=0;i

t=a[i]*a[i]+a[j]*a[j];

for (k=0;k

&&a[i]*a[j]/2.0>mj)

{mj=a[i]*a[j]/2.0f;p++;}

}

} if(p==0) System.out.printf(\); else

System.out.printf(\,mj);

}

}

publicclass Mugun {

publicstaticvoid main(String args[]) { int n;

Scanner reader=new Scanner(System.in);

while(reader.hasNext()) { n=reader.nextInt(); int b[]; b=newint[n]; int i;

for (i=0;i

b[i]=reader.nextInt();

Trangle s;

s=newTrangle(n,b); s.are();

}

} }

F.圆类(作业3) Description

设计一个圆类,圆类中设置必要的数据成员,使之能够表示圆的大小和位置;类中设置必要的成员函数,能够求圆的面积和周长;设计一个成员函数,能够将圆从一个位置移动到另一个位置。 Input

多组数据。每组数据包括圆的半径和圆心坐标,以及移动的增量。半径是浮点数,圆心坐标和移动增量都是整型数。 Output

圆的面积和周长(精确到小数点后两位),及移动后的圆的位置(整型)。 Sample Input

10 10 10 15 15 20 20 30 16 16

Sample Output

area:314.16 perimeter:62.83 after moved:25 25

area:1256.64 perimeter:125.66 after moved:36 46

import java.util.Scanner;

class Circle { floatr; intx,y;

Circle (float a,int b,int c) {

r=a;

x=b; y=c;

}

float area() { returnr*r*3.1415926f;

}

float perimeter() { return 2.0f*r*3.1415926f; }

void move(int d1,int d2) { x=x+d1; y=y+d2;

}

int getx() { returnx; }

int gety() { returny; }

}

publicclass Main { publicstaticvoid main(String args[])

{

Scanner reader=new Scanner(System.in);

while(reader.hasNext()) { float a; int b,c,d,e;

a=reader.nextFloat(); b=reader.nextInt(); c=reader.nextInt(); d=reader.nextInt();

e=reader.nextInt();

Circle yuan;

yuan=new Circle(a,b,c);

yuan.move(d,e);

System.out.printf(\perimeter:%.2f after moved:%d %d\\n\,

yuan.area(),yuan.perimeter(),yuan.getx(),yuan.gety()); }

}

}

G.游戏I(作业4)

Description

有m个小孩围成一圈。第一个小孩从1开始报数,当报到n时的小孩出圈。下一个小孩又从1开始报数,当又报到n时又出圈。重复这个过程,当圈中只剩一个小孩时,这个小孩是原队列中的第几位? Input

有多组数据,每组数据由两个整型数组成。第1个整型数表示小孩的总数m,第2个整型数表示出圈小孩的报数n。小孩数不超过100个。 Output

圈中最后剩的一个小孩在开始的圈中是第几位? Sample Input 3 1 3 2 10 13

Sample Output 3 3 5

import java.util.Scanner; publicclass Youxi {

publicstaticvoid main(String args[]){

int m,n;

Scanner reader=new

Scanner(System.in);

while(reader.hasNextInt()){ m=reader.nextInt(); n=reader.nextInt(); Child.circle(m,n);

}

} }

class Child{

publicstaticvoid circle(int m,int n){ int i,l=0;

for(i=2;i<=m;i++){ l=(l+n)%i;

} l++;

System.out.println(l);

} }

H.游戏II(作业4) Description

10个小孩围成一圈分糖果,老师分给第一个小孩10块,第二个小孩2块,第三个小孩8块,第四个小孩22块,第五个小孩16块,第六个小孩4块,第七个小孩10块,第八个小孩6块,第九个小孩14块,第十个小孩20块。然后所有的小孩同时将手中的糖果分一半给左边的小孩;糖块为奇数的人可向老师要一块。问经过这样几次后,大家手中的糖的块数将一样多?每人各有多少块糖? Input

多组数据,每组数据10个数,分别为游戏开始时,每个小孩手中的糖果数。 Output

游戏结束后,游戏的次数及每人的中的糖果数。 Sample Input

10 2 8 22 16 4 10 6 14 20 Sample Output

17 18

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a[] = new int[10]; while(in.hasNext()){ int cnt = 1; for (int i = 0; i < 10; i++){ a[i] = in.nextInt(); } Game G = new Game(a); if(G.check() == 1) System.out.printf(\ else { while(true){ G.Play(); cnt++; if(G.check() == 1) break; } System.out.printf(\%d\\n\KID[0]); } } } }

class Game{ int KID[] = new int[10]; int temp[] = new int[10]; Game(int kid[]){ for(int i = 0; i < 10; i++){ KID[i] = kid[i]; temp[i] = KID[i]; } } int check(){ int flag = 1;

for (int i = 0; i < 9; i++){ if(KID[i] != KID[i+1]){ flag = 0; break; } } return flag; } void Play(){ for (int i = 0; i < 10; i++){ temp[i] /= 2; } for (int i = 0; i < 10; i++){ if(i != 9) { temp[i+1] += KID[i]/2; //temp[i] /= 2; } else { temp[0] += KID[9]/2; //temp[9] /= 2; } } for (int i = 0; i < 10; i++){ if(temp[i] % 2 == 1) temp[i]++; } for (int i = 0; i < 10; i++){ KID[i] = temp[i]; } //System.out.println(1); } }

I.魔方矩阵(作业5)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 139 (57 users) Total

Accepted: 61 (57 users) Special Judge: No

Description

有一个n*n矩阵,其各个元素的值由1到n*n自然数组成。将这n*n个自然数放到n*n矩阵中,使得矩阵的每一行元素之和、每一列元素之和、主对角线元素之和及副对角线元素之和都相等。n是奇数,最大不超过99。如下的矩阵就是一个魔方阵: 8 1 6 3 5 7 4 9 2

往魔方阵中放数的规则如下:

(1) 将1放在第0行中间一列;

(2) 从2开始直到n×n止各数依次按下列规则存放:

按 45°方向向右上行走(每一个数存放的行比前一个数的行数减1,列数加1)

(3) 如果行列范围超出矩阵范围,则回绕。

例如1在第0行,则2应放在最下一行,列数同样减1;

(4) 如果按上面规则确定的位置上已有数,或上一个数是第0行第n-1列时,则把下一个数放在上一个数的下面。 Input

若干组数据,每组数据由一个奇数组成。 Output

对于每一个奇数,输出一个魔方矩阵。

同一行中两个数用一个空格分隔,每

一行最后一个数后有一个空格。 Sample Input 3 7

Sample Output 8 1 6 3 5 7 4 9 2

30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; while(in.hasNext()){ n = in.nextInt(); Matrix M = new Matrix(n); M.InitMatrix(); M.MagicMatrix(); M.ShowMatrix(); } } }

class Matrix{ int N; int a[][]; Matrix(int n){ N = n; a = new int[N][N]; } void InitMatrix(){ for(int i = 0; i < N; i++){

for (int j = 0; j < N; j++){ a[i][j] = 0; } } } int check(){ for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ if(a[i][j] == 0) return 0; } } return 1; } int Magic_i(int i,int j){ while(a[i][j] != 0){ i++; if(i == N) i -= N; } return i; } int Change_i(int i){ i--; if(i == -1) i += N; return i; } int Change_j(int j){ j++; if(j == N) j -= N; return j; } void ShowMatrix(){ for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ System.out.printf(\\ } System.out.println(); } } void MagicMatrix(){ a[0][N/2] = 1; int i = 0;

int j = N/2; int cnt = 2; while(check() == 0){ if(i == 0 && j == N-1){ i++; a[i][j] = cnt++; } else {

if(a[Change_i(i)][Change_j(j)] != 0){ i = Magic_i(i,j); } else { i = Change_i(i); j = Change_j(j); } a[i][j] = cnt++; } } }

}

J.算式(作业5)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 171 (59 users) Total Accepted: 61 (59 uDescription

求出以下形式的算式,每个算式中有九个数位,正好用尽1到○○○+○○○=○○○ Input 无。 Output 如Script中所描述的算式,输出时按第1个3位数由小到大

Sample Input 无

Sample Output 124+659=783 125+739=864

?

386+541=927

Total of n Expressions.

(注:n是等式个数,输出时应输出实际个数) import java.util.Scanner; publicclass Main { publicstaticvoid main(String[] args) {

Scanner in = new Scanner(System.in);

Num N = new Num(); N.Play();

}

}

class Num{ intn = 0;

inta[] = newint[3]; intb[] = newint[3]; intc[] = newint[3]; void xsort(int s[]){ int t;

for (int i = 0; i < 3; i++){ for (int j = 0; j < 2-i; j++){

if(s[j] > s[j+1]){ t = s[j]; s[j] = s[j+1]; s[j+1] = t;

}

}

}

}

int check_1(int a,int b,int c){ if(a == b||b == c||a == c) return 0;

return 1; }

int check_2(int a[],int b[]){ for (int k = 0; k < 3; k++){ for (int p = 0; p < 3; p++){ if(a[k] == b[p])

return 0;

}

}

return 1;

}

int check_3(){ int N[] = newint[11];

for (int k = 0; k <= 10; k++){ N[k] = 0;

}

for (int k = 0; k < 3; k++){ N[a[k]]++; }

for (int k = 0; k < 3; k++){ N[b[k]]++; }

for (int k = 0; k < 3; k++){ N[c[k]]++; }

for (int k = 1; k <= 9; k++){ if(N[k] == 0) return 0; }

return 1;

}

void show(int a,int b,int c){ System.out.printf(\,a,b,c);

n++;

}

void show_last(){ System.out.printf(\of %d Expressions.\\n\,n);

}

void Play(){

if(check_1(a[0],a[1],a[2]) == 0) for (int i = 123; i <= 386; i++){

a[0] = i; a[1] = (i/10); a[2] = i/100;

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 142 (58 users) Total Accepted: 65 (58 uDescription

设计一个动物声音模拟器模仿动物的叫声。类图如下:

Animal类中的方法playSound模拟动物的叫声,但输出时要

continue;

xsort(a);

for (int j = i+1; j <= 987; j++){

b[0] = j; b[1] = (j/10);

b[2] = j/100;

if(check_1(b[0],b[1],b[2]) == 0) continue;

xsort(b);

if(check_2(a,b) == 0) continue;

int sum = i+j; if(sum > 999) continue;

c[0] = sum; c[1] = (sum/10);

c[2] = sum/100;

if(check_1(c[0],c[1],c[2]) == 0) continue;

xsort(c);

if(check_3() == 0) continue;

show(i,j,sum);

}

}

show_last();

}

}

K.动物声音模拟器(作业6)

程序中任何地方不允许直接用Dog或Cat的对象调用cr

getAnimalName()方法。(P143 4题)

Input

若干组数据,每组数据由1个整型数和1个字符串组成。整型猫、1——狗;字符串表示动物的名字。 Output 先输出动物的名字。如果是猫,则输出“Miao,Miao?”;如果是

Sample Input

1 Eunice 0 Elma

Sample Output

Eunice:Wang,Wang... Elma:Miao,Miao...

import java.util.Scanner; publicclass Main { publicstaticvoid main(String[] args) { // TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int m;

while(in.hasNext()){ m = in.nextInt(); String n = in.next(); Simulator S = new Simulator();

if(m == 0)

S.PlaySound(new

Cat(n));

else

S.PlaySound(new

Dog(n));

}

}

}

abstractclass Animal{ abstractvoid cry();

abstractvoid getAnimalName();

}

class Simulator{ Animal animal;

void PlaySound(Animal animal){ this.animal = animal; animal.getAnimalName(); animal.cry(); }

}

class Dog extends Animal{

String name; Dog(String n){ name = n;

}

void cry(){

System.out.println(\.\); }

void getAnimalName(){

System.out.printf(\,name); }

}

class Cat extends Animal{ String name; Cat(String n){ name = n;

}

void cry(){

System.out.println(\.\); }

void getAnimalName(){

System.out.printf(\,name); } }

L.计算三角形的面积和周长(作业6)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 102 (56 users) Total Accepted: 57 (55 uDescription

设计一个点类和一个三角形类。三角形的三个顶点用三个点类三角形的面积。如果三点位于同一直线,则输出“a line”。Input 有多组数据,每组数据由6个浮点数组成,分别表示3个点的

Output 三角形面积。如果3点共线,则输出“a line”。面积保留两

Sample Input

87 12 34 76 23 29 11 22 33 44 55 66 Sample Output 1597.50 a line

import java.util.Scanner;

import java.text.DecimalFormat; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()){ double a,b,c,d,e,f; a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); d = in.nextDouble(); e = in.nextDouble(); f = in.nextDouble(); Point A = new Point(a,b); Point B = new Point(c,d); Point C = new Point(e,f); Triangle T = new Triangle(A,B,C); T.GetS(); T.show(); } } }

class Point{ double x,y;

Point(){};

Point(double xx,double yy){ x = xx; y = yy; } }

class Triangle{ Point A; Point B; Point C; double l1,l2,l3,S; double distance(Point a,Point b){ return

Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } Triangle(Point a,Point b,Point c){ A = a; B = b; C = c; l1 = distance(A,B); l2 = distance(B,C); l3 = distance(A,C); } void GetS(){ S = Math.sqrt(((l1+l2+l3)/2)*(((l1+l2+l3)/2)-l1)*(((l1+l2+l3)/2)-l2)*(((l1+l2+l3)/2)-l3)); } void show(){ if(S == 0) System.out.printf(\line\\n\ else

System.out.printf(\ } }

M.异常处理(作业7)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 120 (60 users) Total Accepted: 63 (59 u

Description

if(l==0) returntrue; elsereturnfalse;

设计一个自然数类,该类的对象可以表示一个自然数,可以判断该数是否是素数。如果

}

所给的数为0或负数,则在构造自然数类的对象时进行异常处理并显示“not a nature!”;

}

如果所给的数是一个自然数,则正常构造自然数对象并给出是否是素数的结果。 Input

若干组数据,每组数据只有一个整型数。 Output

classnatureExceptionextends Exception {

String str;

public natureException(String str) 如果所给的数是自然数,则构造自然数类的对象,并判断该自然数是否是素数,如果是 { ”;如果所给的数不是自然数(0素数,则输出“a prime”,否则输出“not a prime

this.str=str; 或负整数),则构造自然数对象时进行异常处理,并输出“not a nature”。

Sample Input 10 -10 11 8374 Sample Output not a prime not a nature a prime not a prime

import java.util.Scanner; class nature{ intn;

public nature (int n) throws natureException{ if(n<=0)

thrownew natureException(\nature\); this.n=n; }

publicboolean sushu(){ int l=0;

for(int i=2;i

}

public String ExptMess() { returnstr; } }

publicclass Main{

publicstaticvoid main(String args[]) {

Scanner reader=new Scanner(System.in); int n;

nature n1;

while(reader.hasNext()) {

n1=null;

n=reader.nextInt(); try {

n1=new nature(n);

if(n1.sushu()) System.out.println(\prime\);

else System.out.println(\prime\); }

catch(natureException e) {

System.out.println(e.ExptMess()); }

}

} } }

} N.字符串(作业7,P220-5)

class Strings{

Time Limit: 1000 MS Memory Limit: 32768 K

String S;

Total Submit: 112 (60 users) Total Accepted: 68 (59 users) Special Judge: No Strings(String s){

S = s; Description

} 编写程序剔除一个字符串中全部非数字字符,例如,将形如“ab123you”的非数字字符 void play(){ 全部剔除,得到字符串“123”。

S = S.replaceAll(\

Input

}

有多组数据,每组数据由1个字符串组成,内有数字字符和非数字字符。 void show(){ Output System.out.printf(\

} 字符串中全部数字字符。

} Sample Input

}

ab123you 2 books

O.字符串二(作业8,P220-6)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 168 (55 users) Total Accepted: 56 (51 uDescription

total of 32 files Sample Output 123 2 32

给定字符串“数学87分,物理76分,英语96分”,编写程计算总成绩和平均成绩。 Input Output

学生的总成绩和平均成绩,保留一位小数。 Sample Input

math 80 English 90 math 68 Sample Output 238.0 79.3

有多组数据,每组数据由1个字符串组成,表示某一个学生的

import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub String s; Scanner in = new Scanner(System.in); while(in.hasNext()){ s = in.next(); Strings a = new Strings(s); a.play(); a.show();

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s;

import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()){ int a,b; a = in.nextInt(); b = in.nextInt(); Car C = new Car(a,b); C.Play(); } } }

class Car{ int l1,l2,t,v; Car(int l,int tt){ P.汽车速度(作业8)

l1 = l;

Time Limit: 1000 MS Memory Limit: 32768 K t = tt; Total Submit: 98 (50 users) Total Accepted: 52 (50 users) Special Judge: No }

int check(int n){ Description

int a[] = new int[105];

一辆以固定速度行驶的汽车,司机在上午10点看到里程表上的读数是一个对称数(如果

int cnt = 0;

一个数等于它的反序数,则称它为对称数。即这个数从左向右读和从右向左读是完全一

int f = 1;

样的),为95859。两小时后里程表上出现了一个新的对称数。问该车的速度是多少?新

while(n > 0){

while(in.hasNext()){

s = in.nextLine(); Grades G = new Grades(s); G.show(); } } }

class Grades{ double sum = 0,average; Grades(String s){ Scanner scanner = new Scanner(s); scanner.useDelimiter(\89.]+\ int num = 0; while(scanner.hasNext()){ double temp = scanner.nextDouble(); sum += temp; num++; } average = sum/num; } void show(){ System.out.printf(\%.1f\\n\m,average); } }

对称数是多少? Input Output

车的速度和新的里程数(对称数)。 Sample Input 95859 2 Sample Output 50 95959

有多组数据,每组数据由两个整型数组成,分别表示汽车的里

import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int y,m,d; Scanner in = new

System.out.printf(\

Scanner(System.in);

}

while(in.hasNext()){ y = in.nextInt();

Q.打鱼还是晒网(实验4-1) m = in.nextInt();

d = in.nextInt(); Time Limit: 1000 MS Memory Limit: 32768 K Dates Special Judge: No D = new Total Submit: 130 (52 users) Total Accepted: 58 (50 users) Dates(y,m,d);

Description D.show(); 中国有句俗语叫“三天打鱼两天晒网”。某人从 2000 年} 1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。 }

Input

} 有多组数据,每组数据由3个整型数组成,分别表示年、月和日。 class Dates{

Output

Calendar calendar =

}

a[cnt++] = n; n /= 10; } if(cnt % 2 == 0){ for (int i = 0; i < cnt/2; i++){ if(a[i] != a[cnt-i-1]){ f = 0; } } } else{ for (int i = 0; i < cnt/2; i++){ if(a[i] != a[cnt-i-1]){ f = 0; } } } return f; }

void Play(){ for (int i = 1; ; i++){ if(check(l1+i) == 1){ l2 = l1+i; break; } } v = (l2-l1)/t;

如果是打渔,输出“fish”;否则,输出“rest”。 Sample Input 2000 1 1 2000 1 5 2005 9 8 2010 12 3 Sample Output fish rest fish rest

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 a,b,c,d,f; while(in.hasNext()){ R.分数类(实验4-2)

a = in.nextInt(); Time Limit: 1000 MS Memory Limit: 32768 K b = in.nextInt();

Total Submit: 214 (53 users) Total Accepted: 52 (47 users) Special Judge: No c = in.nextInt(); Description d = in.nextInt();

a和 b表示(分数形式为f = in.nextInt(); a/b),能定义一个分数类,分数的分子和分母分别用整型数

point p = new 够实现分数的加法、减法、乘法和除法。

point(a,b,c,d); Input

if(f == 1){

有多组数据,每组数据由5个整型数组成,前4个数分别表示两个分数的分子和分母,

try{

第5个数表示运算类型,1、2、3、4分别对应加、减、乘、除。

p.add();

Output }

catch(NumException

两个分数加、减、乘和除的结果。如果分母为0或运算后分母为0,

e){ e.warnMess();

则输出“Divided by zero”。

} } Sample Input

else if(f == 2) { 1 2 3 4 4

try{ p.sub();

Calendar.getInstance();

long time1,time2; long f = 0,mod; //calendar.setTime(new Date()); Dates(int Y,int M,int D){ calendar.setTime(new Date()); calendar.set(2000,0,1); time1 = calendar.getTimeInMillis(); calendar.set(Y,M-1,D); time2 = calendar.getTimeInMillis(); mod = ((time2-time1)/(1000*60*60*24))%5; } void show(){ if(mod <= 2) System.out.println(\ else System.out.println(\ } }

1 2 3 0 1 1 2 0 4 4 1 2 -3 4 3 Sample Output (1/2)/(3/4)=2/3 Divided by zero Divided by zero (1/2)*(-3/4)=-3/8

} catch(NumException e){ e.warnMess(); } } else if(f == 3){ try{ p.mul(); } catch(NumException e){ e.warnMess(); } } else{ try{ p.div(); } catch(NumException e){ e.warnMess(); } } } } }

class NumException extends Exception{ void warnMess(){ System.out.println(\by zero\ } }

class point{ int a,b,c,d; int gcd(int a,int b) { if(b == 0) return a; else return gcd(b,a % b); } point(int aa,int bb,int cc,int dd){ a = aa;

b = bb; c = cc; d = dd; } void add()throws NumException{ if(b == 0 || d == 0) throw new NumException(); if(a*b > 0 && a < 0){ a = Math.abs(a); b = Math.abs(b); } if(c*d > 0 && c < 0){ c = Math.abs(c); d = Math.abs(d); } if(a*b < 0){ int s = a*b; b = Math.abs(b); a = s/b; } if(c*d < 0){ int s = c*d; d = Math.abs(d); c = s/d; } int temp1 = a*d+b*c; int temp2 = b*d; int g = gcd(temp1,temp2); temp1 /= g; temp2 /= g; if(temp1 == 0) System.out.printf(\\\n\

else if(temp1 % temp2 == 0) System.out.printf(\%d\\n\ else { if(temp1 * temp2 < 0){ int s = temp1 * temp2; temp2 = Math.abs(temp2); temp1 = s/temp2;

} System.out.printf(\d)=%d/%d\\n\ } } void sub()throws NumException{ if(b == 0 || d == 0) throw new NumException(); if(a*b > 0 && a < 0){ a = Math.abs(a); b = Math.abs(b); } if(c*d > 0 && c < 0){ c = Math.abs(c); d = Math.abs(d); } if(a*b < 0){ int s = a*b; b = Math.abs(b); a = s/b; } if(c*d < 0){ int s = c*d; d = Math.abs(d); c = s/d; } int temp1 = a*d-b*c; int temp2 = b*d; //int g = Math.abs(gcd(temp1,temp2)); int g = gcd(temp1,temp2); temp1 /= g; temp2 /= g; if(temp1 == 0) System.out.printf(\\\n\

else if(temp1 % temp2 == 0) System.out.printf(\d\\n\ else { if(temp1 * temp2 < 0){ int s = temp1 * temp2; temp2 = Math.abs(temp2);

temp1 = s/temp2; } System.out.printf(\d)=%d/%d\\n\ } } void mul() throws NumException{ if(b == 0 || d == 0) throw new NumException(); if(a*b > 0 && a < 0){ a = Math.abs(a); b = Math.abs(b); } if(c*d > 0 && c < 0){ c = Math.abs(c); d = Math.abs(d); } if(a*b < 0){ int s = a*b; b = Math.abs(b); a = s/b; } if(c*d < 0){ int s = c*d; d = Math.abs(d); c = s/d; } int temp1 = a*c; int temp2 = b*d; //int g = Math.abs(gcd(temp1,temp2)); int g = gcd(temp1,temp2); //System.out.println(g); temp1 /= g; temp2 /= g; if(temp1 == 0) System.out.printf(\\\n\

else if(temp1 % temp2 == 0) System.out.printf(\%d\\n\

else { if(temp1 * temp2 < 0){ int s = temp1 * temp2; temp2 = Math.abs(temp2); temp1 = s/temp2; } System.out.printf(\d)=%d/%d\\n\ } } void div()throws NumException{ if(b == 0 || d == 0 || c == 0) throw new NumException(); if(a*b > 0 && a < 0){ a = Math.abs(a); b = Math.abs(b); } if(c*d > 0 && c < 0){ c = Math.abs(c); d = Math.abs(d); } if(a*b < 0){ int s = a*b; b = Math.abs(b); a = s/b; } if(c*d < 0){ int s = c*d; d = Math.abs(d); c = s/d; } int temp1 = a*d; int temp2 = b*c; //int g = gcd(Math.abs(temp1),Math.abs(temp2)); int g = gcd(temp1,temp2); temp1 /= g; temp2 /= g; if(temp1 == 0) System.out.printf(\\\n\

else if(temp1 % temp2 == 0) System.out.printf(\d\\n\ else { if(temp1 * temp2 < 0){ int s = temp1 * temp2; temp2 = Math.abs(temp2); temp1 = s/temp2; } System.out.printf(\d)=%d/%d\\n\ } } }

S.卖西瓜(实验5-1)

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 88 (52 users) Total Accepted: 54 (52 usDescription

有个瓜农卖西瓜。他有m个西瓜,第1天卖一半多n个,个,问几天以后能卖完?例如:1020个西瓜,第1天卖一半多一半多2个,8天后卖完。 Input 多组数据。每组数据两个数:西瓜总数m,每天多卖的个

Output

卖完所需的天数。 Sample Input 1020 2 378 3 Sample Output 8 6

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in);

int m,n; while(in.hasNext()){ m = in.nextInt(); n = in.nextInt(); Watermelon w = new Watermelon(m,n); w.Play(); w.show(); } } }

class Watermelon{ int m,n,day; Watermelon(int mm,int nn){ m = mm; n = nn; day = 0; } void Play(){ int temp = m; while(temp > 0){ int t = temp/2+n; if(temp <= t){ day++; break; } temp /= 2; temp -= n; day++; } } void show(){ System.out.println(day); } }

T.二维数组运算(实验5-2)

Description

给定两个二维数组,对这两个数组执行相应的运算。 Input 有多组数据。

每组数据开始先是一个整型数,表示运算类型。1表示对两个示对两个数组进行减法运算,3表示对两个数组进行乘法运算示第1个二维数组的行数和列数,其后是每一个元素的值;再示第2个数组的行数和列数,其后是每一个元素的值。矩阵元Output

两个数组运行后的结果。如果两个数组不能执行相应的运算,

number of row or number of col not equal,can't b add! number of row or number of col not equal,can't b subed! number of row or number of col not equal,can't b muled!

Sample Input

1 2 2 1 2 3 4 2 2 5 6 7 8 2 2 3 1 2 3 4 5 6

Time Limit: 1000 MS Memory Limit: 32768 K

3 3

Total Submit: 197 (52 users) Total Accepted: 55 (50 users) Special Judge: No

c1 = in.nextInt(); M.Init_a(r1,c1);

4 5 6 r2 = in.nextInt();

c2 = in.nextInt();

7 8 9 M.Init_b(r2, c2);

if(f == 1){

3 try{

M.Add();

2 3

} catch(MatrixException

1 2 3

m){ m.warnAdd();

4 5 6

} } 3 3

if(f == 2){ try{ 1 2 3

M.Sub(); } 4 5 6

catch(MatrixException m){ 7 8 9

m.warnSub(); Sample Output

}

6 8

} if(f == 3){ 10 12

try{ M.Mul(); number of row or number of col not equal,can't b subed! } catch(MatrixException 30 36 42

m){

66 81 96 m.warnMul();

} import java.util.Scanner;

} public class Main {

} public static void main(String[] args) } {

int f,r1,c1,r2,c2;

} Scanner in = new

class MatrixException extends Scanner(System.in);

Exception{ while(in.hasNext()){

void warnAdd(){ f = in.nextInt();

System.out.println(\of Matrix M = new Matrix();

row or number of col not equal,can't b r1 = in.nextInt();

1 2 3

add!\ } void warnSub(){ System.out.println(\of row or number of col not equal,can't b subed!\ } void warnMul(){ System.out.println(\of row or number of col not equal,can't b muled!\ } }

class Matrix{ int row1,col1,row2,col2; int a[][] = new int[1005][1005]; int b[][] = new int[1005][1005]; int c[][] = new int[1005][1005]; Scanner in = new Scanner(System.in); void Init_a(int r ,int c){ row1 = r; col1 = c; for (int i = 0; i < row1; i++){ for (int j = 0; j < col1; j++){ a[i][j] = in.nextInt(); } } } void Init_b(int r ,int c){ row2 = r; col2 = c; for (int i = 0; i < row2; i++){ for (int j = 0; j < col2; j++){ b[i][j] = in.nextInt(); } } } void Add()throws MatrixException{ if(row1 != row2 || col1 != col2) throw new MatrixException(); for (int i = 0; i < row1; i++){ for (int j = 0; j < col1; j++){

a[i][j] += b[i][j]; } } for (int i = 0; i < row1; i++){ System.out.printf(\ for (int j = 1; j < col1; j++){ System.out.printf(\ } System.out.printf(\ } } void Sub()throws MatrixException{ if(row1 != row2 || col1 != col2) throw new MatrixException(); for (int i = 0; i < row1; i++){ for (int j = 0; j < col1; j++){ a[i][j] -= b[i][j]; } } for (int i = 0; i < row1; i++){ System.out.printf(\ for (int j = 1; j < col1; j++){ System.out.printf(\ } System.out.printf(\ } } void Mul()throws MatrixException{ if(col1 != row2) throw new MatrixException(); for (int i = 0; i < row1; i++){ for (int j = 0; j < col1; j++){ int temp = 0; for (int k = 0; k < col1; k++){ temp += a[i][k]*b[k][j];

BufferedReader bufferRead = new BufferedReader(in); String str = null; while((str = bufferRead.readLine())!= null){ //StringTokenizer fenxi = new StringTokenizer(str); //int count = fenxi.countTokens(); int i = 1; str = i++ + str; bufferWrite.write(str); bufferWrite.newLine(); } bufferWrite.close(); out.close(); U.随机文件读写(作业9 P333(1))

in = new

Time Limit: 1000 MS Memory Limit: 32768 K

FileReader(fWrite);

Total Submit: 139 (46 users) Total Accepted: 43 (38 users) Special Judge: No bufferRead = new

BufferedReader(in); Description

String s = null; 使用RandomAccessFile将一个文本文件倒置读出。 while((s =

Input

bufferRead.readLine())!=null){

自定义文本文件。 System.out.println(s); Output }

bufferRead.close(); 将文本文件倒序读出。

in.close(); import java.io.*;

} import java.util.*;

catch(Exception e){} public class Main{

} public static void main(String

} args[]){

}

} c[i][j] = temp; } } for (int i = 0; i < row1; i++){

System.out.printf(\ for (int j = 1; j < col2; j++){

System.out.printf(\ } System.out.printf(\ } }

File fRead = new File (\ File fWrite = new File(\ try{ Writer out = new FileWriter(fWrite); BufferedWriter

bufferWrite = new BufferedWriter(out); Reader in = new FileReader(fRead);

V.读写文本文件(作业9 P333(2))

Time Limit: 1000 MS Memory Limit: 32768 K

Total Submit: 95 (46 users) Total Accepted: 44 (40 usDescription

用Java的输入、输出流将一个文本文件的内容按行读出行号,并写入到另一个文件中。 Input

自定义文本文件。 Output

按Description所述形成一个新文件。

import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { RandomAccessFile inAndOut = null; int data[] = {1,2,3,4,5,6,7,8,9,10}; try{ inAndOut = new RandomAccessFile(\ for (int i = 0; i < data.length; i++){ inAndOut.writeInt(data[i]); } for (int i = data.length-1; i >= 0; i--){ inAndOut.seek(i*4); System.out.printf(\readInt()); } inAndOut.close(); } catch(Exception e){} } }

本文来源:https://www.bwwdw.com/article/qurt.html

Top