package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序3】
* 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
* 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
* 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位
*/publicclass ProgramEx3 {
publicstaticvoid main(String[] args) {
for (int i=100; i<=999; i++) {
int a = i/100;
int b = i%100/10;
int c = i%10;
if (i == a*a*a + b*b*b + c*c*c) {
System.out.println(i);
}
}
}
}
第四题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序4】
* 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
* 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
* (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
* (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
* (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
*/publicclass ProgramEx4 {
publicstaticvoid main(String[] args) {
int k=90;
System.out.print("90=");
for (int n=2; n<=k; n++) {
if (k%n ==0) {
if (n != k) {
System.out.print(n+"*");
k = k/n;
n =2;
}
else {
//System.out.print("90="); System.out.print(k);
}
}
}
}
}
第五题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
* 【程序5】
* 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下
* 的用C表示。
* 1.程序分析:(a>b)?a:b这是条件运算符的基本例子
*/import java.io.*;
publicclass ProgramEx5 {
publicstaticvoid main(String[] args) throws Exception {
int m;
BufferedReader br =new BufferedReader (new InputStreamReader (System.in));
m = (int)br.read();
//char a = 'a';
//char b = 'b';
//char c = 'c';char n = (m<60)?"c":((m>=90)?"a":"b");
System.out.println(n);
}
}
第六题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序6】
* 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
* 1.程序分析:利用辗除法。
*/import java.util.Scanner;
publicclass ProgramEx6 {
publicstaticvoid main(String[] args) throws Exception {
System.out.print("请输入两个整数");
Scanner scan =new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
int a, b;
if (m<n) {
b = m;
m = n;
n = b;
}
a = m%n;
while(a!=0) {
m = n;
n = a;
a = m%n;
m = m*n;
}
System.out.println("最小公倍数为:"+m);
//System.out.println("最大公约数为:"+n); }
}
package com.supersoft.exercise;
/**
* @author JamesLiu
*
* 【程序11】
* 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
* 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
* 组成所有的排列后再去 掉不满足条件的排列。
*/publicclass ProgramEx11 {
publicstaticvoid main(String[] args) {
int sum =0;
int m =0;
for (int a=1; a<=4; a++) {
for (int b=1; b<=4; b++) {
if (a!=b) {
for (int c=1; c<=4; c++) {
if (b!=c && a!=c) {
m = a*100+ b*10+c;
sum = sum +1;
System.out.print(m+"");
//System.out.print(a+""+b+""+c); }
}
}
}
}
System.out.println("一共有"+sum+"个");
}
}
第十二题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序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,求应发放奖金总数?
* 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
*/import java.util.Scanner;
publicclass ProgramEx12 {
publicstaticvoid main(String[] args) {
System.out.print("请输入利润(注:利润>0):");
Scanner scanner =new Scanner(System.in);
int m = scanner.nextInt();
long n =0L;
if (m<=10) {
n = (long)(0.1* m);
}
elseif (m<=20) {
n = (long)(1+ (m-10)*0.075);
}
elseif (m<=40) {
n = (long)(1.75+ (m-20)*0.05);
}
elseif (m<=60) {
n = (long)(1.85+ (m-40)*0.03);
}
else {
n = (long)(1.91+ (m-60)*0.01);
}
System.out.print("所得奖金为:"+n+"万元");
}
}
第十三题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序13】
* 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
* 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足
* 如下条件,即是结果。
*/publicclass ProgramEx13 {
publicstaticvoid main(String[] args) {
long x, y;
for (long i=1; i<100000;i++) {
x = (long)Math.sqrt(i+100);
y = (long)Math.sqrt(i+268);
if (x*x == (i+100) && y*y == (i+268)) {
System.out.println(i);
}
}
}
}
第十四题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序14】
* 题目:输入某年某月某日,判断这一天是这一年的第几天?
* 1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且
* 输入月份大于3时需考虑多加一天。
*/import java.util.Scanner;
publicclass ProgramEx14 {
publicstaticvoid main(String[] args) {
System.out.print("请输入年year:");
Scanner scanner =new Scanner(System.in);
int y = scanner.nextInt();
System.out.print("请输入月month:");
int m = scanner.nextInt();
System.out.print("请输入日day:");
int d = scanner.nextInt();
int sum =0;
for (int i=1; i<m; i++) {
if ((i==4) || (i==6) || (i==9) || (i==11)) {
sum = sum +30;
}
elseif (i==2) {
if (((y%4==0&& y%100!=0) || (y%400==0))) {
sum = sum +29;
}
else {
sum = sum +28;
}
}
else {
sum = sum +31;
}
}
sum = sum + d;
System.out.print("这一天是这一年中的第"+sum+"天");
}
}
/*
import java.util.Scanner;
public class ProgramEx14 {
public static boolean isValidate(int y, int m, int d, int[] monthOfDays) {
if (y<=0) {
return false;
}
if (m<=0 || m>12) {
return false;
}
if (d<=0 || d>monthOfDays[m-1]) {
return false;
}
return true;
}
public static void main(String[] args) {
System.out.print("请输入年year:");
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
System.out.print("请输入月month:");
int m = scanner.nextInt();
System.out.print("请输入日day:");
int d = scanner.nextInt();
int[] monthOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (((y%4==0 && y%100!=0) || (y%400==0))) {
monthOfDays[1] = 29;
}
if (isValidate(y, m, d, monthOfDays) == false) {
System.out.println("");
return;
}
int sum = 0;
for (int i=0; i<m-1; i++) {
sum = sum + monthOfDays[i];
}
sum = sum + d;
System.out.println("这一天是这一年的第"+sum+"天");
}
}
*/
第十五题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序15】
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
* 1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x
* 与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
*/import java.util.Scanner;
publicclass ProgramEx15 {
publicstaticvoid main(String[] args) {
System.out.println("请输入三个整数x,y,z:");
Scanner scanner =new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
if (x>y) {
int m = x;
x = y;
y = m;
}
if (x>z) {
int p = x;
x = z;
z = p;
}
if (y>z) {
int n = y;
y = z;
z = n;
}
System.out.print(x+","+y+","+z);
}
}
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序19】
* 题目:打印出如下图案(菱形)
* *
* ***
* *****
* *******
* *****
* ***
* *
* 1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制
* 行,第二层控制列。
*
public class ProgramEx19 {
public static void main(String[] args) {
for (int i=1; i<=4; i++) {
for (int j=1; j<=4-i; j++) {
System.out.print(" ");
}
for (int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i=3; i>=1; i--) {
for (int j=1; j<=4-i; j++) {
System.out.print(" ");
}
for (int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
*/publicclass ProgramEx19 {
publicstaticvoid main(String[] args) {
int n =0;
for (int i=0; i<7; i++) {
for (int j=0; j<4+n; j++) {
if (j<3-i || j<i-3) {
System.out.print("");
}
else {
System.out.print("*");
}
}
if (i<3) {
n++;
}
else {
n--;
}
System.out.println();
}
}
}
第二十题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序20】
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
* 1.程序分析:请抓住分子与分母的变化规律。
*/publicclass ProgramEx20 {
publicstaticvoid main(String[] args) {
double result =0;
double a =2.0;
double b =1.0;
for (int i=1; i<=20; i++) {
result = result + a/b;
double m = b;
b = a;
a = a+m;
}
System.out.print(result);
}
}
第二十一题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序21】
* 题目:求1+2!+3!+...+20!的和
*/publicclass ProgramEx21 {
publicstaticvoid main(String[] args) {
int result =0;
int m =1;
for (int i=1; i<=20; i++) {
m = m*i;
result = result + m;
}
System.out.print(result);
}
}
第二十二题
Java code
package com.supersoft.exercise;
/**
* @author JamesLiu
*
*【程序22】
题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
*
public class ProgramEx22 {
public static void main(String[] args) {
int m = 1;
for (int i=1; i<=5; i++) {
m = m * i;
}
System.out.print(m);
}
}
*/publicclass ProgramEx22 {
publicstaticvoid main(String[] args) {
System.out.print(f(5));
}
publicstaticint f(int n) {
if (n==0) {
return1;
}
return n*f(n-1);
}
}