首页 JAVA经典算法题

JAVA经典算法题

举报
开通vip

JAVA经典算法题JAVA经典算法题 【程序1】 题目,古典题题,有一题子~出生后第兔从3月起每月都生个个一题子~小子题到第四月后每月又生一题子~假如子都不死~题每兔兔个个兔兔个 月的子题题多少,兔数 1.程序分析, 兔数子的题律题列1,1,2,3,5,8,13,21.... public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i <=20;i++) System.out.println(f(i)); } publi...

JAVA经典算法题
JAVA经典算法题 【程序1】 题目,古典题题,有一题子~出生后第兔从3月起每月都生个个一题子~小子题到第四月后每月又生一题子~假如子都不死~题每兔兔个个兔兔个 月的子题题多少,兔数 1.程序分析, 兔数子的题律题列1,1,2,3,5,8,13,21.... public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i <=20;i++) System.out.println(f(i)); } public static int f(int x) { if(x==1 || x==2) return 1; else return f(x-1)+f(x-2); } } 或 public class exp2{ public static void main(String args[]){ int i=0; math mymath = new math(); for(i=1;i <=20;i++) System.out.println(mymath.f(i)); } } class math { public int f(int x) { if(x==1 || x==2) return 1; else return f(x-1)+f(x-2); } } 【程序2】 题目,判断101-200之题有多少素~题出所有素。个数并数 1.程序分析,判素的方法,用一分题去除断数个数2到sqrt(题个数)~如果能被整除~ 题 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 明此不是素~反之是素。数数数 public class exp2{ public static void main(String args[]){ int i=0; exp2 mymath = new exp2(); for(i=101;i <=200;i += 2) if(mymath.iszhishu(i)==true) System.out.println(i); } public boolean iszhishu(int x) { for(int i=2;i <=x/2;i++) if (x % i==0 ) return false; return true; } } 【程序3】 题目,打印出所有的 "水仙花数 "~所题 "水仙花数 "是指一三个位~其各位字立方和等于题本身。例如,数数数153是一个 "水仙花数 "~因题153=1的三次方,5的三次方,3的三次方。 1.程序分析,利用for循题控制100-999~每分解出位~十位~百位。个数个数个 public class exp2{ public static void main(String args[]){ int i=0; math mymath = new math(); for(i=100;i <=999;i++) if(mymath.shuixianhua(i)==true) System.out.println(i); } } class math { public int f(int x) { if(x==1 || x==2) return 1; else return f(x-1)+f(x-2); } public boolean iszhishu(int x) { for(int i=2;i <=x/2;i++) if (x % 2==0 ) return false; return true; } public boolean shuixianhua(int x) { int i=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) return true; else return false; } } 【程序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的题,重题题行第一步。 public class exp2{ public exp2(){} public void fengjie(int n){ for(int i=2;i <=n/2;i++){ if(n%i==0){ System.out.print(i+ "* "); fengjie(n/i); } } System.out.print(n); System.exit(0);///不能少题句~否题题果出题会 } public static void main(String[] args){ String str= " "; exp2 c=new exp2(); str=javax.swing.JOptionPane.showInputDialog( "题题入N的题;题 入exit退出,, "); int N; N=0; try{ N=Integer.parseInt(str); }catch(NumberFormatException e){ e.printStackTrace(); } System.out.print(N+ "分解题因,数 "+N+ "= "); c.fengjie(N); } } 【程序5】 题目,利用件算符的嵌套完成此题,题成题条运来学> =90分的同用学A 表示~60-89分之题的用B表示~60分以下的用C表示。 1.程序分析,(a> b)?a:b题是件算符的基本例子。条运 import javax.swing.*; public class ex5 { public static void main(String[] args){ String str= " "; str=JOptionPane.showInputDialog( "题题入N的题;题入exit退出,, "); int N; N=0; try{ N=Integer.parseInt(str); } catch(NumberFormatException e){ e.printStackTrace(); } str=(N> 90? "A ":(N> 60? "B ": "C ")); System.out.println(str); } } 【程序6】 题目,题入正整两个数m和n~求其最大公题和最小公倍。数数 1.程序分析,利用题除法。 最大公题,数 public class CommonDivisor{ public static void main(String args[]) { commonDivisor(24,32); } static int commonDivisor(int M, int N) { if(N <0||M <0) { System.out.println( "ERROR! "); return -1; } if(N==0) { System.out.println( "the biggest common divisor is : "+M); return M; } return commonDivisor(N,M%N); } } _______________________________________________________________________ ___________________________ 更新版, public class exp2 { exp2() { } public int commonDivisor(int M, int N) { if (N < 0 || M < 0) { System.out.println("ERROR! "); return -1; } if (N == 0) { System.out .println("the biggest common divisor is : " + M); return M; } return commonDivisor(N, M % N); } public static void main(String args[]) { String str = " "; String str2 = " "; exp2 c = new exp2(); str = javax.swing.JOptionPane.showInputDialog("题题入M的题;题入exit退出,, "); str2 = javax.swing.JOptionPane.showInputDialog("题题入N的题;题入exit退出,, "); int N, M = 0; N = 0; try { N = Integer.parseInt(str); M = Integer.parseInt(str2); } catch (NumberFormatException e) { e.printStackTrace(); } System.out.print("最大公题, 数" + M ); c.commonDivisor(M, N); } } ————————————————————————————————————————————————— 最小公倍和最大公题,数数 import java.util.Scanner; public class CandC { //下面的方法是求出最大公题数 public static int gcd(int m, int n) { while (true) { if ((m = m % n) == 0) return n; if ((n = n % m) == 0) return m; } } public static void main(String args[]) throws Exception { //取得题入题 //Scanner chin = new Scanner(System.in); //int a = chin.nextInt(), b = chin.nextInt(); int a=23; int b=32; int c = gcd(a, b); System.out.println( "最小公倍,数 " + a * b / c + "\n最大公题,数 " + c); } } 【程序7】 题目,题入一行字符~分题题题出其中英文字母、空格、字和其字符的数它个数。 1.程序分析,利用while题句,件题题入的字符不题条 '\n '. import java.util.Scanner; public class exp2 { public static void main(String[] args) { int abcCount = 0;// 英文字母个数 int spaceCount = 0;// 空格题个数 int numCount = 0;// 字数个数 int otherCount = 0;// 其他字符个数 System.out.println("Please input your data! and input Enter you can Continue!"); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (Character.isLetter(ch[i])) { // 判是否字母断 abcCount++; } else if (Character.isDigit(ch[i])) { // 判是否字断数 numCount++; } else if (Character.isSpaceChar(ch[i])) { // 判是否空格题断 spaceCount++; } else { // 以上都不是题题题是其他字符 otherCount++; } } System.out.println("字母,个数" + abcCount); System.out.println("字,数个数" + numCount); System.out.println("空格,个数" + spaceCount); System.out.println("其他字符,个数" + otherCount); } } 【程序8】 题目,求s=a+aa+aaa+aaaa+aa...a的题~其中a是一字。例如个数 2+22+222+2222+22222(此题共有5相加个数)~相加有题题控制。几个数 1.程序分析,题题是题算出每一题的题。 import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; String output= " "; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); System.out.println( "题题入a的题 "); String input =stadin.readLine(); for(int i =1;i <=Integer.parseInt(input);i++) { output+=input; int a=Integer.parseInt(output); s+=a; } System.out.println(s); } } 另解: import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; int n; int t=0; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); String input = stadin.readLine(); n=Integer.parseInt(input); for(int i=1;i <=n;i++){ t=t*10+n; s=s+t; System.out.println(t); } System.out.println(s); } } 【程序9】 题目,一如果恰好等于的因子之和~题就题个数它个数称 "完数 "。例 如6=1,2,3.题程 找出1000以的所有完。内数 public class Wanshu { public static void main(String[] args) { int s; for(int i=1;i <=1000;i++) { s=0; for(int j=1;j
本文档为【JAVA经典算法题】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_882336
暂无简介~
格式:doc
大小:33KB
软件:Word
页数:0
分类:互联网
上传时间:2017-12-30
浏览量:22