Java经典算法Word格式.docx
- 文档编号:18365079
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:41
- 大小:33.06KB
Java经典算法Word格式.docx
《Java经典算法Word格式.docx》由会员分享,可在线阅读,更多相关《Java经典算法Word格式.docx(41页珍藏版)》请在冰豆网上搜索。
则表明此数不是素数,反之是素数。
42:
43:
44:
45:
46:
for(i=2;
=200;
47:
if(mymath.iszhishu(i)==true)
48:
System.out.println(i);
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
publicbooleaniszhishu(intx)
61:
62:
for(inti=2;
=x/2;
63:
if(x%2==0)
64:
returnfalse;
65:
returntrue;
66:
67:
68:
69:
【程序3】题目:
打印出所有的"
水仙花数"
,所谓"
是指一个三位数,其各位数字立方和等于该数本身。
例如:
153是一个"
,因为153=1的三次方+5的三次方+3的三次方。
70:
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
71:
72:
73:
74:
75:
for(i=100;
=999;
76:
if(mymath.shuixianhua(i)==true)
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
publicbooleanshuixianhua(intx)
97:
98:
inti=0,j=0,k=0;
99:
i=x/100;
100:
j=(x%100)/10;
101:
k=x%10;
102:
if(x==i*i*i+j*j*j+k*k*k)
103:
104:
105:
106:
107:
108:
109:
【程序4】题目:
将一个正整数分解质因数。
输入90,打印出90=2*3*3*5。
110:
程序分析:
对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
111:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
112:
(2)如果n<
>
k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你,重复执行第一步。
113:
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
114:
115:
publicexp2(){}
116:
publicvoidfengjie(intn){
117:
=n/2;
i++){
118:
if(n%i==0){
119:
System.out.print(i+"
*"
);
120:
fengjie(n/i);
//递归
121:
122:
123:
System.out.print(n);
124:
System.exit(0);
///不能少这句,否则结果会出错
125:
126:
publicstaticvoidmain(String[]args){
127:
Stringstr="
"
;
128:
exp2c=newexp2();
129:
str=javax.swing.JOptionPane.showInputDialog("
请输入N的值(输入exit退出):
130:
intN;
131:
N=0;
132:
try{
133:
N=Integer.parseInt(str);
134:
}catch(NumberFormatExceptione){
135:
e.printStackTrace();
136:
137:
System.out.print(N+"
分解质因数:
+N+"
="
138:
c.fengjie(N);
139:
}
140:
141:
【程序5】题目:
利用条件运算符的嵌套来完成此题:
学习成绩>
=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
142:
(a>
b)?
a:
b这是条件运算符的基本例子。
143:
importjavax.swing.*;
144:
publicclassex5{
145:
146:
147:
str=JOptionPane.showInputDialog("
148:
149:
150:
151:
152:
153:
catch(NumberFormatExceptione){
154:
155:
156:
str=(N>
90?
A"
:
(N>
60?
B"
C"
));
157:
System.out.println(str);
158:
159:
160:
【程序6】题目:
输入两个正整数m和n,求其最大公约数和最小公倍数。
161:
利用辗除法。
162:
最大公约数:
//这个列子中没有判断N>
M的情况如果N>
M交换NM的值
163:
publicclassCommonDivisor{
164:
publicstaticvoidmain(Stringargs[])
165:
166:
commonDivisor(24,32);
167:
168:
staticintcommonDivisor(intM,intN)
169:
170:
if(N<
0||M<
0)
171:
172:
System.out.println("
ERROR!
173:
return-1;
174:
175:
if(N==0)
176:
177:
thebiggestcommondivisoris:
+M);
178:
returnM;
179:
180:
returncommonDivisor(N,M%N);
181:
182:
183:
最小公倍数和最大公约数:
184:
importjava.util.Scanner;
185:
publicclassCandC
186:
{
187:
//下面的方法是求出最大公约数
188:
publicstaticint**(intm,intn)//没有比较n和m的大小,//需不需要呢?
?
189:
190:
while(true)//用while循环来进行判断
191:
192:
if((m=m%n)==0)//%的优先级高于=
193:
returnn;
194:
if((n=n%m)==0)
195:
returnm;
196:
197:
198:
publicstaticvoidmain(Stringargs[])throwsException
199:
200:
//取得输入值
201:
//Scannerchin=newScanner(System.in);
202:
//inta=chin.nextInt(),b=chin.nextInt();
203:
inta=23;
intb=32;
204:
intc=**(a,b);
205:
最小公倍数:
+a*b/c+"
\n最大公约数:
+c);
//最小公倍数a*b/c(其中c为最大公约数)
206:
207:
208:
【程序7】题目:
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
209:
利用while语句,条件为输入的字符不为'
\n'
.
210:
211:
publicclassex7{
212:
213:
214:
请输入字符串:
215:
Scannerscan=newScanner(System.in);
216:
Stringstr=scan.next();
217:
StringE1="
[\u4e00-\u9fa5]"
218:
StringE2="
[a-zA-Z]"
219:
intcountH=0;
220:
intcountE=0;
221:
char[]arrChar=str.toCharArray();
222:
String[]arrStr=newString[arrChar.length];
223:
for(inti=0;
arrChar.length;
i++)
224:
225:
arrStr[i]=String.valueOf(arrChar[i]);
226:
227:
for(Stringi:
arrStr)
228:
229:
if(i.matches(E1))
230:
231:
countH++;
232:
233:
if(i.matches(E2))
234:
235:
countE++;
236:
237:
238:
汉字的个数"
+countH);
239:
字母的个数"
+countE);
240:
241:
242:
【程序8】题目:
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
243:
关键是计算出每一项的值。
244:
importjava.io.*;
245:
publicclassSumloop{
246:
publicstaticvoidmain(String[]args)throwsIOException
247:
248:
ints=0;
249:
Stringoutput="
250:
BufferedReaderstadin=newBufferedReader(newInputStreamReader(System.in));
251:
请输入a的值"
252:
Stringinput=stadin.readLine();
253:
for(inti=1;
=Integer.parseInt(input);
254:
255:
output+=input;
256:
inta=Integer.parseInt(output);
257:
s+=a;
258:
259:
System.out.println(s);
260:
261:
262:
另解:
263:
264:
265:
266:
267:
268:
intn;
269:
intt=0;
270:
271:
Stringinput=stadin.readLine();
272:
n=Integer.parseInt(input);
273:
for(inti=1;
=n;
274:
t=t*10+n;
275:
s=s+t;
276:
System.out.println(t);
277:
278:
279:
280:
281:
【程序9】题目:
一个数如果恰好等于它的因子之和,这个数就称为"
完数"
。
例如6=1+2+3.编程找出1000以内的所有完数。
282:
publicclassWanshu{
283:
publicstaticvoidmain(String[]args)
284:
285:
ints;
286:
=1000;
287:
288:
s=0;
289:
for(intj=1;
j<
i;
j++)
290:
if(i%j==0)
291:
s=s+j;
292:
if(s==i)
293:
"
294:
295:
System.out.println();
296:
297:
298:
【程序10】题目:
一球从100米高度自由落下,每次落地后反跳回原高度的一半;
再落下,求它在第10次落地时,共经过多少米?
第10次反弹多高?
299:
publicclassEx10{
300:
301:
302:
doubles=0;
303:
doublet=100;
304:
=10;
305:
306:
s+=t;
307:
t=t/2;
308:
309:
310:
311:
312:
313:
314:
【程序11】题目:
有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?
都是多少?
315:
可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
316:
317:
318:
319:
320:
intj=0;
321:
intk=0;
322:
323:
=4;
324:
for(j=1;
325:
for(k=1;
k<
k++)
326:
if(i!
=j&
&
j!
=k&
i!
=k)
327:
{t+=1;
328:
System.out.println(i*100+j*10+k);
329:
330:
System.out.println(t);
331:
332:
333:
【程序12】题目:
企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;
40万到60万之间时高于40万元的部分,可提成3%;
60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
334:
请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
335:
importjava.util.*;
336:
publicclasstest{
337:
publicstaticvoidmain(String[]args){
338:
doublesum;
//声明要储存的变量应发的奖金
339:
Scannerinput=newScanner(System.in);
//导入扫描器
340:
System.out.print("
输入当月利润"
341:
doublelirun=input.nextDouble();
//从控制台录入利润
342:
if(lirun<
=100000){
343:
sum=lirun*0.1;
344:
}elseif(lirun<
=200000){
345:
sum=10000+lirun*0.075;
346:
=400000){
347:
sum=17500+lirun*0.05;
348:
=600000){
349:
sum=lirun*0.03;
350:
=1000000){
351:
sum=lirun*0.015;
352:
}else{
353:
sum=lirun*0.01;
354:
355:
应发的奖金是"
+sum);
356:
357:
358:
后面其他情况的代码可以由读者自行完善.
359:
360:
【程序13】
361:
题目:
一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
362:
在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
请看具体分析:
363:
364:
365:
longk=0;
366:
=100000l;
367:
if(Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100)&
Math.floor(Math.sqrt(k+168))==Math.sqrt(k+168))
368:
System.out.println(k);
369:
370:
371:
【程序14】题目:
输入某年某月某日,判断这一天是这一年的第几天?
372:
以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
373:
importjava.util.*;
374:
375:
376:
intday=0;
377:
intmonth=0;
378:
intyear=0;
379:
intsum=0;
380:
intleap;
381:
System.out.print("
请输入年,月,日\n"
382:
Scannerinput=newScanner(System.in);
383:
year=input.nextInt();
384:
month=input.nex
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 经典 算法