【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子累颂,小兔子長到第三個月后每個月又生一對兔子对人,假如兔子都不死谣殊,問每個月的兔子總數(shù)為多少?
1.程序分析:兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....
具體分析如下:
f(1) = 1(第1個月有一對兔子)
f(2) = 1(第2個月還是一對兔子)
f(3) = 2(原來有一對兔子牺弄,第3個開始姻几,每個月生一對兔子)
f(4) = 3(原來有兩對兔子,有一對可以生育)
f(5) = 5(原來有3對兔子势告,第3個月出生的那對兔子也可以生育了蛇捌,那么現(xiàn)在有兩對兔子可以生育)
f(6) = 8(原來有5對兔子,第4個月出生的那對兔子也可以生育了咱台,那么現(xiàn)在有3對兔子可以生育)
..............
由以上可以看出络拌,第n個月兔子的對數(shù)為
f(n) = f(n - 1) + f(n - 2);
f(n-1)是上個月的兔子數(shù)量,是原來有的回溺。
f(n-2)是可以生育的兔子數(shù)春贸,即多出來的數(shù)量。第n-2個月開始后的第3個月是第n個月遗遵,此時第n-2個月時的兔子都可以生育了萍恕。
public class Demo01 {
public static void main(String args[]) {
for (int 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 Demo01 {
public static void main(String args[]) {
math mymath = new math();
for (int 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之間有多少個素數(shù),并輸出所有素數(shù)车要。
1.程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù))允粤,如果能被整除,則表明此數(shù)不是素數(shù),反之是素數(shù)类垫。
public class Demo02{
public static void main(String[] args){
for(int i=2;i<=200;i++){
boolean flag=true;
for(int j=2;j<i;j++){
if(i%j==0){
flag=false;
break;
}
}
if(flag==true){
System.out.print(" "+i);
}
}
}
}
【程序3】 題目:打印出所有的 水仙花數(shù) 绳姨,所謂 水仙花數(shù) 是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身阔挠。例如:153是一個 水仙花數(shù) ,因為153=1的三次方+5的三次方+3的三次方脑蠕。
1.程序分析:利用for循環(huán)控制100-999個數(shù)购撼,每個數(shù)分解出個位,十位谴仙,百位迂求。
public class Demo03 {
public static void main(String args[]) {
math mymath = new math();
for (int i = 100; i <= 999; i++)
if (mymath.shuixianhua(i) == true)
System.out.println(i);
}
}
class math {
public boolean shuixianhua(int x) {
int i = 0, j = 0, k = 0;
i = x/100;
j = (x%100)/10;
k = x%10;
if (x == iii + jjj + kkk)
return true;
else
return false;
}
}
【程序4】 題目:將一個正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2335晃跺。
1.程序分析:對n進行分解質(zhì)因數(shù)揩局,應(yīng)先找到一個最小的質(zhì)數(shù)i,然后按下述步驟完成:
(1)如果這個質(zhì)數(shù)恰等于n掀虎,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束凌盯,打印出即可。
(2)如果n > i烹玉,但n能被i整除驰怎,則應(yīng)打印出i的值,并用n除以i的商,作為新的正整數(shù)你,重復(fù)執(zhí)行第一步二打。
(3)如果n不能被i整除县忌,則用i+1作為i的值,重復(fù)執(zhí)行第一步。
import java.util.Scanner;
public class Demo04 {
public Demo04() {
super();
}
public void fenjie(int n) {
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
System.out.print(i);
if(n!=i){
System.out.print("");
}
fenjie(n/i);
}
}
System.exit(0); //退出程序
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入N的值:");
int N = in.nextInt();
System.out.print( "分解質(zhì)因數(shù):" + N +"=");
new Demo04().fenjie(N);
}
}
【程序5】 題目:利用條件運算符的嵌套來完成此題:學(xué)習(xí)成績=90分的同學(xué)用A表示继效,60-89分之間的用B表示症杏,60分以下的用C表示。
1.程序分析:(a>b)?a:b這是條件運算符的基本例子瑞信。
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
System.out.println("請輸入N的值:");
Scanner in = new Scanner(System.in);
int N = in.nextInt();
System.out.println(N >= 90 ?"A": (N >= 60 ? "B":"C"));
}
}
【程序6】 題目:輸入兩個正整數(shù)m和n厉颤,求其最大公約數(shù)和最小公倍數(shù)。
1.程序分析:利用輾除法凡简。
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args){
int a,b,m,n;
Scanner in=new Scanner(System.in);
System.out.println("請輸入一個正整數(shù):");
a=in.nextInt();
System.out.println("再輸入一個正整數(shù):");
b=in.nextInt();
commonDivisor use=new commonDivisor();
m=use.commonDivisor(a,b);
n=a*b/m;
System.out.println("最大公約數(shù):"+m);
System.out.println("最小公倍數(shù):"+n);
}
}
class commonDivisor{
public int commonDivisor(int x,int y){
if(x<y){
int t=x;
x=y;
y=t;
}
while(y!=0){
if(x==y)return x;
else{
int k=x%y;
x=y;
y=k;
}
}
return x;
}
}
【程序7】 題目:輸入一行字符走芋,分別統(tǒng)計出其中英文字母、空格潘鲫、數(shù)字和其它字符的個數(shù)翁逞。
1.程序分析:利用for循環(huán)語句,if條件語句。
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args){
System.out.println("請輸入一個字符串;");
Scanner in=new Scanner(System.in);
String str=in.nextLine();
char[] ch=str.toCharArray();
count use=new count();
use.count(ch);
}
}
class count{
int digital,character,blank,other;
public void count(char[] arr){
for(int i=0;i<arr.length;i++){
if(arr[i]>='0'&&arr[i]<='9'){
digital++;
}else if((arr[i]>='a'&&arr[i]<='z')||(arr[i]>='A'&&arr[i]<='Z')){
character++;
}else if(arr[i]==' '){
blank++;
}else{
other++;
}
}
System.out.println("數(shù)字個數(shù):"+digital);
System.out.println("英文字母個數(shù):"+character);
System.out.println("空格個數(shù):"+blank);
System.out.println("其他字符個數(shù):"+other);
}
}
【程序8】 題目:求s = a + aa + aaa + aaaa + aa...a的值溉仑,其中a是一個數(shù)字挖函。例如2 + 22 + 222 + 2222 + 22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制浊竟。
1.程序分析:關(guān)鍵是計算出每一項的值怨喘。
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(請輸入a的值);
int a = in.nextInt();
System.out.println(請輸入n個數(shù));
int n = in.nextInt();
int s = 0,t=0;
for (int i = 1; i <= n; i++) {
t += a;
a = a*10;
s += t;
}
System.out.println(s);
}
}
【程序9】 題目:一個數(shù)如果恰好等于它的因子之和津畸,這個數(shù)就稱為"完數(shù)"。例如6=1+2+3必怜。編程找出1000以內(nèi)的所有完數(shù)肉拓。
public class Demo09 {
public static void main(String[] args) {
int s;
for (int i = 1; i <= 1000; i++) {
s = 0;
for (int j = 1; j < i; j++)
if (i % j == 0)
s = s + j;
if (s == i)
System.out.print(i + " " );
}
System.out.println();
}
}
或
public class Demo09{
public static void main(String[] args) {
int i,j,sum;
for(i=1;i<1000;i++)
{
sum = 0;
for(j=1;j<=i/2;j++)
{
if(i%j==0)
{
sum+=j;
}
}
if(sum==i)
{
System.out.print(i+" its factors are: ");
for(j=1;j<=i/2;j++)
{
if(i%j==0)
System.out.print(j+", ");
}
System.out.println();
}
}
}
}
【程序10】 題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半梳庆;再落下暖途,求它在第10次落地時,共經(jīng)過多少米膏执?第10次反彈多高驻售?
public class Demo10 {
public static void main(String[] args) {
double s = 0;
double h = 100;
for (int i = 1; i <= 10; i++) {
s += h;
h = h/2;
s += h;
}
System.out.println("經(jīng)過路程:"+s);
System.out.println("反彈高度:"+h);
}
}
【程序11】 題目:有1、2更米、3欺栗、4個數(shù)字,能組成多少個互不相同且無重復(fù)數(shù)字的三位數(shù)征峦?都是多少迟几?
1.程序分析:可填在百位、十位栏笆、個位的數(shù)字都是1瘤旨、2、3竖伯、4存哲。組成所有的排列后再去掉不滿足條件的排列。
public class Demo11 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
for (int k = 1; k <= 4; k++)
if (i != j && j != k && i != k) {
count += 1;
System.out.println(i100 + j10 + k);
}
System.out.println("共" + count + "個三位數(shù)");
}
}
【程序12】 題目:企業(yè)發(fā)放的獎金根據(jù)利潤提成七婴。利潤(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%提成族奢,從鍵盤輸入當(dāng)月利潤lirun,求應(yīng)發(fā)放獎金總數(shù)sum丹鸿?
1.程序分析:請利用數(shù)軸來分界越走,定位。注意定義時需把獎金定義成長整型靠欢。
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
double sum;
System.out.println("輸入當(dāng)月利潤:(萬元)");
Scanner in = new Scanner(System.in);
double lirun = in.nextDouble();
if (lirun <= 10) {
sum = lirun * 0.1;
} else if (lirun <= 20) {
sum = 100.1 + (lirun - 10) * 0.075;
} else if (lirun <= 40) {
sum = 100.1 + 100.075 + (lirun - 20) * 0.05;
} else if (lirun <= 60) {
sum = 100.1 + 100.075 + 100.05 + (lirun - 40) * 0.03;
} else if (lirun <= 100) {
sum = 100.1 + 100.075 + 100.05 + 100.03 + (lirun - 60) * 0.015;
} else {
sum = 100.1 + 100.075 + 100.05 + 100.03 + 10*0.015 + (lirun - 100) * 0.01;
}
System.out.println("應(yīng)發(fā)的獎金是:"+sum+"(萬元)");
}
}
【程序13】 題目:一個整數(shù)廊敌,它加上100后是一個完全平方數(shù),加上168又是一個完全平方數(shù)掺涛,請問該數(shù)是多少?
1.程序分析:在10萬以內(nèi)判斷疼进,先將該數(shù)加上100后再開方薪缆,再將該數(shù)加上168后再開方,如果開方后的結(jié)果滿足如下條件伞广,即是結(jié)果拣帽。請看具體分析:
public class Demo13 {
public static void main(String[] args) {
for(int x=1;x<100000;x++){
if(Math.sqrt(x+100)%1==0)
if(Math.sqrt(x+100+168)%1==0)
System.out.println(x+"加上100后是一個完全平方數(shù),加上168又是一個完全平方數(shù)");
}
}
}
【程序14】 題目:輸入某年某月某日嚼锄,判斷這一天是這一年的第幾天减拭?
1.程序分析:以3月5日為例,應(yīng)該先把前兩個月的加起來区丑,然后再加上5天即本月的第幾天拧粪,特殊情況,閏年且輸入月份大于3時需考慮多加一天沧侥。
import java.util.Calendar;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
System.out.println("請輸入年,月,日:");
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
int sum = cal.get(Calendar.DAY_OF_YEAR);
System.out.println("這一天是這一年的第" + sum +"天");
}
}
或
import java.util.*;
public class Demo14 {
public static void main(String[] args){
int year,month,day,sum=0;
Scanner in=new Scanner(System.in);
System.out.println("輸入年:");
year=in.nextInt();
System.out.println("輸入月:");
month=in.nextInt();
System.out.println("輸入日:");
day=in.nextInt();
switch(month){
case 1:
sum=0;
break;
case 2:
sum=31;
break;
case 3:
sum=59;
break;
case 4:
sum=90;
break;
case 5:
sum=120;
break;
case 6:
sum=151;
break;
case 7:
sum=181;
break;
case 8:
sum=212;
break;
case 9:
sum=243;
break;
case 10:
sum=273;
break;
case 11:
sum=304;
break;
case 12:
sum=334;
break;
default:
System.out.println("wrong input!");
return;
}
sum=sum+day;
boolean leap;
if(year%400==0||(year%4==0&&year%100!=0)){
leap=true;
}else {
leap=false;
}
if(leap&&month>2){
sum++;
}
System.out.println("It is the "+sum+"th day.");
}
}
或
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args){
System.out.println("請輸入年 月 日:");
Scanner in=new Scanner(System.in);
int year=in.nextInt();
int month=in.nextInt();
int day=in.nextInt();
System.out.println("是該年的第"+count(year,month,day)+"天");
}
public static int count(int year,int month,int day){
int sum=0;
int days=0;
for(int i=1;i<month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if(year%400==0||year%4==0&&year%100!=0){
days=29;
}else{
days=28;
}
break;
}
sum+=days;
}
sum+=day;
return sum;
}
}
【程序15】 題目:輸入三個整數(shù)x,y,z可霎,請把這三個數(shù)由小到大輸出。
1.程序分析:我們想辦法把最小的數(shù)放到x上宴杀,先將x與y進行比較癣朗,如果x>y則將x與y的值進行交換,然后再用x與z進行比較旺罢,如果x>z則將x與z的值進行交換旷余,這樣能使x最小。
import java.util.Arrays;
import java.util.Scanner;
public class Demo15 {
public static void main(String[] args) {
System.out.print("請輸入三個數(shù):");
Scanner in = new Scanner(System.in);
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
for (int i=0;i<arr.length;i++) {
System.out.print(arr[i] + " ");
}
}
}
或
if(x > y) { int t = x; x = y; y = t; } if(x > z) { int t = x; x = z; z = t; } if(y > z) { int t = y; y = z; z = t; }
【程序16】 題目:輸出99口訣乘法表扁达。
1.程序分析:分行與列考慮正卧,共9行9列,i控制行跪解,j控制列穗酥。
出現(xiàn)重復(fù)的乘積(全矩形)
public class Demo16 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++)
System.out.print(i + "" + j + "=" + (ij) + "\t");
System.out.println();
}
}
}
不現(xiàn)重復(fù)的乘積(下三角)
public class Demo16 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++)
System.out.print(i + "" + j + "=" + (i*j) + "\t");
System.out.println();
}
}
}
【程序17】 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮砾跃,又多吃了一個第二天早上又將剩下的桃子吃掉一半骏啰,又多吃了一個。以后每天早上都吃了前一天剩
下的一半零一個抽高。到第10天早上想再吃時判耕,見只剩下一個桃子了。求第一天共摘了多少翘骂。
1.程序分析:采取逆向思維的方法壁熄,從后往前推斷。
public class Demo17 {
public static void main(String[] args) {
int sum = 1;
for (int i = 0; i < 9; i++) {
sum = (sum + 1) * 2;
}
System.out.println("第一天共摘"+sum);
}
}
【程序18】 題目:兩個乒乓球隊進行比賽碳竟,各出三人草丧。甲隊為a,b,c三人,乙隊為x,y,z三人莹桅。已抽簽決定比賽名單昌执。有人向隊員打聽比賽的名單。a說他不和x比诈泼,c說他不和x,z比懂拾,請編程
序找出三隊賽手的名單。
public class Demo18 {
static char[] m = { 'a', 'b', 'c' };
static char[] n = { 'x', 'y', 'z' };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++) {
if (m[i] == 'a' && n[j] == 'x') {
continue;
} else if (m[i] == 'a' && n[j] == 'y') {
continue;
} else if ((m[i] == 'c' && n[j] == 'x')
|| (m[i] == 'c' && n[j] == 'z')) {
continue;
} else if ((m[i] == 'b' && n[j] == 'z')
|| (m[i] == 'b' && n[j] == 'y')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
或
public class Demo18 {
public String a, b, c;
public Demo18(String a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
public static void main(String[] args) {
Demo18 arr_a = new Demo18("a", "b", "c");
String[] b = { "x", "y", "z" };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
Demo18 arr_b = new Demo18(b[i], b[j], b[k]);
if (!arr_b.a.equals(arr_b.b) & !arr_b.b.equals(arr_b.c)
& !arr_b.c.equals(arr_b.a) & !arr_b.a.equals("x")
& !arr_b.c.equals("x") & !arr_b.c.equals("z")) {
System.out.println(arr_a.a + "--" + arr_b.a);
System.out.println(arr_a.b + "--" + arr_b.b);
System.out.println(arr_a.c + "--" + arr_b.c);
}
}
}
}
}
}
【程序19】 題目:打印出如下圖案(菱形)
1.程序分析:先把圖形分成兩部分來看待铐达,前四行一個規(guī)律岖赋,后三行一個規(guī)律,利用雙重for循環(huán)瓮孙,第一層控制行唐断,第二層控制列。
三角形:
public class Demo19 {
public static void main(String[] args) {
int i=0;
int j=0;
for ( i = 1; i <= 4; i++) {
for ( j = 1; j <= 2 * i - 1; j++)
System.out.print("");
System.out.println();
}
for ( i = 3; i >= 1; i--) {
for ( j = 1; j <= 2 * i - 1; j++)
System.out.print("");
System.out.println();
}
}
}
菱形:
public class Demo19 {
public static void main(String[] args) {
int i = 0;
int j = 0;
for (i = 1; i <= 4; i++) {
for (int k = 1; k <= 4 - i; k++)
System.out.print( " " );
for (j = 1; j <= 2 * i - 1; j++)
System.out.print("");
System.out.println();
}
for (i = 3; i >= 1; i--) {
for (int k = 1; k <= 4 - i; k++)
System.out.print( " " );
for (j = 1; j <= 2 * i - 1; j++)
System.out.print("");
System.out.println();
}
}
}
【程序20】 題目:有一分數(shù)序列:2/1杭抠,3/2栗涂,5/3,8/5祈争,13/8斤程,21/13...求出這個數(shù)列的前20項之和。
1.程序分析:請抓住分子與分母的變化規(guī)律菩混。
public class Demo20 {
public static void main(String[] args) {
float fm = 1.0f;
float fz = 1.0f;
float temp;
float sum = 0f;
for (int i = 0; i < 20; i++) {
temp = fm;
fm = fz;
fz = fz + temp;
System.out.println((int) fz + "/" + (int) fm);
sum += fz / fm;
}
System.out.println(sum);
}
}
【程序21】 題目:求1+2!+3!+...+20!的和忿墅。
1.程序分析:此程序只是把累加變成了累乘。
public class Demo21 {
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for (int i = 1; i <= 20; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
}
【程序22】 題目:利用遞歸方法求5!沮峡。
1.程序分析:遞歸公式:f(n)=f(n-1)*4!
import java.util.Scanner;
public class Demo22 {
public static long fac(int n) {
long value = 0;
if (n == 1 || n == 0) {
value = 1;
} else if (n > 1) {
value = n * fac(n - 1);
}
return value;
}
public static void main(String[] args) {
System.out.println("請輸入一個數(shù):");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(n + "的階乘為:" + fac(n));
}
}
【程序23】 題目:有5個人坐在一起疚脐,問第五個人多少歲?他說比第4個人大2歲邢疙。問第4個人歲數(shù)棍弄,他說比第3個人大2歲望薄。問第三個人,又說比第2人大兩歲呼畸。問第2個人痕支,說比第一個人大兩
歲。最后問第一個人蛮原,他說是10歲卧须。請問第五個人多大?
1.程序分析:利用遞歸的方法儒陨,遞歸分為回推和遞推兩個階段花嘶。要想知道第五個人歲數(shù),需知道第四人的歲數(shù)蹦漠,依次類推椭员,推到第一人(10歲),再往回推笛园。
直接求解:
public class Demo23 {
public static void main(String[] args) {
int n = 10;
for (int i = 0; i < 4; i++) {
n = n + 2;
}
System.out.println("第五個人" + n + "歲");
}
}
遞歸求解:
public class Demo23 {
public static int getAge(int n) {
if (n == 1) {
return 10;
}
return 2 + getAge(n - 1);
}
public static void main(String[] args) {
System.out.println("第五個的年齡為" + getAge(5));
}
}
【程序24】 題目:給一個不多于5位的正整數(shù)隘击,要求:一、求它是幾位數(shù)喘沿,二闸度、逆序打印出各位數(shù)字竭贩。
本題原方法:
import java.util.Scanner;
public class Demo24 {
public static void main(String[] args) {
Demo24 use = new Demo24();
System.out.println("請輸入:");
Scanner in = new Scanner(System.in);
long a = in.nextLong();
if (a < 0 || a >= 100000) {
System.out.println("Error Input, please run this program Again!");
System.exit(0);
}
if (a >= 0 && a <= 9) {
System.out.println(a + "是一位數(shù)");
System.out.println("按逆序輸出是:" + a);
} else if (a >= 10 && a <= 99) {
System.out.println(a + "是二位數(shù)");
System.out.println("按逆序輸出是:");
use.converse(a);
} else if (a >= 100 && a <= 999) {
System.out.println(a + "是三位數(shù)");
System.out.println("按逆序輸出是:");
use.converse(a);
} else if (a >= 1000 && a <= 9999) {
System.out.println(a + "是四位數(shù)");
System.out.println("按逆序輸出是:");
use.converse(a);
} else if (a >= 10000 && a <= 99999) {
System.out.println(a + "是五位數(shù)");
System.out.println("按逆序輸出是:");
use.converse(a);
}
}
public void converse(long l) {
String s = Long.toString(l);
char[] ch = s.toCharArray();
for (int i = ch.length - 1; i >= 0; i--) {
System.out.print(ch[i]);
}
}
}
個人版方法:
import java.util.Scanner;
public class Demo24 {
public static void main(String[] args) {
System.out.println("請輸入:");
Scanner in = new Scanner(System.in);
String str = in.next();
if (str.matches("\d+")) { //正則表達式
System.out.println("輸入的是" + str.length() + "位數(shù)");
StringBuffer buf = new StringBuffer(str);
System.out.println(buf.reverse());//字符串反轉(zhuǎn)
}
}
}
【程序25】 題目:一個5位數(shù)蚜印,判斷它是不是回文數(shù)。即12321是回文數(shù)留量,個位與萬位相同窄赋,十位與千位相同。
原方法:
import java.util.Scanner;
public class Demo25 {
static int[] a = new int[5];
static int[] b = new int[5];
public static void main(String[] args) {
boolean is = false;
System.out.println("Please input:");
Scanner in = new Scanner(System.in);
long l = in.nextLong();
if (l > 99999 || l < 10000) {
System.out.println("Input error, please input again!");
l = in.nextLong();
}
for (int i = 4; i >= 0; i--) {
a[i] = (int) (l / (long) Math.pow(10, i));
l = (l % (long) Math.pow(10, i));
}
System.out.println();
for (int i = 0, j = 0; i < 5; i++, j++) {
b[j] = a[i];
}
for (int i = 0, j = 4; i < 5; i++, j--) {
if (a[i] != b[j]) {
is = false;
break;
} else {
is = true;
}
}
if (is == false) {
System.out.println("is not a Palindrom!");
} else if (is == true) {
System.out.println("is a Palindrom!");
}
}
}
個人版:
import java.util.Scanner;
public class Demo25 {
public static void main(String[] args) {
System.out.println("請輸入:");
Scanner in = new Scanner(System.in);
String str = in.next();
int l = Integer.parseInt(str);//轉(zhuǎn)換成整數(shù)
if (l < 10000 || l > 99999) {
System.out.println("輸入錯誤楼熄!");
System.exit(0);
}
boolean is=false;
char[] ch = str.toCharArray();
for(int i=0;i<ch.length/2;i++){
if(ch[i]!=ch[ch.length-i-1]){
is=false;
}else{
is=true;
}
}
if(is){
System.out.println("這是一個回文!");
}else{
System.out.println("不是一個回文!");
}
}
}
【程序26】 題目:請輸入星期幾的第一個字母來判斷一下是星期幾忆绰,如果第一個字母一樣,則繼續(xù)判斷第二個字母可岂。
1.程序分析:用情況語句比較好错敢,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母缕粹。
import java.util.Scanner;
public class Demo26 {
public static void main(String[] args) {
char weekSecond;//保存第二字母
Scanner in = new Scanner(System.in);//接收用戶輸入
System.out.println("請輸入星期的第一個字母:");
String letter = in.next();
if (letter.length() == 1) {//判斷用戶控制臺輸入字符串長度是否是一個字母
char weekFirst = letter.charAt(0);//取第一個字符
switch (weekFirst) {
case 'm':
case 'M':
System.out.println("星期一(Monday)");
break;
case 't':
case 'T':
System.out.print("由于星期二(Tuesday)與星期四(Thursday)均以字母T開頭稚茅,故需輸入第二個字母才能正確判斷:");
letter = in.next();
if (letter.length() == 1) {
weekSecond = letter.charAt(0);
if (weekSecond == 'U' || weekSecond == 'u') {
System.out.println("星期二(Tuesday)");
break;
} else if (weekSecond == 'H' || weekSecond == 'h') {
System.out.println("星期四(Thursday)");
break;
} else {
System.out.println("Error!");
break;
}
} else {
System.out.println("輸入錯誤,只能輸入一個字母平斩,程序結(jié)束亚享!");
break;
}
case 'w':
case 'W':
System.out.println("星期三(Wednesday)");
break;
case 'f':
case 'F':
System.out.println("星期五(Friday)");
break;
case 's':
case 'S':
System.out.print("由于星期六(Saturday)與星期日(Sunday)均以字母S開頭,故需輸入第二個字母才能正確判斷:");
letter = in.next();
if (letter.length() == 1) {
weekSecond = letter.charAt(0);
if (weekSecond == 'A' || weekSecond == 'a') {
System.out.println("星期六(Saturday)");
break;
} else if (weekSecond == 'U' || weekSecond == 'u') {
System.out.println("星期日(Sunday)");
break;
} else {
System.out.println("Error!");
break;
}
} else {
System.out.println("輸入錯誤绘面,只能輸入一個字母欺税,程序結(jié)束侈沪!");
break;
}
default:
System.out.println("輸入錯誤,不能識別的星期值第一個字母晚凿,程序結(jié)束亭罪!");
break;
}
} else {
System.out.println("輸入錯誤,只能輸入一個字母晃虫,程序結(jié)束皆撩!");
}
}
}
【程序27】 題目:求100之內(nèi)的素數(shù)
public class Demo27 {
public static void main(String args[]) {
int sum, i;
for (sum = 2; sum <= 100; sum++) {
for (i = 2; i <= sum / 2; i++) {
if (sum % i == 0)
break;
}
if (i > sum / 2)
System.out.println(sum + "是素數(shù)");
}
}
}
或
public class Demo27{
public static void main(String args[]){
int w=1;
for(int i=2;i<=100;i++){
for(int j=2;j<i;j++){
w=i%j;
if(w==0)break;
}
if(w!=0)
System.out.println(i+"是素數(shù)");
}
}
}
【程序28】 題目:對10個數(shù)進行排序。
1.程序分析:可以利用選擇法哲银,即從后9個比較過程中扛吞,選擇一個最小的與第一個元素交換,下次類推荆责,即用第二個元素與后8個進行比較滥比,并進行交換。
本例代碼為生成隨機10個數(shù)排序做院,并輸入1個數(shù)盲泛,插入重排序輸出:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Demo28 {
public static void main(String[] args) {
int arr[] = new int[11];
Random r = new Random();
for (int i = 0; i < 10; i++) {
arr[i] = r.nextInt(100) + 1; //得到10個100以內(nèi)的整數(shù)
}
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] +"\t");
}
System.out.print("\nPlease Input a int number:" );
Scanner in = new Scanner(System.in);
arr[10] = in.nextInt();
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] +"\t");
}
}
}
個人代碼:
import java.util.Arrays;
import java.util.Scanner;
public class Demo28 {
public static void main(String[] args) {
System.out.println("請輸入10個數(shù):");
Scanner in = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = in.nextInt();
}
System.out.println("原數(shù)組為:");
for (int x : arr) {//foreach遍歷
System.out.print( x + "\t");
}
Arrays.sort(arr);
System.out.println();
System.out.println("排序后為:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
【程序29】 題目:求一個3*3矩陣主對角線元素之和。
1.程序分析:利用雙重for循環(huán)控制輸入二維數(shù)組键耕,再將a[i][i]累加后輸出寺滚。
public class Demo29 {
public static void main(String[] args) {
double sum = 0;
int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 7, 8 } };
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (i == j)
sum = sum + array[i][j];
}
System.out.println(sum);
}
}
主負對角線:
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j) sum1+=a[i][j];
if(i+j==n-1) sum2+=a[i][j];
}
【程序30】 題目:有一個已經(jīng)排好序的數(shù)組。現(xiàn)輸入一個數(shù)屈雄,要求按原來的規(guī)律將它插入數(shù)組中村视。
1.程序分析:首先判斷此數(shù)是否大于最后一個數(shù),然后再考慮插入中間的數(shù)的情況酒奶,插入后此元素之后的數(shù)蚁孔,依次后移一個位置。
import java.util.Random;
public class Demo30 {
public static void main(String[] args) {
int temp = 0;
int arr[] = new int[12];
Random r = new Random();
for (int i = 0; i <= 10; i++)
arr[i] = r.nextInt(1000);
for (int i = 0; i <= 10; i++)
System.out.print(arr[i] + "\t");
for (int i = 0; i <= 9; i++)
for (int k = i + 1; k <= 10; k++)
if (arr[i] > arr[k]) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
System.out.println();
for (int k = 0; k <= 10; k++)
System.out.print(arr[k] + "\t");
arr[11] = r.nextInt(1000);
for (int k = 0; k <= 10; k++)
if (arr[k] > arr[11]) {
temp = arr[11];
for (int j = 11; j >= k + 1; j--)
arr[j] = arr[j - 1];
arr[k] = temp;
}
System.out.println();
for (int k = 0; k <= 11; k++)
System.out.print(arr[k] + "\t");
}
}
【程序31】 題目:將一個數(shù)組逆序輸出惋嚎。
程序分析:用第一個與最后一個交換杠氢。
用逆序循環(huán)控制變量輸出:
public class Demo31 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
}
}
【程序32】 題目:取一個整數(shù)a從右端開始的第4~7位數(shù)字。
import java.util.*;
public class Demo32 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入一個7位以上的正整數(shù):");
long l = in.nextLong();
String str = Long.toString(l);
char[] ch = str.toCharArray();
int j=ch.length;
if (j<7){System.out.println("輸入錯誤另伍!");
} else {
System.out.println("截取從右端開始的4~7位是:"+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);
}
}
}
或
import java.util.Scanner;
public class Demo32{
public static void main(String[] args) {
int a = 0;
Scanner s = new Scanner(System.in);
long b = s.nextLong();
a = (int) (b % 10000000 / 1000);
System.out.println(a);
}
}
【程序33】 題目:打印出楊輝三角形(要求打印出10行如下圖)
1.程序分析:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
public class Demo33 {
public static void main(String args[]) {
int i, j;
int a[][];
int n = 10;
a = new int[n][n];
for (i = 0; i < n; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (i = 2; i < n; i++) {
for (j = 1; j <= i - 1; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
System.out.printf(a[i][j] + "\t");
}
System.out.println();
}
}
}
【程序34】 題目:輸入3個數(shù)a,b,c鼻百,按大小順序輸出。
(也可互相比較交換排序)
import java.util.Arrays;
public class Demo34 {
public static void main(String[] args) {
int[] arrays = { 800, 56, 500 };
Arrays.sort(arrays);
for (int n = 0; n < arrays.length; n++)
System.out.println(arrays[n]);
}
}
或
if(x > y) { int t = x; x = y; y = t; } if(x > z) { int t = x; x = z; z = t; } if(y > z) { int t = y; y = z; z = t; }
【程序35】 題目:輸入數(shù)組摆尝,最大的與第一個元素交換温艇,最小的與最后一個元素交換,輸出數(shù)組结榄。
import java.util.*;
public class Demo35 {
public static void main(String[] args) {
int i, min=0, max=0, n, temp1, temp2;
int a[];
System.out.println("定義數(shù)組的長度:");
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n];
for (i = 0; i < n; i++) {
System.out.print("輸入第" + (i + 1) + "個數(shù)據(jù):");
a[i] = in.nextInt();
}
for (i = 1; i < n; i++) {
if (a[i] > a[max])
max = i;
if (a[i] < a[min])
min = i;
}
temp1 = a[0];
a[0] = a[max];
a[max] = temp1;
temp2 = a[min];
if (min != 0) { // 如果最小值不是a[0]中贝,執(zhí)行下面
a[min] = a[n - 1];
a[n - 1] = temp2;
} else { //如果最小值是a[0],執(zhí)行下面
a[max] = a[n - 1];
a[n - 1] = temp1;
}
for (i = 0; i < n; i++) {
System.out.print(a[i] + " " );
}
}
}
【程序36】 題目:有n個整數(shù),使其前面各數(shù)順序向后移m個位置臼朗,最后m個數(shù)變成最前面的m個數(shù)
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Demo36 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("輸入數(shù)字個數(shù)n:");
int n = in.nextInt();
System.out.println("輸入后移位數(shù)m:");
int m = in.nextInt();
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
System.out.println("請輸入第"+(i+1)+"個數(shù):");
list.add(in.nextInt());
}
System.out.println("原數(shù)據(jù)排序為:");
for (int t : list) {
System.out.print(t + " " );
}
System.out.println();
List<Integer> temp1 = list.subList(list.size() - m, list.size());
List<Integer> temp2 = list.subList(0, list.size() - m);
temp2.addAll(0, temp1);
System.out.println("移動后排序為;");
for (int t : temp2) {
System.out.print(t + " " );
}
}
}
或
import java.util.*;
public class Demo36{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("請定義數(shù)組的長度:");
int n=in.nextInt();
System.out.println("請輸入移動的位數(shù):");
int m=in.nextInt();
int [] arr=new int [n];
int [] brr=new int [n];
for(int i=0;i<n;i++){
System.out.println("請輸入第"+(i+1)+"個數(shù):");
arr[i]=in.nextInt();
}
System.out.println("排序前:");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
for(int i=0;i<m;i++){
brr[i]=arr[n-m+i];
}
for(int i=0;i<n-m;i++){
arr[m+i]=arr[i];
}
for(int i=0;i<m;i++){
arr[i]=brr[i];
}
System.out.println("排序后:");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
}
}
【程序37】 題目:有n個人圍成一圈邻寿,順序排號蝎土。從第一個人開始報數(shù)(從1到3報數(shù)),凡報到3的人退出圈子绣否,問最后留下的是原來第幾號的那位誊涯。
(約瑟夫環(huán)問題,百度百科有時間復(fù)雜度最簡單的數(shù)學(xué)方法)
原例代碼:
import java.util.Scanner;
public class Demo37 {
public static void main(String[] args) {
System.out.println("請輸人數(shù)n:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
boolean[] arr = new boolean[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = true; //下標(biāo)為TRUE時說明還在圈里
}
int leftCount = n;
int countNum = 0;
int index = 0;
while (leftCount > 1) {
if (arr[index] == true) { //當(dāng)在圈里時
countNum++; //報數(shù)遞加
if (countNum == 3) { //報數(shù)為3時
countNum = 0; //從零開始繼續(xù)報數(shù)
arr[index] = false; //此人退出圈子
leftCount--; //剩余人數(shù)減一
}
}
index++; //每報一次數(shù)蒜撮,下標(biāo)加一
if (index == n) { //是循環(huán)數(shù)數(shù)暴构,當(dāng)下標(biāo)大于n時,說明已經(jīng)數(shù)了一圈段磨,
index = 0; //將下標(biāo)設(shè)為零重新開始取逾。
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == true) {
System.out.println(i);
}
}
}
}
個人代碼1:
import java.util.Scanner;
public class Demo37 {
public static void main(String[] args) {
System.out.println("請輸入人數(shù):");
Scanner in = new Scanner(System.in);
int[] a = new int[in.nextInt()];
for (int i = 0; i < a.length; i++) {
a[i] = 1;
}
int left = a.length;
int j = 0;
int num = 0;
while (left > 1) {
if (a[j] == 1) {
num++;
}
if (num == 3) {
a[j] = 0;
num = 0;
left--;
}
j++;
if (j == a.length) {
j = 0;
}
}
for (int i = 0; i < a.length; i++) {
if (a[i] == 1) {
System.out.println("最后留下的人是"+ (i + 1) + "號");
break;
}
}
}
}
個人代碼2:
import java.util.LinkedList;
import java.util.Scanner;
public class Demo37 {
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<Integer>();
System.out.println("請輸入人數(shù):");
Scanner in = new Scanner(System.in);
int len = in.nextInt();
for (int i = 0; i < len; i++) {
l.add(i + 1);
}
int sum = 0;
int temp = 0;
for (int i = 0; sum != len - 1;) {
if (l.get(i) != 0) {
temp++;
}
if (temp == 3) {
l.remove(i);
l.add(i, 0);
temp = 0;
sum++;
}
i++;
if (i == l.size()) {
i = 0;
}
}
for (int t : l) {
if (t != 0) {
System.out.println("最后留下的人是" + t + "號");
}
}
}
}
【程序38】 題目:寫一個函數(shù),求一個字符串的長度苹支,在main函數(shù)中輸入字符串砾隅,并輸出其長度。
import java.util.Scanner;
public class Demo38 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入一個字符串:");
String mys = in.next();
System.out.println(str_len(mys));
}
public static int str_len(String x) {
return x.length();
}
}
或
import java.util.Scanner;
public class Demo38 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入一個字符串:");
String mys = in.next();
System.out.println(mys.length());
}
}
【程序39】 題目:編寫一個函數(shù)债蜜,輸入n為偶數(shù)時晴埂,調(diào)用函數(shù)求1/2+1/4+...+1/n,當(dāng)輸入n為奇數(shù)時,調(diào)用函數(shù)1/1+1/3+...+1/n
import java.util.Scanner;
public class Demo39 {
public static double ouShu(int n) {
double result = 0;
for (int i = 2; i <= n; i = i + 2) {
result += 1 / (double) i;
}
return result;
}
public static double jiShu(int n) {
double result = 0;
for (int i = 1; i <= n; i = i + 2) {
result += 1 / (double) i;
}
return result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("輸入n的值:");
int n = in.nextInt();
if (n % 2 == 0) { //偶數(shù)寻定,1/2+1/4+...+1/n
System.out.println(ouShu(n));
} else { //奇數(shù)儒洛,1/1+1/3+...+1/n
System.out.println(jiShu(n));
}
}
}
【程序40】 題目:字符串排序。
(利用容器類中的sort方法)
import java.util.;
public class Demo40 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("010102");
list.add("010003");
list.add("010201");
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
或
import java.util.;
public class Demo40 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("請定義字符串的個數(shù):");
int n=in.nextInt();
String[] str=new String[n];
for(int i=0;i<str.length;i++){
System.out.println("請輸入第"+(i+1)+"字符串:");
str[i]=in.next();
}
strSort(n,str);
System.out.println("字符串排序后:");
for(int i=0;i<str.length;i++){
System.out.print(str[i]+" ");
}
}
public static void strSort(int n,String[] arr){
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if(compare(arr[i], arr[j]) == false) {
String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
}
}
static boolean compare(String s1, String s2) {
boolean result = true;
for(int i=0; i<s1.length() && i<s2.length(); i++) {
if(s1.charAt(i) > s2.charAt(i)) {
result = false;
break;
} else if(s1.charAt(i) <s2.charAt(i)) {
result = true;
break;
} else {
if(s1.length() < s2.length()) {
result = true;
} else {
result = false;
}
}
}
return result;
}
}
【程序41】 題目:海灘上有一堆桃子狼速,五只猴子來分琅锻。第一只猴子把這堆桃子平均分為五份,多了一個唐含,這只猴子把多的一個扔入海中浅浮,拿走了一份沫浆。第二只猴子把剩下的桃子又平均分成五份捷枯,又多了一個,它同樣把多的一個扔入海中专执,拿走了一份淮捆,第三、第四本股、第五只猴子都是這樣做的攀痊,問海灘上原來最少有多少個桃子?
本題源碼:
public class Demo41 {
static int ts = 0;// 桃子總數(shù)
int fs = 1;// 記錄分的次數(shù)
static int hs = 5;// 猴子數(shù)
int tsscope = 5000;// 桃子數(shù)的取值范圍拄显,太大容易溢出苟径。
public int fT(int t) {
if (t == tsscope) {
// 當(dāng)桃子數(shù)到了最大的取值范圍時取消遞歸
System.out.println("結(jié)束");
return 0;
} else {
if ((t - 1) % hs == 0 && fs <= hs) {
if (fs == hs) {
System.out.println("桃子數(shù)=" + ts + "時滿足分桃條件");
}
fs += 1;
return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的總數(shù)
} else {
// 沒滿足條件
fs = 1;// 分的次數(shù)重置為1
return fT(ts += 1);// 桃子數(shù)加+1
}
}
}
public static void main(String[] args) {
new Demo41().fT(0);
}
}
個人修改:
public class Demo41 {
public static void main(String[] args) {
int sum = 0;
for (int i = 6;; i++) {// 最少6個分最后一次
sum = i;// 桃子數(shù)
for (int j = 0; j < 5; j++) {// 分的次數(shù)循環(huán)
if ((sum - 1) % 5 == 0 && j < 5) {// 如果扔一個后能均分5份,繼續(xù)分
sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子數(shù)
if (j == 4) {// 如果已分5次躬审,且仍能除盡棘街,輸出藐不,退出程序
System.out.println(i);
System.exit(0);
}
}
}
}
}
}
【程序42】 題目:809??=800??+9??+1摇展。其中??代表的兩位數(shù),8??的結(jié)果為兩位數(shù),9??的結(jié)果為3位數(shù)。求??代表的兩位數(shù)立哑,及809??后的結(jié)果。
(本題為無解致讥,去掉1有解)
public class Demo42 {
public static void main(String[] args) {
for (int i = 10; i < 100; i++) {
if (809 * i == (800 * i + 9 * i + 1) && 8 * i >= 10 && 8 * i < 100
&& 9 * i >= 100 && 9 * i < 1000) {
System.out.println("?? =" + i);
System.out.println("809*??="+ 809 * i);
System.exit(0);
}
}
}
}
【程序43】 題目:求0—7所能組成的奇數(shù)個數(shù)憎瘸。
暴力算法:
public class Demo43 {
public static boolean isJiShu(int n) {
if (n % 2 != 0) {
return true;
} else {
return false;
}
}
public static boolean fun(char c) {
if (c >= '0' && c <= '7') {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int count = 0;
String s;
for (int i = 0; i < 100000000; i++) {
s = "" + i;
boolean flag = true;
char[] c = s.toCharArray();
for (int j = 0; j < c.length; j++) {
if (!fun(c[j])) {
flag = false;
break;
}
}
if (flag && isJiShu(i)) {
count++;
}
s = "";
}
System.out.println("共" + count + "個。");
}
}
數(shù)學(xué)算法:
public class Demo43 {
public static void main(String[] args) {
// 因為是奇數(shù)蛔糯,所以個位只能是1拯腮,3,5蚁飒,7共4種疾瓮,前面可隨便排列
int count = 4;// 個位的4種
// 2位時,十位有8種飒箭,個位4種狼电,8×4
// 3位時,8×8×4……
for (int i = 1; i < 8; i++) {
count = 8 * count;
System.out.println("count:" + count);
}
}
}
個人算法:
//組成1位數(shù)是4個弦蹂。
//組成2位數(shù)是74個肩碟。
//組成3位數(shù)是784個。
//組成4位數(shù)是7884個凸椿。
//......
public class Demo43 {
public static void main (String[] args) {
int sum=4;
int j;
System.out.println("組成1位數(shù)是 "+sum+" 個");
sum=sum7;
System.out.println("組成2位數(shù)是 "+sum+" 個");
for(j=3;j<=9;j++){
sum=sum8;
System.out.println("組成"+j+"位數(shù)是 "+sum+" 個");
}
}
}
【程序44】 題目:一個偶數(shù)總能表示為兩個素數(shù)之和削祈。(注:哥德巴赫猜想是想證明對任何大于6的自然數(shù)n之內(nèi)的所有偶數(shù)可以表示為兩個素數(shù)之和)
public class Demo44 {
public static boolean isSuShu(int x) {
if (x == 0 || x == 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
// 求了下100以內(nèi)的情況
for (int i = 0; i < 100; i = i + 2) {
for (int j = 0; j <= (i + 1) / 2; j++) {
if (isSuShu(j) && isSuShu(i - j)) {
System.out.println(i + "=" + j + "+" + (i - j));
}
}
}
}
}
或
public class Demo44{
public static void main(String[] args){
for (int i=6;i<=100 ;i+=2 ){
for (int j=2;j<100 ;j++ ){
if(!isPrime(j)||!isPrime(i-j)||j>=i)
continue;
System.out.println(i+"="+j+"+"+(i-j));
break;
}
}
}
public static boolean isPrime(int n){
for (int i=2;i<n ;i++ ){
if(n%i==0)return false;
}
return true;
}
}
【程序45】 題目:(1)判斷幾個9能被一個素數(shù)整除。(2)判斷一個整數(shù)能被幾個9整除脑漫。(原題:一個素數(shù)能被幾個9整除)
(一)
public class Demo45 {
public static boolean isSuShu(int x) {
if (x == 0 || x == 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] a = new int[100];
int n = 0;
int num = 0;
// 長度100的素數(shù)數(shù)組
while (n < 100) {
if (isSuShu(num)) {
a[n] = num;
n++;
num++;
} else {
num++;
}
}
/* for (int t : a) {
System.out.println(t);
}*/
String s = "9";
int index = 0;
while (s.length() < 9) {
if (new Integer(s).intValue() % a[index] == 0) {
System.out.println(s + "%" + a[index] + "=0");
if (index < 100 - 1) {
index++;
} else {
index = 0;
s = s + "9";
}
// System.exit(0);
} else {
if (index < 100 - 1) {
index++;
} else {
index = 0;
s = s + "9";
}
}
}
}
}
(二)
import java.util.; public class Demo45 {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入一個整數(shù):");
int num = in.nextInt();
int tmp = num;
int count = 0;
for(int i = 0 ; tmp%9 == 0 ;){
tmp = tmp/9;
count ++;
}
System.out.println(num+" 能夠被 "+count+" 個9 整除髓抑。");
}
}
【程序46】 題目:兩個字符串連接程序。
import java.util.Scanner;
public class Demo46 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("輸入第一個字符串:");
String s1 = in.next();
System.out.println("輸入第一個字符串:");
String s2 = in.next();
System.out.println("連接后:\n" + s1 + s2);
}
}
或
import java.util.;
public class Demo46 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入一個字符串:");
String str1 = in.nextLine();
System.out.print("請再輸入一個字符串:");
String str2 = in.nextLine();
String str = str1+str2;
System.out.println("連接后的字符串是:"+str);
}
}
【程序47】 題目:讀取7個數(shù)(1—50)的整數(shù)值优幸,每讀取一個值吨拍,程序打印出該值個數(shù)的。
import java.util.;
public class Demo47 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n=1,num;
while(n<=7){
do{
System.out.print("請輸入一個1--50 之間的整數(shù):");
num= s.nextInt();
}while(num<1||num>50);
for(int i=1;i<=num;i++)
{System.out.print("");
}
System.out.println();
n ++;
}
}
}
或
import java.util.Scanner;
public class Demo47 {
public static void print(int n) {
for (int i = 0; i < n; i++) {
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
int temp = in.nextInt();
print(temp);
}
}
}
【程序48】 題目:某個公司采用公用電話傳遞數(shù)據(jù)网杆,數(shù)據(jù)是四位的整數(shù)羹饰,在傳遞過程中是加密的,加密規(guī)則如下:每位數(shù)字都加上5碳却,然后用和除以10的余數(shù)代替該數(shù)字队秩,再將第一位和第四位交換,第二位和第三位交換昼浦。
import java.util.Scanner;
public class Demo48{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入一個4位數(shù)字:");
String str = in.next();
if (!((str).matches("\d{4}"))) {
System.out.println("輸入的不是4位數(shù)字馍资!");
System.exit(0);
}
char[] c = str.toCharArray();
int[] a = new int[4];
for (int i = 0; i < a.length; i++) {
a[i] = ((int) (c[i] - '0') + 5) % 10;
}
int t;
t = a[0];
a[0] = a[3];
a[3] = t;
t = a[1];
a[1] = a[2];
a[2] = t;
System.out.println("結(jié)果是:" + a[0] + a[1] + a[2] + a[3]);
}
}
或
import java.util.*;
public class Demo48 {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int num=0,temp;
do{
System.out.print("請輸入一個4位正整數(shù):");
num = s.nextInt();
}while (num<1000||num>9999);
int a[]=new int[4];
a[0] = num/1000; //取千位的數(shù)字
a[1] = (num/100)%10; //取百位的數(shù)字
a[2] = (num/10)%10; //取十位的數(shù)字
a[3] = num%10; //取個位的數(shù)字
for(int j=0;j<4;j++) {
a[j]+=5; a[j]%=10;
}
for(int j=0;j<=1;j++) {
temp = a[j]; a[j] = a[3-j]; a[3-j] =temp;
}
System.out.print("加密后的數(shù)字為:");
for(int j=0;j<4;j++) System.out.print(a[j]);
}
}
【程序49】 題目:計算字符串中子串出現(xiàn)的次數(shù)。
import java.util.Scanner;
public class Demo49 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("請輸入主串:");
String str1 = in.nextLine();
System.out.println("請輸入子串:");
String str2 = in.nextLine();
// 生成子串長度的N個字符串?dāng)?shù)組
String[] sa = new String[str1.length() - str2.length() + 1];
for (int i = 0; i < sa.length; i++) {
sa[i] = str1.substring(i, i + str2.length());
}
int sum = 0;
// 子串與N個拆開的子串比對
for (int i = 0; i < sa.length; i++) {
if (sa[i].equals(str2)) {
// 成功配對关噪,計數(shù)器+1鸟蟹;
sum++;
// 因為不計算重疊的子串物舒,所以跳過配對之后的部分拆分子串
i = i + str2.length();
}
}
System.out.println("主串中共包含" + sum + "個字串");
}
}
【程序50】 題目:有五個學(xué)生,每個學(xué)生有3門課的成績戏锹,從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號冠胯,姓名,三門課成績)锦针,計算出平均成績荠察,把原有的數(shù)據(jù)和計算出的平均分數(shù)存放在磁盤文import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
class Student {
private int number = 0;
private String name = "";
private double[] a = new double[3];
public double getAve() {
return (a[0] + a[1] + a[2]) / 3;
}
public Student(int number, String name, double[] a) {
super();
this.number = number;
this.name = name;
this.a = a;
}
@Override
public String toString() {
return "學(xué)號:" + this.number + "\t姓名:" + this.name + "\r\n各科成績:\r\n" + a[0] + "\t" + a[1] + "\t" + a[2] + "\r\n平均成績:\r\n"
+ this.getAve();
}
}
public class Demo50 {
public static Student input() {
Scanner s = new Scanner(System.in);
System.out.println("請輸入學(xué)號:");
int num = s.nextInt();
System.out.println("請輸入姓名:");
String name = s.next();
System.out.println("請分別輸入3門成績:");
double[] a = new double[3];
for (int i = 0; i < 3; i++) {
a[i] = s.nextDouble();
}
return new Student(num, name, a);
}
public static void main(String[] args) throws Exception {
Student[] st = new Student[2];
for (int i = 0; i < st.length; i++) {
st[i] = input();
}
File f = new File("d:" + File.separator + "123.txt");
FileWriter output = new FileWriter(f);
for (int i = 0; i < st.length; i++) {
output.write(st[i].toString() + "\r\n");
output.write("\r\n");
}
output.close();
}
}
我的博客即將搬運同步至騰訊云+社區(qū),邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=2grqdg8faq80w