return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"\n";
}
}
【程序19】题目:打印出如下图案(菱形)
*
***
******
********
******
***
*
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。
三角形:
public class StartG { 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=4;i>=1;i--)
{ for(j=1;j<=2*i-3;j++)
System.out.print("*");
System.out.println("");
}
}
}
菱形:
public class StartG { 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 for .print(" "); (j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.println("");
}for(i=4;i>=1;i--)
{ for(int k=1; k<=5-i;k++)
for System.out.print(" "); (j=1;j<=2*i-3;j++)
System.out.print("*");
System.out.println("");
}
}
}
【程序20】题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。
public class test20 { public static void main(String[] args) { float fm = 1f; float fz = 1f; float temp; float sum = 0f; for (int i=0;i<20;i++){
temp = fm;
fm = fz;
fz = fz + temp;
sum += fz/fm;
//System.out.println(sum);
}
System.out.println(sum);
}
}
【程序21】题目:求1+2!+3!+...+20!的和
1.程序分析:此程序只是把累加变成了累乘。
public class Ex21 { static long sum = 0; static long fac = 0; public static void main(String[] args) { long sum = 0; long fac = 1; for(int i=1; i<=10; i++) {
fac = fac * i;
sum += fac;
}
}}
【程序22】题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
import java.util.Scanner; public class Ex22 { public static void main(String[] args) {
Scanner s = new Scanner(System.in); int n = s.nextInt();
Ex22 tfr = new Ex22();
System.out.println(tfr.recursion(n));
}
public long recursion(int n) { long value = 0 ; if(n ==1 || n == 0) {
value = 1;
} else if(n > 1) {
value = n * recursion(n-1);
}return value;
}}
【程序23】题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比
第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),
再往回推。
public class Ex23 { 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位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
import java.util.Scanner; public class Ex24 { public static void main(String[] args) {
Ex24 tn = new Ex24();
Scanner s = new Scanner(System.in); long a = s.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 + "是一位数");
System.out.println("按逆序输出是" + '\n' + a);
} else if(a >= 10 && a <= 99) {
System.out.println(a + "是二位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 100 && a <= 999) {
System.out.println(a + "是三位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 1000 && a <= 9999) {
System.out.println(a + "是四位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 10000 && a <= 99999) {
System.out.println(a + "是五位数");
System.out.println("按逆序输出是" );
tn.converse(a);
}
}public void converse(long l) {
String s = Long.toString char (l); [] ch = s.toCharArray(); for(int i=ch.length-1; i>=0; i--) {
System.out.print(ch[i]);
}
}}
【程序25】题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
import java.util.Scanner; public class Ex25 { static int[] a = new int[5]; static int[] b = new int[5]; public static void main(String[] args) { boolean is =false;
Scanner s = new Scanner(System.in); long l = s.next