数据结构经典算法文档格式.docx
《数据结构经典算法文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构经典算法文档格式.docx(28页珍藏版)》请在冰豆网上搜索。
else
returnf(x-1)+f(x-2);
或
mathmymath=newmath();
System.out.println(mymath.f(i));
classmath
publicintf(intx)
【程序2】题目:
判断101-200之间有多少个素数,并输出所有素数。
判断素数的方法:
用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
for(i=2;
=200;
if(mymath.iszhishu(i)==true)
System.out.println(i);
publicbooleaniszhishu(intx)
for(inti=2;
=x/2;
if(x%2==0)
returnfalse;
returntrue;
【程序3】题目:
打印出所有的"
水仙花数"
,所谓"
是指一个三位数,其各位数字立方和等于该数本身。
例如:
153是一个"
,因为153=1的三次方+5的三次方+3的三次方。
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
for(i=100;
=999;
if(mymath.shuixianhua(i)==true)
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)
【程序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){
=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("
catch(NumberFormatExceptione){
str=(N>
90?
A"
:
(N>
60?
B"
C"
));
System.out.println(str);
【程序6】题目:
输入两个正整数m和n,求其最大公约数和最小公倍数。
利用辗除法。
最大公约数:
publicclassCommonDivisor{
publicstaticvoidmain(Stringargs[])
commonDivisor(24,32);
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);
最小公倍数:
+a*b/c+"
\n最大公约数:
+c);
【程序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);
【程序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;
System.out