JAVA经典算法50题Word格式文档下载.docx
《JAVA经典算法50题Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《JAVA经典算法50题Word格式文档下载.docx(54页珍藏版)》请在冰豆网上搜索。
=200;
i++)//101-200的数
{
for(j=2;
j<
=(int)Math.sqrt(i);
j++)
if(i%j==0)
break;
}
if(j>
(int)Math.sqrt(i))
count++;
System.out.println(i);
System.out.println("
从101到200间有"
+count+"
个素数。
"
);
}(2,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,101,103,105,107,109,111,113,115,117,119,121,123,125,127,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199)
【程序3】
打印出所有的"
水仙花数"
,所谓"
是指一个三位数,其各位数字立方和等于该数本身。
例如:
153是一个"
,因为153=1的三次方+5的三次方+3的三次方。
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
(结果:
153、370、371、407)
for(i=100;
=999;
if(mymath.shuixianhua(i)==true)
System.out.println(i);
publicbooleanshuixianhua(intx)
inti=0,j=0,k=0;
i=x/100;
j=(x%100)/10;
k=x%10;
if(x==i*i*i+j*j*j+k*k*k)
returntrue;
else
returnfalse;
【程序4】
将一个正整数分解质因数。
输入90,打印出90=2*3*3*5。
程序分析:
对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<
>
k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
publicexp2(){}
publicvoidfengjie(intn){
for(inti=2;
=n/2;
i++){
if(n%i==0){
System.out.print(i+"
*"
fengjie(n/i);
System.out.print(n);
System.exit(0);
///不能少这句,否则结果会出错
publicstaticvoidmain(String[]args){
Stringstr="
;
exp2c=newexp2();
str=javax.swing.JOptionPane.showInputDialog("
请输入N的值(输入exit退出):
intN;
N=0;
try{N=Integer.parseInt(str);
catch(NumberFormatExceptione){e.printStackTrace();
System.out.print(N+"
分解质因数:
+N+"
="
c.fengjie(N);
【程序5】
利用条件运算符的嵌套来完成此题:
学习成绩>
=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
(a>
b)?
a:
b这是条件运算符的基本例子。
importjavax.swing.*;
publicclassex5{
str=JOptionPane.showInputDialog("
try{
N=Integer.parseInt(str);
catch(NumberFormatExceptione){
e.printStackTrace();
str=(N>
90?
A"
:
(N>
60?
B"
C"
));
System.out.println(str);
【程序6】
输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:
利用辗除法。
/*
*在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,
*取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,
*此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
*/
最大公约数:
publicclassCommonDivisor{
publicstaticvoidmain(Stringargs[])
commonDivisor(24,32);
//最小公倍数:
96最大公约数:
8
staticintcommonDivisor(intM,intN)
if(N<
0||M<
0)
System.out.println("
ERROR!
return-1;
if(N==0)
thebiggestcommondivisoris:
+M);
returnM;
returncommonDivisor(N,M%N);
最小公倍数和最大公约数:
importjava.util.Scanner;
publicclassCandC
{//下面的方法是求出最大公约数
publicstaticintgcd(intm,intn)
while(true)
if((m=m%n)==0)
returnn;
if((n=n%m)==0)
returnm;
}
publicstaticvoidmain(Stringargs[])throwsException
//取得输入值
//Scannerchin=newScanner(System.in);
//inta=chin.nextInt(),b=chin.nextInt();
inta=23;
intb=32;
intc=gcd(a,b);
System.out.println("
最小公倍数:
+a*b/c+"
\n最大公约数:
+c);
//最小公倍数:
736,最大公约数:
1
//importjavax.swing.*;
publicclassgs{
publicstaticvoidmain(Stringargs[])throwsException
inta,b,m,n,temp;
m=0;
n=0;
System.out.println("
Pleaseinputtwonumbers:
//Stringstr=JOptionPane.showInputDialog("
请输入一个数:
//Stringstr1=JOptionPane.showInputDialog("
请输入另一个数:
//m=Integer.parseInt(str);
//n=Integer.parseInt(str1);
Scannerscanner=newScanner(System.in);
m=scanner.nextInt();
n=scanner.nextInt();
if(m<
n)
temp=m;
m=n;
n=temp;
a=m;
b=n;
while(b!
=0)
temp=a%b;
a=b;
b=temp;
gongyueshu:
+a);
gongbeishu:
+n*m/a);
【程序7】
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
利用while语句,条件为输入的字符不为'
\n'
.
publicclassex7{
请输入字符串:
Scannerscan=newScanner(System.in);
Stringstr=scan.next();
StringE1="
[\u4e00-\u9fa5]"
StringE2="
[a-zA-Z]"
intcountH=0;
intcountE=0;
char[]arrChar=str.toCharArray();
String[]arrStr=newString[arrChar.length];
for(inti=0;
arrChar.length;
i++)
arrStr[i]=String.valueOf(arrChar[i]);
for(Stringi:
arrStr)
if(i.matches(E1))
countH++;
if(i.matches(E2))
countE++;
汉字的个数"
+countH);
字母的个数"
+countE);
更好的解法:
importjava.util.*;
publicclasslianxi07{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
intabcCount=0;
//英文字母个数
intspaceCount=0;
//空格键个数
intnumCount=0;
//数字个数
intotherCount=0;
//其他字符个数
Scannerscan=newScanner(System.in);
Stringstr=scan.nextLine();
char[]ch=str.toCharArray();
for(inti=0;
ch.length;
if(Character.isLetter(ch[i])){
//判断是否字母
abcCount++;
}
elseif(Character.isDigit(ch[i])){
//判断是否数字
numCount++;
elseif(Character.isSpaceChar(ch[i])){
//判断是否空格键
spaceCount++;
else{
//以上都不是则认为是其他字符
otherCount++;
字母个数:
+abcCount);
数字个数:
+numCount);
空格个数:
+spaceCount);
其他字符个数:
+otherCount);
}
【程序8】
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
关键是计算出每一项的值。
importjava.io.*;
publicclassSumloop{
publicstaticvoidmain(String[]args)throwsIOException
ints=0;
Stringoutput="
BufferedReaderstadin=newBufferedReader(newInputStreamReader(System.in));
请输入a的值"
Stringinput=stadin.readLine();
for(inti=1;
=Integer.parseInt(input);
output+=input;
inta=Integer.parseInt(output);
s+=a;
}er
System.out.println(s);
另解:
intn;
intt=0;
Stringinput=stadin.readLine();
n=Integer.parseInt(input);
for(inti=1;
=n;
t=t*10+n;
s=s+t;
System.out.println(t);
【程序9】
一个数如果恰好等于它的因子之和,这个数就称为"
完数"
。
例如6=1+2+3.编程
找出1000以内的所有完数。
publicclassWanshu{//6,28,496
publicstaticvoidmain(String[]args)
ints;
=1000;
s=0;
for(intj=1;
j<
i;
j++)
if(i%j==0)
s=s+j;
if(s==i)
System.out.print(i+"
"
System.out.println();
【程序10】题目:
一球从100米高度自由落下,每次落地后反跳回原高度的一半;
再落下,求它在
第10次落地时,共经过多少米?
第10次反弹多高?
(199.8046875,0.09765625)
publicclassEx10{
doubles=0;
doublet=100;
=10;
s+=t;
t=t/2;
【程序11】
有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?
都是多少?
(123,124,132,134,142,143,213,214,231,234,241,243,312,314,321,324,341,342,412,413,421,423,431,432。
24个)1.程序分析:
可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去
掉不满足条件的排列。
publicclassWanshu{
inti=0;
intj=0;
intk=0;
for(i=1;
=4;
for(j=1;
for(k=1;
k<
k++)
if(i!
=j&
&
j!
=k&
i!
=k)
{t+=1;
System.out.println(i*100+j*10+k);
System.out.println(t);
【程序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,求应发放奖金总数?
请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
importjava.util.*;
publicclasstest{
publicstaticvoidmain(String[]args){
doublesum;
//声明要储存的变量应发的奖金
Scannerinput=newScanner(System.in);
//导入扫描器
System.out.print("
输入当月利润"
doublelirun=input.nextDouble();
//从控制台录入利润
if(lirun<
=100000){
sum=lirun*0.1;
}elseif(lirun<
=200000){
sum=10000+lirun*0.075;
=400000){
sum=17500+lirun*0.05;
=600000){
sum=lirun*0.03;
=1000000){
sum=lirun*0.015;
}else{
sum=lirun*0.01;
应发的奖金是"
+sum);
后面其他情况的代码可以由读者自行完善.
【程序13】
一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
请看具体分析:
(156)
longk=0;
for(k=1;
=100000l;
if(Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100)&
Math.floor(Math.