admin 管理员组

文章数量: 1087139


2024年3月12日发(作者:eniac采用电子管和继电器吗)

精品文档

java集合编程练习题及答案

题目:古典问题:有一对兔子,从出生后第3个月起每

个月都生一对兔子,小兔子长到第四个月后每个月又生一对

兔子,假如兔子都不死,问每个月的兔子总数为多少?

1.程序分析:兔子的规律为数列

1,1,2,3,5,8,

public class exp2{

public static void main{

int i=0;

for

n);

}

public static int f

{

if

return 1;

else

return f+f;

}

}

public class exp2{

2016

1 / 23

精品文档

public static void main{

int i=0;

math mymath = new math;

for

n);

}

}

class math

{

public int f

{

if

return 1;

else

return f+f;

}

}

题目:判断101-200之间有多少个素数,并输出所

有素数。

1.程序分析:判断素数的方法:用一个数分别去除2

到sqrt,如果能被整除,

则表明此数不是素数,反之是素数。

2016

2 / 23

精品文档

public class exp2{

public static void main{

int i=0;

math mymath = new math;

for

if==true)

n;

}

}

class math

{

public int f

{

if

return 1;

else

return f+f;

}

public boolean iszhishu

{

for

}if return false; return true; }

2016

3 / 23

精品文档

题目:打印出所有的 “水仙花数 “,所谓 “水仙

花数 “是指一个三位数,其各位数字立方和等于该数本身。

例如:153是一个 “水仙花数 “,因为153=1的三次方+5

的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个

数分解出个位,十位,百位。

public class exp2{

public static void main{

int i=0;

math mymath = new math;

for

if==true)

n;

}

}

class math

{

public int f

{

if

return 1;

else

2016

4 / 23

精品文档

return f+f;

}

public boolean iszhishu

{

for

if

return false;

return true;

}

public boolean shuixianhua

{

int i=0,j=0,k=0;

i=x / 100;

j= /10;

k=x % 10;

if

return true;

else

return false;

}

}

题目:将一个正整数分解质因数。例如:输入

2016

90,打

5 / 23

精品文档

印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小

的质数k,然后按下述步骤完成:

如果这个质数恰等于n,则说明分解质因数的过程已

经结束,打印出即可。

如果n k,但n能被k整除,则应打印出k的值,并

用n除以k的商,作为新的正整数你,重复执行第一步。

如果n不能被k整除,则用k+1作为k的值,重复执

行第一步。

public class exp2{

public exp2{}

public void fengjie{

for{

if{

;

fengjie;

}

}

;

;///不能少这句,否则结果会出错

}

public static void main{

2016

6 / 23

精品文档

String str=““;

expc=new exp2;

str=putDialog;

int N;

N=0;

try{

N=nt;

}catch{

tackTrace;

}

;

e;

}

}

题目:利用条件运算符的嵌套来完成此题:学习成绩>

=90分的同学用A表示,60-89分之间的用B表示,60分以

下的用C表示。

1.程序分析:?a:b这是条件运算符的基本例子。

import .*;

public class ex{

public static void main{

String str=““;

2016

7 / 23

精品文档

str=putDialog;

int N;

N=0;

try{

N=nt;

}

catch{

tackTrace;

}

str=);

n;

}

}

题目:输入两个正整数m和n,求其最大公约数和最

小公倍数。

1.程序分析:利用辗除法。

最大公约数:

public class CommonDivisor{

public static void main

{

commonDivisor;

}

2016

8 / 23

精品文档

static int commonDivisor

{

if

{

n;

return -1;

}

if

{

n;

return M;

}

return commonDivisor;

}

}

最小公倍数和最大公约数:

import r;

public class CandC

{

//下面的方法是求出最大公约数

public static int gcd

{

2016

9 / 23

精品文档

while

{

if == 0)

return n;

if == 0)

return m;

}

}

public static void main throws Exception

{

//取得输入值

//Scanner chin = new Scanner; //int a

t, b = t;

int a=23; int b=32;

int c = gcd;

n;

}

}

经典算法

public static int gongyue {

while {

int temp = m % n;

2016

10 / 23

=

精品文档

m = n;

n = temp;

}

return n;

}

//求m和n的最小公倍数

public static int gongbei {

return m * n / gongyue;

}

}

题目:输入一行字符,分别统计出其中英文字母、

空格、数字和其它字符的个数。

1.程序分析:利用while语句,条件为输入的字符不

为 ‘n ‘.

import r;

public class ex{

public static void main

{n;Scanner scan=new Scanner;String

str=;String E1=“[u4e00-u9fa5]”;String

E2=“[a-zA-Z]”;int countH=0;int countE=0;char[]

arrStr=new arrChar=Array;String[]

String[];for

2016

11 / 23

精品文档

{ arrStr[i]=f;}for { if ) { countH++; }

if )

{ countE++; }}n;

n; } }

题目:求s=a+aa+aaa+a的值,其中a是

一个数字。例如2+22+222+2222+22222,几个数相加有键盘

控制。

1.程序分析:关键是计算出每一项的值。

import .*;

public class Sumloop {

public static void main throws IOException

{

int s=0;String output=““;BufferedReader

stadin = new

input BufferedReader);n;String

=ne;for;i++)

Java编程题集合

题目:有n个整数,使其前面各数顺序向后移m个位

置,最后m个数变成最前面的m个数

import r;

public class lianxi3{

2016

12 / 23


本文标签: 分析 题目 程序 利用 分解