史上最强悍90道Java基础题附10题(含完整答案)

来源:互联网 发布:路由器连接上没有网络 编辑:程序博客网 时间:2024/06/18 02:50

说明

本文档由吴江波同学独自编写整合而成,希望对广大Java爱好者有所帮助,本人还属于Java菜鸟级别,所以,如发现有任何不当或错误之处,还望指正,交流QQ:1134135987,谢谢!

1.完成数组int[] a = {100,40, 60, 87, 34, 11, 56, 0}的快速排序、冒泡排序;

快速排序

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int[] a = newint[]{100,40, 60, 87, 34, 11, 56, 0};

       System.out.println("未排序之前的数据是:");

       print(a);

       System.out.println("排序之后的数据是:");

       sort(a,0,a.length-1);

       print(a);

      

    }

    //打印方法

    public static void print(int[] b){

       for(int i=0; i<b.length; i++){

           System.out.print(b[i]+" ");

       }

       System.out.println();

    }

    //排序方法

    static void sort(int[] a,int low,int high){

       if(low >= high) return;//low小于high,则直接返回

       if((high - low)==1){//如果只有两个数字,则直接比较

           if(a[0] > a[1])

              swap(a,0,1);

           return;

       }

       int pivot = a[low];//取第一个数作为中间数

       int left = low +1;

       int right = high;

       while(left<right){

           //从左边开始找

           while(left < right && left <= high){//如果左小于右则一直循环

              if(a[left] > pivot)break;

              left++;//左下标往右边走一点

           }

           //从右边开始找

           while(left <= right && right > low){//如果左大于右则一直循环

              if(a[right] <= pivot)

                  break;

              right--;//右下标往左走一点

           }

           if(left < right)//如果还没有找完,则交换数字

              swap(a,right,left);

       }

       swap(a,low,right);

       sort(a,low,right);

       sort(a,right+1,high);

    }

    //调位方法

    private static void swap(int[] array,int i, int j){

       int temp;

       temp = array[i];

       array[i] = array[j];

       array[j] = temp;

    }

}

打印结果为:

未排序之前的数据是:

100 40 60 87 34 11 56 0

排序之后的数据是:

0 11 34 40 56 60 87 100

 

冒泡排序

实现代码:

public class Test002 {

    public static void main(String[] args) {

       int[] arr = {100,40, 60, 87, 34, 11, 56, 0};//定义数组

       System.out.println("未排序之前的数据是:");

       maopaoPrint(arr);

       System.out.println();

       System.out.println("排序之后的数据是:");

       maopaoSort(arr);

    }

    //排序方法

    public static void maopaoSort(int[] arrys){

       //定义临时变量temp

       int temp = 0;

       //j表示下标,遍历数组

       for(int j=0; j<arrys.length; j++){

           //对于每一个数组元素,从0到还未排序的最大下标,总是把最大的数字放在后边

           for(int k=0; k<arrys.length-j-1; k++){

              if(arrys[k]>arrys[k+1]){//判断当前数字与后面数字的大小

                  temp = arrys[k];

                  arrys[k] = arrys[k+1];

                  arrys[k+1] = temp;

              }

           }

       }

       maopaoPrint(arrys);//打印输出

    }

    //打印方法

    public static void maopaoPrint(int[] l){

       for(int i=0; i<l.length; i++){

           System.out.print(l[i]+" ");//从小到大的输出

       }

    }

}

打印结果为:

未排序之前的数据是:

100 40 60 87 34 11 56 0

排序之后的数据是:

0 11 34 40 56 60 87 100

 

2.采用折半查找的算法,在数组中查询到某个数;

实现代码:

import java.util.Scanner;

public class Test003 {

    public static int Max = 20;

    // 数据数组源

    public static int data[] = { 12, 16, 19, 22, 25, 32, 39,39, 48, 55, 57, 58,

           63, 68, 69, 70, 78, 84, 88, 90, 97 };

    // 计数器

    public static int count = 1;

    public static void main(String[] args) {

       System.out.println("请输入您要查找的数字:");

       Scanner sc = new Scanner(System.in);

       int KeyValue = sc.nextInt();

       // 调用折半查找

       if (Search(KeyValue)) {

           // 输出查找次数

           System.out.println("共查找了" +count + "");

       } else {

           // 输出没有找到数据

           System.out.println("抱歉,数据数组源中找不到您输入的数字");

       }

    }

    //折半查找法

    public static boolean Search(int k) {

       int left = 0;//左边界变量

       int right = Max - 1;//右边界变量

       int middle;//中位数变量

       while (left <= right) {

           middle = (left + right) / 2;

           if (k < data[middle]) {

              right = middle - 1;//查找前半段

           } else if (k > data[middle]) {

              left = middle + 1;//查找后半段

           } else if (k == data[middle]) {

              System.out.println("Data[" + middle +"] = " + data[middle]);

              return true;

           }

           count++;

       }

       return false;

    }

}

3.输入一个字符串,其中包含数字、特殊符号(像:¥、&、(、>等)、大小写字母等,然后输出每个字符串或符号的ASCII码的和;例如:输入“@#$%^&*():"|”,则打印出643。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       System.out.println("请输入一个字符串:");

       Scanner sc = new Scanner(System.in);

       String str = sc.nextLine();

       int sum = 0;

       for(int i=0; i<str.length(); i++){

           sum = sum+str.charAt(i);

       }

       System.out.println("您输入的字符串每个字节相加的和为:"+sum);

    }

}

 

4. 将一个数组中值=0的项去掉,将不为0的值存入一个新的数组

比如:

int a[]={0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

生成的新数组为:

intb[]={1,3,4,5,6,6,5,4,7,6,7,5}

实现代码:

import java.util.*;

public class Test001 {

    public static void main(String[] args) {

       int a[]={0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

       List<Integer> list = new ArrayList<Integer>();

       for(int i =0;i<a.length;i++){

           if(a[i]!=0){

              list.add(a[i]);

           }

       }

       int b[] = newint[list.size()];

       for(int i = 0;i<list.size();i++){

           b[i] = list.get(i);

       }

       System.out.println("原数组为:");

       for(int i =0;i<a.length;i++){

           System.out.print(a[i]+" ");

       }

       System.out.println();

       System.out.println("去掉值为0的项之后为:");

       for(int i:b){

           System.out.print(i+" ");

       }

    }

}

 

5. 定义10个长度的Student数组,将10个Student对象的年龄全部加1,然后把10个Student对象的详细信息逐行打印出来(数组和ArrayList实现)

实现代码:

第一个类:

public class Student {

    public String name;

    public String sex;

    public int age;

   

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getSex() {

       return sex;

    }

    public void setSex(String sex) {

       this.sex = sex;

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

    public Student(String name, String sex,int age) {

       super();

       this.name = name;

       this.sex = sex;

       this.age = age;

    }

}

第二个类:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    static Student[] s =new Student[10];

    int k = 1;

    public static void main(String[] args) {

       List<Student> li = new ArrayList<Student>();

       for(int i=0; i<10; i++){

           li.add(new Student("zhangsan"+i,"",20));

       }

       for(int i=0; i<10; i++){

           (li.get(i).age)++;

       }

       for(int i=0; i<10; i++){

           System.out.println(li.get(i).getName()+" "+li.get(i).getSex()+" "+li.get(i).getAge());

       }

    }

}

 

6. 有工人,农民,教师,科学家,服务生,其中,工人,农民,服务生只有基本工资.教师除基本工资外,还有课酬(元/天),科学家除基本工资外,还有年终奖,请你写出相关类,将各种类型的员工的全年工资打印出来

实现代码:(共有7个类)

第一个类:

package com.softeem.zy006;

/**

 * 定义一个人的接口,以供实现

 */

public interface People{

    public double num();

}

第二个类:

package com.softeem.zy006;

/**

 * 工人类

 */

public class Worker implements People{

    private double montherSalary;

    public Worker(double montherSalary) {

       super();

       this.montherSalary = montherSalary;

    }

    public double num() {

       return getMontherSalary()*12;

    }

    public double getMontherSalary() {

       return montherSalary;

    }

    public void setMontherSalary(double montherSalary) {

       this.montherSalary = montherSalary;

    }

}

第三个类:

package com.softeem.zy006;

/**

 * 农民类

 */

public class Peasant implements People{

    private double montherSalary;

    public Peasant(double montherSalary) {

       super();

       this.montherSalary = montherSalary;

    }

    public double getMontherSalary() {

       return montherSalary;

    }

    public void setMontherSalary(double montherSalary) {

       this.montherSalary = montherSalary;

    }

    public double num() {

       return getMontherSalary()*12;

    }

}

第四个类:

package com.softeem.zy006;

/**

 * 教师类

 */

public class Teacher implements People{

    private double montherSalary;

    private double daySalary;

   

    public Teacher(double montherSalary,double daySalary) {

       super();

       this.montherSalary = montherSalary;

       this.daySalary = daySalary;

    }

    public double num() {

       return getMontherSalary()*12+getDaySalary()*365;

    }

    public double getMontherSalary() {

       return montherSalary;

    }

    public void setMontherSalary(double montherSalary) {

       this.montherSalary = montherSalary;

    }

    public double getDaySalary() {

       return daySalary;

    }

    public void setDaySalary(double daySalary) {

       this.daySalary = daySalary;

    }

}

第五个类:

package com.softeem.zy006;

/**

 * 科学家类

 */

public class Scientist implements People{

    private double montherSalary;

    private double projectSalary;

    public Scientist(double montherSalary,double projectSalary) {

       super();

       this.montherSalary = montherSalary;

       this.projectSalary = projectSalary;

    }

    public double num(){

       return getMontherSalary()*12+getProjectSalary();

    }

    public double getMontherSalary() {

       return montherSalary;

    }

    public void setMontherSalary(double montherSalary) {

       this.montherSalary = montherSalary;

    }

    public double getProjectSalary() {

       return projectSalary;

    }

    public void setProjectSalary(double projectSalary) {

       this.projectSalary = projectSalary;

    }

}

第六个类:

package com.softeem.zy006;

/**

 * 服务员类

 */

public class Waiter implements People{

    private double montherSalary;

    public Waiter(double montherSalary) {

       super();

       this.montherSalary = montherSalary;

    }

    public double num() {

       return getMontherSalary()*12;

    }

    public double getMontherSalary() {

       return montherSalary;

    }

    public void setMontherSalary(double montherSalary) {

       this.montherSalary = montherSalary;

    }

}

第七个类:

package com.softeem.zy006;

/**

 * 测试类

 */

public class Test {

    public static void main(String[] args) {

       Test a = new Test();

       Worker w = new Worker(1000);

       System.out.println("工人的年薪为:" + w.num()+"");

      

       Peasant p = new Peasant(2500);

       System.out.println("农民的年薪为:"+p.num()+"");

      

       Teacher t = new Teacher(4500,50);

       System.out.println("教师的年薪为:"+t.num()+"");

 

       Scientist e = new Scientist(10500,30000);

       System.out.println("科学家的年薪为:" + e.num()+"");

 

       Waiter y = new Waiter(3400);

       System.out.println("服务生的年薪为:" + y.num());

    }

}

打印结果为:

工人的年薪为:12000.0元

农民的年薪为:30000.0元

教师的年薪为:72250.0元

科学家的年薪为:156000.0元

服务生的年薪为:40800.0

7. 创建一个复数类complex,对复数进行数学运算,复数具有如下格式:

RealPart+ImaginaryPart*I

其中,I为-1的平方根。

要求如下:

(1)利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明时对象的初始化;一个为带默认值得无参构造方法。

(2)提供两复数加、减、乘的运算方法。

(3)按格式(a,b)打印复数。其中a为实部,b为虚部。

实现代码:

package com.softeem.zy007;

/**

 * 创建一个复数类complex,对复数进行数学运算,复数具有如下格式: RealPart+ImaginaryPart*I其中,I为-1的平方根。

 * 要求如下:(1)利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明时对象的初始化;一个为带默认值得无参构造方法。

 * (2)提供两复数加、减、乘的运算方法。(3)按格式(a,b)打印复数。其中a为实部,b为虚部。

 */

public class ComplexNumber implements Cloneable {

    /** 复数的实部 */

    private double realPart;

 

    /** 复数的虚部 */

    private double imaginaryPart;

 

    /** 默认构造函数 */

    public ComplexNumber() {

       this.realPart = 0.0;

       this.imaginaryPart = 0.0;

    }

 

    /**

     * 构造函数

     *

     * @param a

     *            实部

     * @param b

     *            虚部

     */

    public ComplexNumber(double a,double b) {

       this.realPart = a;

       this.imaginaryPart = b;

    }

 

    /**

     * 复数的加法运算。 c = a + b的运算法则是: c.实部 = a.实部 + b.实部; c.虚部 = a.虚部 + b.虚部

     *

     * @param aComNum

     *            加数

     * @return

     */

    public ComplexNumber add(ComplexNumber aComNum) {

       if (aComNum == null) {

           System.err.println("对象不能够为null");

           return new ComplexNumber();

       }

       return new ComplexNumber(this.realPart + aComNum.getRealPart(),

              this.imaginaryPart + aComNum.getImaginaryPart());

    }

 

    /**

     * 复数的减法运算。 c = a- b的运算法则是: c.实部 = a.实部- b.实部; c.虚部 = a.虚部- b.虚部

     *

     * @param aComNum

     *            减数

     * @return

     */

    public ComplexNumber decrease(ComplexNumber aComNum) {

       if (aComNum == null) {

           System.err.println("对象不能够为null");

           return new ComplexNumber();

       }

       return new ComplexNumber(this.realPart - aComNum.getRealPart(),

              this.imaginaryPart - aComNum.getImaginaryPart());

    }

 

    /**

     * 复数的乘法运算。 c = a * b的运算法则是: c.实部 = a.实部 * b.实部- a.虚部 * b.虚部; c.虚部 = a.虚部 *

     * b.实部 + a.实部 * b.虚部;

     *

     * @param aComNum

     *            乘数

     * @return

     */

    public ComplexNumber multiply(ComplexNumber aComNum) {

       if (aComNum == null) {

           System.err.println("对象不能够为null");

           return new ComplexNumber();

       }

       double newReal = this.realPart * aComNum.realPart -this.imaginaryPart

              * aComNum.imaginaryPart;

       double newImaginary =this.realPart * aComNum.imaginaryPart

              + this.imaginaryPart * aComNum.realPart;

       ComplexNumber result = new ComplexNumber(newReal, newImaginary);

       return result;

    }

 

    /**

     * 复数的除法运算。 c = a / b的运算法则是: c.实部 = (a.实部 * b.实部 + a.虚部 * b.虚部) / (b.实部

     * *b.实部 + b.虚部 * b.虚部); c.虚部 = (a.虚部 * b.实部- a.实部 * b.虚部) / (b.实部 * b.实部 +

     * b.虚部 * b.虚部);

     *

     * @param aComNum

     *            除数

     * @return

     */

    public ComplexNumber divide(ComplexNumber aComNum) {

       if (aComNum == null) {

           System.err.println("对象不能够为null");

           return new ComplexNumber();

       }

       if ((aComNum.getRealPart() == 0) && (aComNum.getImaginaryPart() == 0)) {

           System.err.println("除数不能够为0");

           return new ComplexNumber();

       }

 

       double temp = aComNum.getRealPart() * aComNum.getRealPart()

              + aComNum.getImaginaryPart() * aComNum.getImaginaryPart();

       double crealpart = (this.realPart * aComNum.getRealPart() +this.imaginaryPart

              * aComNum.getImaginaryPart())

              / temp;

       double cimaginaryPart = (this.imaginaryPart * aComNum.getRealPart() -this.realPart

              * aComNum.getImaginaryPart())

              / temp;

       return new ComplexNumber(crealpart, cimaginaryPart);

    }

 

    /**

     * 将一个复数显示为字符串

     */

    public String toString() {

       return this.realPart + " + " + this.imaginaryPart +"i";

    }

 

    /**

     * 比较一个对象是否和这个复数对象的值相等

     */

    public boolean equals(Object obj) {

       if (obj == null) {

           return false;

       }

       // 首先判断a是不是一个复数对象,instanceof关键字是用来判断对象的类型。

       if (obj instanceof ComplexNumber) {

           // 如果a是复数对象,需要将它强制类型转换成复数对象,才能调用复数类提供的方法。

           ComplexNumber b = (ComplexNumber) obj;

           if ((this.realPart == b.getRealPart())

                  && (this.imaginaryPart == b.getImaginaryPart())) {

              return true;

           } else {

              return false;

           }

       } else {

           return false;

       }

    }

 

    /**

     * 获得该复数对象的hashcode

     */

    public int hashCode() {

       // 如果两个复数对象是equals的,那么它们的hashCode也必须相同。

       // 两个值相等的复数对象通过toString()方法得到的输出字符串是一样的,

       // 于是,可以把得到的字符串的hashCode当作复数对象的hashCode

       return this.toString().hashCode();

    }

 

    /**

     * 根据现有对象克隆一个新对象

     */

    public Object clone() {

       // 如果你要使自定义的类能够被clone,就必须实现Cloneable接口并且重写它的clone()方法.

       // 如果你仅仅重写了clone方法而没有在类的声明中添加实现Cloneable接口,调用clone方法时将会出现

       // CloneNotSupportedException异常,读者可以试试。

       try {

           ComplexNumber newObject = (ComplexNumber) super.clone();

           newObject.setRealPart(this.realPart);

           newObject.setImaginaryPart(this.imaginaryPart);

           return newObject;

       } catch (CloneNotSupportedException e) {

           // //如果没有实现Cloneable接口,抛出异常

           e.printStackTrace();

           return null;

       }

    }

 

    /**

     * @return返回 imaginaryPart

     */

    public double getImaginaryPart() {

       return imaginaryPart;

    }

 

    /**

     * @param imaginaryPart

     *            要设置的 imaginaryPart

     */

    public void setImaginaryPart(double imaginaryPart) {

       this.imaginaryPart = imaginaryPart;

    }

 

    /**

     * @return返回 realPart

     */

    public double getRealPart() {

       return realPart;

    }

 

    /**

     * @param realPart

     *            要设置的 realPart

     */

    public void setRealPart(double realPart) {

       this.realPart = realPart;

    }

 

    public static void main(String[] args)throws CloneNotSupportedException {

       ComplexNumber a = new ComplexNumber(3, 5);

       ComplexNumber b = new ComplexNumber(2, 4);

       System.out.println("ComplexNumber a: " + a.toString());

       System.out.println("ComplexNumber b: " + b.toString());

       System.out.println("(a + b) = " + a.add(b).toString());

       System.out.println("(a - b) = " + a.decrease(b).toString());

       System.out.println("(a * b) = " + a.multiply(b).toString());

       System.out.println("(a / b) = " + a.divide(b).toString());

    }

}

 

8. 实现圆类circle,包含相关的成员变量和成员方法。从圆类派生出圆柱类cylinder。根据建立的两个类,从键盘输入5个圆的半径,5个圆柱的半径和高度,并分别是输出5个圆的面积,5个圆柱的体积

实现代码:(共有三个类)

第一个类:

package com.softeem.zy008;

public class Circle {

    private static final float f = (float)Math.PI;

    /**

     * 计算出圆的面积

     */

    public float getArea(float rr){

       float area = f * rr *rr;

       return area;

    }

}

第二个类:

package com.softeem.zy008;

public class Cylinder extends Circle{

    public float getVolume(float rr,float g){

       float area = super.getArea(rr);

       float volume = area*g;

       return volume;

    }

}

第三个类(测试类):

package com.softeem.zy008;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

       Test t = new Test();

       t.Demo001();

       System.out.println();

       t.Demo002();

    }

    public void Demo001() {

       System.out.println("计算圆的面积:");

       System.out.println("请输入5个圆的半径(一行写一个半径):");

       float[] f = new float[5];

       for (int i = 0; i <= 4; i++) {

           Scanner sc = new Scanner(System.in);

           f[i] = sc.nextFloat();

       }

       for (int i = 0; i < f.length; i++) {

           Circle c = new Circle();

           float area = c.getArea(f[i]);

           System.out.println("半径为" + f[i] +"的圆的面积为:" + area);

       }

    }

    public void Demo002() {

       System.out.println("接下来是计算圆柱的体积:");

       System.out.println("请先输入5个圆柱的半径(一行写一个):");

       float[] f1 = new float[5];

       for (int i = 0; i <= 4; i++) {

           Scanner sc = new Scanner(System.in);

           f1[i] = sc.nextFloat();

       }

       System.out.println("请再分别输入5个圆柱对应的高度(一行写一个):");

       float[] f2 = new float[5];

       for (int i = 0; i <= 4; i++) {

           Scanner sc = new Scanner(System.in);

           f2[i] = sc.nextFloat();

       }

       for (int i = 0; i < 5; i++) {

           Cylinder c = new Cylinder();

           float volume = c.getVolume(f1[i], f2[i]);

           System.out.println("半径为"+f1[i]+",高为"+f2[i]+"的圆柱的体积为:"+volume);

       }

    }

}

 

 

9. 输入一个整数,求这个整数中每位数字相加的和

实现代码:

package com.softeem.zy009;

import java.util.Scanner;

/**

 * 输入一个整数,求这个整数中每位数字相加的和

 */

public class Test001 {

    public static void main(String[] args) {

       System.out.println("请输入一个整数:");

       Scanner sc = new Scanner(System.in);

       String s = sc.nextLine();

       int sum = 0;

       for(int i=0; i<s.length(); i++){

           sum = sum+Integer.parseInt(s.valueOf(s.charAt(i)));

       }

       System.out.println("您输入的整数是:"+s);

       System.out.println("各位数字相加的和为:"+sum);

    }

}

 

10. 编写一个java应用程序,要求如下

(1)声明一个String类的变量并初始化值“HelloWorld”。

(2)用字符串类的一个方法将上面变量的值拆分成” Hello”和“World”两个字符串并打印输出。

(3)将” Hello”这个变量转换成大写、“World”转换成 小写并打印输出。

(4)声明一个String类的变量并初始化值“20100110”。

(5)将上面变量的值转换成2010年1月10日的形式打印输出。

实现代码:

public class Test001 {

    public static final String str1 ="Hello World";

    public static final String str2 ="20100110";

    public static void main(String[] args) {

       Test001 t = new Test001();

       t.Demo();

    }

    public void Demo(){

       String[] s = str1.split(" ");

       System.out.println("拆分后为:"+s[0]+""+s[1]);

       System.out.println(s[0]+"转换成大写之后为:"+s[0].toUpperCase());

       System.out.println(s[1]+"转换成小写之后为:"+s[1].toLowerCase());

       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

       System.out.println(str2.substring(0, 4)+""+str2.substring(4,6)+""+str2.substring(6)+"");

    }

}

 

11. 求s=1+3+5+7+...直到s>2000为止

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum  = 0;

       for(int i=1;;i=i+2){

           sum = sum +i;

           System.out.println(sum);

           if(sum>2000){

              break;

           }

       }

       System.out.println("程序停止:和已经大于2000");

    }

}

 

12. 计算s=2!+4!+8!+11!。(首先先定义一个函数,函数的功能就是求任何一个数的阶乘)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       Test001 t = new Test001();

       int sum = t.getJieCheng(2)+t.getJieCheng(2)+t.getJieCheng(4)+t.getJieCheng(8)+t.getJieCheng(11);

       System.out.println("2!+4!+8!+11! = "+sum);

    }

    //求任何一个数的阶乘

    public int getJieCheng(int m){

       int f = 1;

       for(int i=1; i<=m; i++){

           f = i*f;

       }

       return f;

    }

}

 

13. 求200到400间,能被3整除但不能被7整除的数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       System.out.println("能被3整除但不能被7整除的数有:");

       for(int i=200; i<=400; i++){

           if(i%3==0 && i%7!=0){

              System.out.print(i+" ");

              count++;

           }

       }

       System.out.println();

       System.out.print("能被3整除但不能被7整除的数的个数有 "+ count+"");

    }

}

 

14. 求能被3整除且至少有一位数字为5的三位数的个数

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       List<Integer> li = new ArrayList<Integer>();

       for (int i = 100; i <= 999; i++) {

           li.add(i);

       }

       System.out.println("能被3整除且至少有一位数字为5的三位数有:");

       for (int i = 100; i < li.size(); i++) {

           if (li.get(i) % 3 == 0) {

              if (li.get(i).toString().substring(2).equals("5")

                     || li.get(i).toString().substring(1, 2).equals("5")

                     || li.get(0).toString().substring(0, 1).equals("5")) {

                  System.out.print(li.get(i)+" ");

                  count ++;

              }

           }

       }

       System.out.println("\r\n能被3整除且至少有一位数字为5的三位数的个数有 "+count+" ");

    }

}

打印结果为:

能被3整除且至少有一位数字为5的三位数有:

225 252 255 258 285 315 345 351 354 357 375 405 435 450 453 456 459 465 495 525 552 555 558 585 615 645 651 654 657 675 705 735 750 753 756 759 765 795 825 852 855 858 885 915 945 951 954 957 975

能被3整除且至少有一位数字为5的三位数的个数有 49 个

 

15. 求三位奇数中,个位数字与十位数字之和除以10所得的余数是百位数字的数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       int a = 0,aa = 0,aaa = 0;

       for(int i=100; i<=999; i++){

           if(i%2 == 1){

              a = i%10;//个位

              aa = (i/10)%10;//十位

              aaa = (i/10)/10;//百位

              if(aaa == ((a+aa)%10)){

                  count ++;

              }

           }

       }

       System.out.println("所求个数为:"+count+"");

    }

}

 

16. 解百马百瓦古题。

大、小马和马驹共100匹,共驮100片瓦。大马一驮三,小马一驮二,马驹二驮一,一次驮完,三种马都驮,共有多少种组合?

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int i = 1;i<=98;i++){

           for(int j = 1;j<=98;j++){

              for(int k = 1;k<=98;k++){

                  if(i+j+k==100 && k%2==0 && i*3+j*2+k/2*1==100){

                     System.out.println("大马:"+i+"  "+"小马:"+j+"  "+"马驹:"+k+"  ");

                  }

              }

           }

       }

    }

}

 

17. 求100-200之间的所有素数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       Test001 s = new Test001();

       s.Method1();

    }

    //判断101-200之间有多少个素数,并输出所有素数

    public void Method1() {

       int count = 0;

       System.out.println("100-200之间的素数有:");

       for (int i = 101; i <= 200; i++) {

           boolean flag = true;

           for (int j = 2; j <= Math.sqrt(i); j++) {

              if (i % j == 0) {

                  flag = false;

                  break;

              }

           }

           if (flag) {

              count++;

              System.out.print(i +"  ");

           }

       }

       System.out.println("\n共有" + count +"");

}

}

 

18. 输出水仙花的个数

(所谓水仙花数是指一个三位十进制数,该数的各位数字立方之和等于该数本身。例如153是一个水仙花数,因为1^3+5^3+3^3=153)

实现代码:

public class Test001 {

    static int b, bb,bbb;

    public static void main(String[] args) {

       System.out.println("水仙花数有:");

       int count = 0;

       for (int num = 101; num < 1000; num++) {

           bbb = num / 100;

           bb = (num % 100) / 10;

           b = (num % 100) % 10;

           if (bbb *bbb * bbb +bb * bb *bb + b *b * b == num) {

              count ++;

              System.out.println(num);

           }

       }

       System.out.println("共有 "+count+"");

    }

}

 

19. 猴子摘桃问题

猴子第1天摘下若干桃子,当即吃掉一半,又多吃一个,第二天将剩余的部分吃掉一半还多一个;以此类推,到第10天只剩余1个。问第1天共摘了多少桃子

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int a = 1;//第十个月的桃子个数

       int b = 10;//b-1来表示所经过的月数

       for (int i = b - 1; i >= 1; i--) {

           a = (a + 1) * 2;

       }

       System.out.print("第一天共摘了"+a+"个桃子");

    }

}//打印结果为:第一天共摘了1534个桃子

 

20. 一个两位数的正整数,如果将其个位数与十位数字对调所生成的数称为其对调数,如28是82的对调数。现给定一个两位的正整数46,请找到另一个两位的整数,使这两个数之和等于它们的各自的对调数之和。这样的另一个两位数有多少个。

实现代码:

public class Test001 {

    static int demo = 46;

    public static void main(String[] args) {

       int demoSum = (demo/10)+(demo%10)*10;

       int count = 0;

       System.out.println("这样的两位数有:");

       for(int i=10; i<100; i++){

           int shi = i/10;

           int ge = (i%10)*10;

           int sum = shi+ge;

           if((demo+i) == (demoSum+sum)){

              count ++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有"+count+"");

    }

}

 

21. 求1~200之间的能被7整除的数的平方和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=200; i++){

           if(i%7==0){

              sum = sum+(i*i);

           }

       }

       System.out.println("1200之间的能被7整除的数的平方和 = "+sum);

    }

}

 

22. 求1~99的平方根的和并输出结果。(保留小数点两位)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double sum = 0;

       for(int i=1; i<=99; i++){

           sum = sum + Math.sqrt(i);

       }

       System.out.println("1~99的平方根的和"+sum);

    }

}

 

23. 求[351,432]之间既不能被3整除,也不能被8整除的正整数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       System.out.println("[351,432]之间既不能被3整除,也不能被8整除的正整数有:");

       for(int i=351; i<=432; i++){

           if(i%3 != 0 && i%8 != 0){

              count ++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有:"+count+"");

    }

}

 

24. 已知24有8个正整数因子

(即:1,2,3,4,6,8,12,24),而24正好被其因子个数8整除。问[100,300]之间有多少个这样的数。

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    public static void main(String[] args) {

       int k;

       int flag = 0;

       int size = 0;

       System.out.println("[100,300]之间符合题意的正整数有:");

       for (int n = 100; n <= 300; n++) {

           List<Integer> list = new ArrayList<Integer>();

           for (int i = 1; i <= n; i++) {

              if (n % i == 0) {

                  list.add(i);

              }

           }

           size = list.size();

           if (n % size == 0) {

              System.out.print(n+" ");

              flag++;

           }

       }

       System.out.println("\r\n共有 "+flag+"");

    }

}

 

 

 

 

 

 

扩展:

求任意一个正整数的正整数因子

要求:用户任意输入一个int型的正整数,然后程序打印出此正整数的所有因子,

如:用户输入24,则打印出:1 2 3 4 6 8 12 24

实现代码:

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Test002 {

    public static void main(String[] args) {

       System.out.println("请输入一个正整数:");

       Scanner sc = new Scanner(System.in);

       int num = sc.nextInt();

       List<Integer> list = getNum(num);//将用户输入的正整数传到getNum方法中进行处理,返回一个list集合,此集合中就是符合题意的因子

       System.out.println(num+"的正整数因子有:");

       for (Integer i : list) {

           System.out.print(i + " ");

       }

    }

    public static List getNum(int demo) {

       List<Integer> list = new ArrayList<Integer>();

       int t = 1;

       for (int i = 1; i <= demo; i++) {

           if (demo % i == 0) {

              list.add(i);

           }

       }

       return list;

    }

}

 

25.多因子完备数

若某整数N的所有因子之和等于N的倍数,则N称为多因子完备数,如数28,其因子之和1+2+4+7+14+28=56=2*28,28是多因子完备数。求[1,200]之间有多少个多因子完备数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("[1,200]之间的多因子完备数有:");

       for(int n=1; n<=200; n++){

           List<Integer> list = new ArrayList<Integer>();

           int sum = 0;

           for (int i = 1; i <= n; i++) {

              if (n % i == 0) {

                  list.add(i);

              }

           }

           for(int j=0; j<list.size();j++){

              sum = sum + list.get(j);

           }

           if(sum % n == 0){

              System.out.print(n+" ");

              flag++;

           }

       }

       System.out.println("\r\n[1,200]之间的多因子完备数共有:"+flag+"");

    }

}

 

扩展:

任意输入一个正整数,判断其是否是多因子完备数,且输出所有的因子,不管是不是多因子完备数,都输出其所有因子的和

若某整数N的所有因子之和等于N的倍数,则N称为多因子完备数,如数28,其因子之和1+2+4+7+14+28=56=2*28, 28则是多因子完备数

实现代码:

public class Test002 {

    public static void main(String[] args) {

       System.out.println("请输入任一正整数:");

       Scanner sc = new Scanner(System.in);

       int k = sc.nextInt();

       System.out.println("\r\n所有因子的和为:"+getSum(k));

    }

    //得到一个数的所有因子,并返回其和

    public static int getSum(int demo) {

       List<Integer> list = new ArrayList<Integer>();

       int t = 1;

       int sum = 0;

       for (int i = 1; i <= demo; i++) {

           if (demo % i == 0) {

              list.add(i);

           }

       }

       for(int j=0; j<list.size();j++){

           sum = sum + list.get(j);

       }

       if(sum % demo == 0){

           System.out.println("您输入的"+demo+"是多因子完备数");

       }else{

           System.out.println("您输入的"+demo+"不是多因子完备数");

       }

       System.out.println("所有的因子为:");

       for(Integer in:list){

           System.out.print(in+" ");

       }

       return sum;

    }

}

 

26. 我国今年的国民生产总值为45600亿元,若今后每年以9%的增长率增长,计算多少年后能实现国民生产总值翻一番

实现代码:

/**

 * 我国今年的国民生产总值为45600亿元,若今后每年以9%的增长率增长,计算多少年后能实现国民生产总值翻一番

 * n年的国民生产总值是(1+0.01)的n次方乘以45600

 */

public class Test001 {

    public static void main(String[] args) {

       double d = 0.0;

       double k = 45600.0;

       for (int i = 1; i <= 100; i++) {

           d = Math.pow((1+0.09),i)*k;

           if(d > 2*k){

              System.out.println(i+"年后能实现国民生产总值翻一番");

              break;

           }

       }

    }

}

 

27. 有一个三位数满足下列条件:

 (1)三位数字各不相同; (2)此数等于它的各位数字的立方和。求这种三位数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("符合题意的三位数有:");

       for(int i=100; i<=999; i++){

           int a = i%10;

           int aa = i/10%10;

           int aaa = i/100;

           if(a!=aa && aa!=aaa){

              if(i == (a*a*a+aa*aa*aa+aaa*aaa*aaa)){

                  flag ++ ;

                  System.out.print(i+" ");

              }

           }

       }

       System.out.println("\r\n共有"+flag+"");

    }

}

 

28. 求1~130之间所有整数的立方和并输出结果

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=130; i++){

           sum = sum + (i*i*i);

       }

       System.out.println("1~130之间所有整数的立方和等于"+sum);

    }

}

 

29. 求[3,500]内所有素数之和

实现代码:

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       int sum = 0;

       Method2(3, 500);

       for (int i = 0; i <list.size(); i++) {

           sum = sum + list.get(i);

       }

       System.out.println("[3500]内所有素数之和为:" + sum);

    }

    public static void Method2(int m,int n) {

       if (m <= n) {

           for (int i = m; i <= n; i++) {

              if (IsPrime(i)) {

                  list.add(i);

              }

           }

       } else {

           for (int i = n; i <= m; i++) {

              if (IsPrime(i)) {

                  System.out.println(i +"  ");

              }

           }

       }

    }

    public static boolean IsPrime(int k) {

       if (k == 2)

           return true;

       else if (k % 2 == 0)

           return false;

       else {

           for (int i = 2; i <= Math.sqrt(k); i++)

              if (k % i == 0)

                  return false;

           return true;

       }

    }

}

 

30.把一张一元钞票,换成一分、二分和五分硬币,每种至少8枚,求方案数

实现代码:

public class Test001 {

    // 一元等于100分钱

    public static void main(String[] args) {

       for (inti = 8; i <= 100; i++) {

           for (int j = 8; j <= 100; j++) {

              for (int k = 8; k <= 100; k++) {

                  if (1 * i + 2 * j + 5 * k == 100) {

                     System.out.println(i+" "+j+" "+k+" ");

                  }

              }

           }

       }

    }

}

 

31. 设某四位数的千位数字与十位数字的和等于百位数字与个位数字的积

例如,对于四位数:9512,9+1=5*2,求所有这样的四位数之和。

实现代码:

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       List<Integer> li = getNum();

       int sum = 0;

       for (int i = 0; i < li.size(); i++) {

           sum = sum + li.get(i);

       }

       System.out.println("所求之和为:" + sum);

    }

    // 获得所有符合题意的四位数,并将其保存在list

    public static List<Integer> getNum() {

       for (int i = 1000; i <= 9999; i++) {

           int a = i / 1000;

           int aa = i / 100 % 10;

           int aaa = i / 10 % 10;

           int aaaa = i % 10;

           if ((a + aaa) == (aa * aaaa)) {

              list.add(i);

           }

       }

       return list;

    }

}

 

32. 四位双平方数

若一个四位正整数是另一个正整数的平方,且各位数字的和是一个平方数,则称该四位正整数是“四位双平方数”

例如:由于7396=86^2,且7+3+9+6=25=5^2,则称7396是“四位双平方数”。求最小的 “四位双平方数”。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       List<Integer> list = new ArrayList<Integer>();

       int sum = 0;

       int flag = 0;

       for (int i = 1000; i <= 9999; i++) {

           for (int k = 10; k <= 99; k++) {

              if (i == k * k) {

                  int aaaa = i / 1000;

                  int aaa = i / 100 % 10;

                  int aa = i / 10 % 10;

                  int a = i % 10;

                  sum = a + aa + aaa + aaaa;

                  for (int j = 1; j <= 6; j++) {

                     if (Math.sqrt(sum) == j) {

                         flag++;

                         list.add(i);

                     }

                  }

              }

           }

       }

       System.out.println("最小的四位双平方数为:"+list.get(0));

    }

}

 

33. 计算y=1+2/3+3/5+4/7+…+n/(2*n-1)(n=50),

要求:按四舍五入的方式精确到小数点后第二位。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = Sum(50.0);

       String s = d + "";

       String s1 = s.substring(0, s.indexOf(".")+3);

       double d1 = Double.parseDouble(s1);

       String s2 = s.substring(5, 6);

       double d3 = Double.parseDouble(s2);

       if(d3>=5){

           d1 = d1 + 0.01;

           String str = (d1+"").substring(0,(d1+"").indexOf(".")+3);

           System.out.println("1+2/3+3/5+4/7+…+n/(2*n-1)(n=50) = "+str);

       }else{

           System.out.println("1+2/3+3/5+4/7+…+n/(2*n-1)(n=50) = "+s1);

       }

    }

    public static double Sum(double k) {

       double sum = 0.0;

       for (double i = 1.0; i <= k; i++) {

           sum = sum + (i) / (2 * i - 1);

       }

       return sum;

    }

}

 

34. 求当N=20时,1/(1*2)+1/(2*3)+1/(3*4)+….+1/(N*(N+1))的值

要求:按四舍五入的方式精确到小数点后第二位

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d1 = getSum(20);

       String s1 = d1+"";

       String s2 = s1.substring(0,s1.indexOf(".")+3);

       String s3 = s1.substring(s1.indexOf(".")+3,s1.indexOf(".")+4);

       double d2 = Double.parseDouble(s2);

       if(Double.parseDouble(s3)>=5){

           d2 = d2 + 0.01;

           System.out.println("结果 = "+d2);

       }else{

           System.out.println("结果 = "+s2);

       }

    }

    public static double getSum(double d) {

       double sum = 0.0;

       for (double i = 1; i <= d; i++) {

           sum = sum + (1.0) / (i * (i + 1));

       }

       return sum;

    }

}

 

35. 回文数是指正读和反读都一样的正整数

例如3773是回文数。求[1000,9999]之间的奇数回文数的个数

实现代码:

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       int flag = 0;

       Test001 t = new Test001();

       t.Method3();

       for(int i=0; i<list.size(); i++){

           if(list.get(i)%2 != 0){

              flag++;

           }

       }

       System.out.println("\r\n共有"+flag+"");

    }

    //打印出10~10,000之间的所有回文数

    public void Method3(){

       System.out.println("1000~9999之间的所有回文数:");

       for(int i=1000; i<=9999; i++){

           if(isCircleNumber(i)){

              list.add(i);

              System.out.print(i+" ");

           }

       }

    }

    //判断是否为回文数字的方法

    public boolean isCircleNumber(int num){

       int oldValue = num;

       int temp = 0;

       while(num > 0){

           temp = temp*10 +num%10;

           num = num/10;

       }

       return temp == oldValue;

    }

}

 

36. 求m=50时,表达式t=1-1/(2*2)-1/(3*3)-…-1/(m*m)的值

要求:按四舍五入的方式精确到小数点后第四位

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d1  = getNum(50);

       String s1 = d1+"";

       String s2 = s1.substring(0,s1.indexOf(".")+5);

       String s3 = s1.substring(s1.indexOf(".")+5,s1.indexOf(".")+6);

       double d2 = Double.parseDouble(s2);

       if(Double.parseDouble(s3)>=5){

           d2 = d2 + 0.0001;

           System.out.println("1-1/(2*2)-1/(3*3)-...-1/(50*50) = "+d2);

       }else{

           System.out.println("1-1/(2*2)-1/(3*3)-...-1/(50*50) = "+s2);

       }

    }

    public static double getNum(double k){

       double sum = 0.0;

       for(double i=2; i<=k; i++){

           sum = sum + (1/(i*i));

       }

       double d = 1-sum;

       return d;

    }

}

 

37. 求[10,1000]之间满足除以7余5、除以5余3、除以3余1的所有整数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("[10,1000]之间满足除以75、除以53、除以31的所有整数有:");

       for(int i=10; i<=1000; i++){

           if((i % 7 == 5) && (i % 5 == 3) && (i % 3 == 1)){

              flag++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有"+flag+"");

    }

}

 

38. 百钱百鸡问题

用100钱买100只鸡,公鸡一只五钱,母鸡一只三钱,雏鸡三只一钱,编程计算共有几种买法(要求每种鸡至少要买1只)。

实现代码:

publicclass Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=1; i<=100; i++){

           for(int j=1; j<=100; j++){

              for(int k=1; k<=100; k++){

                  if(i*5+j*3+k*(1.0/3.0) == 100){

                     flag++;

//                   System.out.println("公鸡:"+i+",母鸡:"+j+",雏鸡:"+k);

                  }

              }

           }

       }

       System.out.println("共有"+flag+"种买法");

    }

}

 

39. 共有几组i、j、k符合算式ijk+kji=1333,其中i、j、k是0~9之间的一位整数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=0; i<=9; i++){

           for(int j=0; j<=9; j++){

              for(int k=0; k<=9; k++){

                  if((i*100+j*10+k)+(k*100+j*10+i) == 1333){

                     flag++;

//                   System.out.println("i="+i+", j="+j+", k="+k);

                  }

              }

           }

       }

       System.out.println("共有"+flag+"");

    }

}

 

40. 求四位奇数中,所有各位数字之和是25的倍数的数之和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       int sum2 = 0;

       for(int i=1000; i<=9999; i++){

           if(i % 2 != 0){

              int aaaa = i / 1000;//个位

              int aaa = i / 100 % 10;

              int aa = i / 10 % 10;

              int a = i % 10;

              sum = a+aa+aaa+aaaa;

              if(sum % 25 == 0){

                  sum2 = sum2 + i;

              }

           }

       }

       System.out.println("四位奇数中,各位数字之和是25的倍数的数的和为:"+sum2);

    }

}

 

41.根据整型参数m的值,计算公式t=1-1/(2*2)-1/(3*3)-…-1/(m*m)的值(m=100)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d1  = getNum(100);

       String s1 = d1+"";

       String s2 = s1.substring(0,s1.indexOf(".")+5);

       String s3 = s1.substring(s1.indexOf(".")+5,s1.indexOf(".")+6);

       double d2 = Double.parseDouble(s2);

       if(Double.parseDouble(s3)>=5){

           d2 = d2 + 0.0001;

           System.out.println("1-1/(2*2)-1/(3*3)-...-1/(100*100) = "+d2);

       }else{

           System.out.println("1-1/(2*2)-1/(3*3)-...-1/(100*100) = "+s2);

       }

    }

    public static double getNum(double k){

       double sum = 0.0;

       for(double i=2; i<=k; i++){

           sum = sum + (1/(i*i));

       }

       double d = 1-sum;

       return d;

    }

}

 

42. 计算1-123的平方根的倒数之和。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double sum = 0.0;

       for(double i=1.0; i<=123.0; i++){

           sum = sum + (1/(Math.sqrt(i)));

       }

       System.out.println(sum);

    }

}

 

43. 用公式pi/4=1-1/3+1/5-1/7+…求pi(pi为圆周率) 的近似值,直到最后一项的绝对值小于指定的数(参数num)为止

实现代码:

public class Test001 {

    public static void main(String[] args) {

       getPi(0.01);//给定指定参数0.001

    }

    public static void getPi(double num){

       double flag = 1.0;

       double sum = 0.0;

       double t = -1;

       for(int i=2;; i++){

           sum = sum + (Math.pow(t, i))*(1/flag);//这里sum的值是等号右边的和

           flag = 2*i-1;

           if(1.0/flag < num){

              break;

           }

       }

       System.out.println("近似值 = "+sum*4);

    }

}

44. 根据以下公式pi/2=1+1/3+1/3*2/5+1/3*2/5*3/7+1/3*2/5*3/7*4/9+…求pi(pi为圆周率)的值。当最后一项的值小于0.0005时停止计算。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       getPi(0.0005);//传入指定参数若为0.0000000000005,则计算出来的pi值就会更加的精确(3.1415926535887833)

    }

    public static void getPi(double num) {

       double flag = 1.0;

       double sum = 0.0;

       for (int i = 1; i <= 100; i++) {

           if(getNum(i) < num){

              break;

           }else{

              sum = sum + getNum(i);

           }

       }

       System.out.println("pi(pi为圆周率)的值 = " + (sum+1)*2);

    }

    // 求第n项的值(我这里把1/3当做是第一项),所以到时候pi的值就等于(sum+1)*2

    public static double getNum(double k) {

       double sum = 1.0 / 3.0;

       int j = 2;

       int n = 2;

       if (k == 1) {

           return sum;

       } else {

           for (int i = 1; i < k; i ++) {

              sum = sum * (j / (3.0 + n));

              n = n + 2;

              j++;

           }

       }

       return sum;

    }

}

 

45. 计算两个数的最小公倍数

要求:用户从键盘输入两个整数,然后程序输出这两个整数的最小公倍数

实现代码:

public class Test001 {

    public static void fenJie(Vector<Integer> m,int n) {

       for (int i = 2; i <= n; i++) {

           if (n == i) {

              m.addElement(i);

              return;

           }

           if (n > i && (n % i == 0)) {

              n = n / i;

              m.addElement(i);

              fenJie(m, n);

              break;

           }

       }

    }

 

    public static int gongBeiShu(Vector<Integer> m,int a, int b) {

       int chengji = 1;

       if (a % b == 0)

           return a;

       for (int i = 0; i < m.size(); i++) {

           chengji = chengji * m.elementAt(i);

           if ((a * chengji) % b == 0) {

              return a * chengji;

           }

       }

       return a * b;

    }

 

    public static void main(String[] args)throws IOException {

       Integer shu1 = 0;

       Integer shu2 = 0;

       int t = 0;

       Vector<Integer> pool = new Vector<Integer>();

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("请输入第一个正整数:");

       shu1 = (new Integer(stdin.readLine()));

       System.out.print("请输入第二个正整数:");

       shu2 = (new Integer(stdin.readLine()));

       if (shu1 < shu2) {

           t = shu2;

           shu2 = shu1;

           shu1 = t;

       }

       fenJie(pool, shu2);

       t = gongBeiShu(pool, shu1, shu2);

       System.out.print(shu1+""+shu2+"的最小公倍数是:" + t);

    }

}

 

46. 求1900年~2003年所有闰年年号之和

(年号能被400整除的是闰年,或者被4整除但不能被100整除的是闰年)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1900; i<=2003; i++){

           if((i % 400 == 0) || (i % 4 == 0 && i % 100 != 0)){

              sum = sum + i;

           }

       }

       System.out.println("1900~2003年所有闰年年号之和为:"+sum);

    }

}

 

47.计算两个数的最大公约数

实现代码:

public static void fenJie(Vector<Integer> m,int n) {

       for (int i = 2; i <= n; i++) {

           if (n == i) {

              m.addElement(i);

              return;

           }

           if (n > i && (n % i == 0)) {

              n = n / i;

              m.addElement(i);

              fenJie(m, n);

              break;

           }

       }

    }

    public static int bigYinZi(int a,int b) {

       int r = a % b;

       int m = 0;

       if (r == 0)

           return b;

       else {

           a = b;

           b = r;

           m = bigYinZi(a, b);

           return m;

       }

    }

    public static void main(String[] args)throws IOException {

       Integer shu1 = 0;

       Integer shu2 = 0;

       int t = 0;

       Vector<Integer> pool = new Vector<Integer>();

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("请输入第一个正整数:");

       shu1 = (new Integer(stdin.readLine()));

       System.out.print("请输入第二个正整数:");

       shu2 = (new Integer(stdin.readLine()));

       if (shu1 < shu2) {

           t = shu2;

           shu2 = shu1;

           shu1 = t;

       }

       fenJie(pool, shu2);

       t = bigYinZi(shu1, shu2);

       System.out.println("最大公约数是:" + t);

    }

}

 

48.同构数

一个数出现在该数的平方数的右边,称这个数为“同构数”。例如,5出现在平方数25的右边,25出现在平方数625的右边,则5、25都是“同构数”。求[1,1000]之间的所有“同构数”的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("[11000]之间的所有同构数为:");

       for (int i = 1; i <= 10000; i++) {

           String s1 = (i * i) + "";

           if(i<=10){

              String s11 = (i * i) + "";

              String s22 = s1.substring(s11.length() - 1, s11.length());

              if(i == Integer.parseInt(s22)){

                  flag++;

                  System.out.print(i+" ");

              }

           }

           if (s1.length() >= 2) {

              String s2 = s1.substring(s1.length() - 2, s1.length());

              if (i == Integer.parseInt(s2)) {

                  flag++;

                  System.out.print(i+" ");

              }

           }

       }

       System.out.println("\r\n共有" + flag +"");

    }

}

 

49. 求字符串“This is my Basic”所有字符的ASCII码之和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       String str = "This is my Basic";

       char[] c = str.toCharArray();

       for(int i=0; i<c.length; i++){

           String s = String.valueOf(c[i]);

           sum = sum + s.hashCode();

       }

       System.out.println("This is my Basic所有字符的ASCII码之和为:"+sum);

    }

}

 

50. 求满足以下条件的(a,b,c)的组数:(1) 1/(a^2)+1/(b^2)=1/(c^2) ;(2)a>b>c ;(3)a+b+c<100

下面是错误示例:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int a=1; a<100; a++){

           for(int b=1; b<100; b++){

              for(int c=1; c<100; c++){

                  if(((1/(a*a))+(1/(b*b)) == (1/(c*c))) && ((a-b>0)&&(b-c>0)) && (a+b+c < 100)){

                     flag++;

                     System.out.println("a = "+a+", b = "+b+", c = "+c);

                  }

              }

           }

       }

       System.out.println("共有"+flag+"");

    }

}

打印结果共有:22696组

其中有一组是:a = 94, b = 3, c = 2

看如下验证方式:

System.out.println((1/(94*94))+(1/(3*3)) == (1/(2*2)));

如果你像上面这样验证的话,则打印出的是true,但是像下面这样验证的话就是false了

System.out.println((1.0/(94*94))+(1.0/(3*3)) == (1.0/(2*2)));

所以下面这样写才是正确的:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int a=1; a<100; a++){

           for(int b=1; b<100; b++){

              for(int c=1; c<100; c++){

                  if(((1.0/(a*a))+(1.0/(b*b)) == (1.0/(c*c))) && ((a-b>0)&&(b-c>0)) && (a+b+c < 100)){

                     flag++;

                     System.out.println("a = "+a+", b = "+b+", c = "+c);

                  }

              }

           }

       }

       System.out.println("共有"+flag+"");

    }

}

 

51.求个位数是6,且能被3整除的所有四位数之和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1000; i<=9999; i++){

           int g = i%10;

           if(g ==6 && i % 3 == 0){

              sum = sum + i;

           }

       }

       System.out.println("个位数是6,且能被3整除的所有四位数之和为:"+sum);

    }

}

 

52. 有一堆零件(零件个数不超过1000),如果分成4个零件一组余2个;7个一组余3个; 9个一组余5个。求这堆零件的个数有几种可能。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("有如下这么多可能:");

       for(int i=6; i<=1000; i++){

           if((i%4.0 == 2.0) && (i%7.0 == 3.0) && (i%9.0 == 5.0)){

              flag++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有"+flag+"种可能");

    }

}

 

53. 求这样的一个三位数,其个位数不大于2。若将个位数移动到百位之前(如:321移成132),新三位数大于原三位数的两倍

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int a = 0;

       int aa = 0;

       int flag = 0;

       System.out.println("满足题意的三位数有:");

       for (int i = 100; i <= 999; i++) {

           a = i % 10;

           aa = i / 10;

           if (a != 0) {

              if ((a * 100 + aa) > (i * 2)) {

                  flag++;

                  System.out.print(i +" ");

              }

           }

       }

       System.out.println("\r\n共有"+flag+"");

    }

}

 

54. 有一堆桃子(个数不超过1000),如果分成4个一组余2个;7个一组余3个; 9个一组余5个。求这堆桃子的个数有几种可能

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("这堆桃子的个数有如下几种可能:");

       for(int i=6; i<=1000; i++){

           if((i%4.0 == 2.0) && (i%7.0 == 3.0) && (i%9.0 == 5.0)){

              flag++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有"+flag+"种可能");

    }

}

 

55. 求sum=d+dd+ddd+……+ddd..d(d为1-9的数字)。例如,3+33+333+3333(此时d=3,n=4)从键盘上输入d 的值为3,n的值为4

实现代码:

public class Test001 {

    public static void main(String[] args)throws IOException {

       int s = 0;

       int a = 0;

       int sum = 0;

       String p = "";

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("请输入数字(1-9)");

       a = new Integer(stdin.readLine());

       System.out.print("请输入数字个数:");

       s = new Integer(stdin.readLine());

       for (int i = 1; i <= s; i++) {

           p = a+"";

           for (int j = 1; j < i; j++) {

              p = p + a;

           }

           int d = Integer.parseInt(p);

           sum = sum + d;

       }

       System.out.println("和为:" + sum);

    }

}

 

56. 求数列2,4,8,16,32,…前若干项之和。当和大于9000时,终止求和并输出结果

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=2; i<=100000; i=i*2){

           sum = sum + i;

           if(sum > 9000){

              System.out.println("和为:"+sum);

              break;

           }

       }

    }

}

 

57. 将50元兑换成5元、2元和1元的方法(每种面额不能为0张)的种数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       int sum = 0;

       for(int i=1; i<10; i++){

           for(int j=1; j<25; j++){

              for(int k=1; k<50; k++){

                  if((i*5 + j*2 + k*1) == 50){

                     flag++;

                     System.out.println("5:"+i+","+"2:"+j+","+"1:"+k+"");

                  }

              }

           }

       }

       System.out.println("\r\n共有"+flag+"");

    }

}

 

58. 某试卷由26个问题组成,答对一题得8分,答错一题扣5分。今有一考生虽然回答了全部26个问题,但所得总分为零,问他错答多少题。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int i=1; i<=26; i++){

           for(int j=1; j<=26; j++){

              if(((i*8 + j*(-5)) == 0) && ((i+j) == 26)){

                  System.out.println("他打错了"+j+"");

              }

           }

       }

    }

}

 

59. 某班级有学生若干名,依次编号为1,2,3,……,除去编号1与2的两名学生外,所有学生编号之和是100的整数倍,如果知道学生编号之和小于1000,问共有学生多少人

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       System.out.println("学生总人数可能是:");

       for(int i=3; i<=100000; i++){

           sum = sum + i;

           if((sum%100 == 0) && sum<1000){

              System.out.println(i);

           }

       }

    }

}

 

60. 在一个正整数序列中,第一项是1978,第二项是1979,从第三项起每一项等于前二项的差的绝对值,问此数列有多少项

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int a = 1978;//a保存较小的数

       int b = 1979;//b保存较大的数

       for(int i=1;;i++){

           int c = a-b;

           if(c<0){

              c = -c;

           }else if(c == 0){

              System.out.println("此数列有:"+(i-1)+"");

              break;

           }

           a = b;

           b = c;

       }

    }

}

 

61. 有一堆零件(零件个数不超过1000),如果分成4个零件一组余2个;7个一组余3个; 9个一组余5个。求这堆零件的个数有几种可能

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       System.out.println("有如下这么多可能:");

       for(int i=6; i<=1000; i++){

           if((i%4.0 == 2.0) && (i%7.0 == 3.0) && (i%9.0 == 5.0)){

              flag++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有"+flag+"种可能");

    }

}

 

62. 求1~200之间勾股数的组数(如a*a+b*b=c*c,则a,b,c为一组勾股数)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=1; i<=200; i++){

           for(int j=1; j<=200; j++){

              for(int k=1; k<=200; k++){

                  if((i*i) + (j*j) == (k*k)){

                     flag++;

                  }

              }

           }

       }

       System.out.println("1200之间的勾股数组数有"+flag+"");

    }

}

 

63. 给定一个100行和100列的整数方阵,求左上至右下对角线上各元素之和。(首先了解什么是方阵)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=(100*100); i=i+(100+1)){

           sum = sum + i;

       }

       System.out.println("元素之和为:"+sum);

    }

}

 

64. 一辆以固定速度行驶的汽车,司机看到里程表上从左到右的读数和从右到左的读数是相同的,这个数是12321(公里),2小时后,里程表上再次出现一个新的对称数。问车的速度是多少公里/小时?

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int k = 0;

       for(int i=1; i<=100000; i++){

           k = 2*i + 12321;

           StringBuffer s = new StringBuffer((k+""));

           String ss1 = s.toString();

           StringBuffer s2 = s.reverse();

           String ss2 = s2.toString();

           if(ss1.equals(ss2) && k > 12321){

              System.out.println("车的速度是"+i+"公里/小时");

              break;

           }

       }

    }

}

 

65. n个人围成一圈(编号为1-n),从第1号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩一个人为止。问当n=17时,剩下的人是多少号

实现代码:

import java.util.LinkedList;

import java.util.List;

public class Test001 {

    public static void main(String[] args) {

       System.out.println("剩下的人是:" +cycle(17, 3)+"");

    }

    // 测试方法

    public static int cycle(int total,int k) {//功能方法

       List<Integer> dataList = new LinkedList<Integer>();//创建链表对象

       for (int i = 0; i < total; i++)

           dataList.add(new Integer(i + 1));//目前为止,dataList里保存了1-1717个数

       // 定义下标,模拟已经去掉一个元素,因此从-1开始

       int index = -1;

       // 一直循环去除数据,直到只剩下一个元素

       while (dataList.size() > 1) {

           index = (index + k) % dataList.size();//得到应该出局的下标,但是还要减一

           System.out.println("出局的下标:"+index);

           dataList.remove(index--);

       }

       return ((Integer) dataList.get(0).intValue());

    }

}

 

66. 求数列f(n) = n*n+n+41的前100项中素数的个数

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       List<Integer> list = new ArrayList<Integer>();

       for(int i=1; i<=100; i++){

           list.add((i*i+i+41));

       }

       System.out.println("n*n+n+41的前100项中素数有:");

       for(int i=list.get(0); i<=list.get(99); i++){

           boolean flag = true;

           for(int k=2; k<=Math.sqrt(i); k++){

              if(i % k == 0){

                  flag = false;

                  break;

              }

           }

           if(flag){

              count++;

              System.out.print(i+" ");

           }

       }

       System.out.println("\r\n共有" + count +"");

    }

}

 

67. 士兵在演练过程中,队伍变换成10、21、35、60行时,队形都能成为矩形。问参加演练的士兵最少有多少人

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int k=10; k<=10000; k++){

           if((k%10 == 0) && (k%21 == 0) && (k%35 == 0) && (k%60 == 0)){

              System.out.println("参加演练的士兵最少有"+k+"");

              break;

           }

       }

    }

}

 

68. 算年龄。用爷爷的年龄的5倍加6得的和,再乘以20,再加上奶奶的年龄,再减去365,得数为6924,又知爷爷比奶奶大2岁。求爷爷、奶奶的年龄的和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=200; i++){

           for(int j=1; j<=200; j++){

              if((((i*5+6)*20+j)-365) == 6924 && (i -j) == 2){

                  sum = i + j;

                  System.out.println("爷爷年龄为:"+i+" ,奶奶年龄为:"+j);

              }

           }

       }

       System.out.println("其年龄和为:"+sum);

    }

}

 

69. 宴会上一共有1225次握手,设每一位参加宴会的人对其他与会人士都有一样的礼节,那么与会人士有多少人?

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=1; i<10000; i++){

           flag = flag + i;

           if(flag == 1225){

              System.out.println("与会人士共有"+(i+1)+"");

              break;

           }

       }

    }

}

 

70. 金星和地球在某一时刻相对于太阳处于某一确定位置,已知金星绕太阳一周为225日,地球绕太阳一周为365日,问两个行星至少经过多少日仍同时回到原来的位置上

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int i=225;;i++){

           if((i%225 == 0) && (i%365 == 0)){

              System.out.println("两个行星至少经过 "+i+"天仍同时回到原来的位置上");

              break;

           }

       }

    }

}

 

71. 求表达式e^x ≈1+x+x^2/2!+x^3/3!+x^4/4!……+x^n/n!的近似值,设x=9,n=25

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(9.0,25.0)+1.0;

       System.out.println("所求和等于:"+d);

    }

    //求和

    public static double getSum(double x,double n){

       double sum = 0;

       for(double i=1; i<=n; i++){

           double a1 = getNum(9,i);

           double a2 = getJieChengSum(i);

           sum = sum + (a1/a2);

       }

       return sum;

    }

    //计算xn次方

    public static double getNum(double x,double n){

       double k = x;

       for(double i=1;i<n;i++){

           k = k*x;

       }

       return k;

    }

    //计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n){

       double sum = 1;

       for(double i=1; i<=n; i++){

           sum = sum * i;

       }

       return sum;

    }

}

 

72. 求表达式e^x ≈1+x+x^2/2!+x^3/3!+x^4/4!……+x^n/n!的近似值,直到最后一项小于0.01为止;设x=9,n=100

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(9.0,100.0)+1.0;

       System.out.println("所求和等于:"+d);

    }

    //求和

    public static double getSum(double x,double n){

       double sum = 0;

       for(double i=1; i<=n; i++){

           double a1 = getNum(9,i);

           double a2 = getJieChengSum(i);

           sum = sum + (a1/a2);

           System.out.println((a1/a2));

           if((a1/a2)<0.01){

              break;

           }

       }

       return sum;

    }

    //计算xn次方

    public static double getNum(double x,double n){

       double k = x;

       for(double i=1;i<n;i++){

           k = k*x;

       }

       return k;

    }

    //计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n){

       double sum = 1;

       for(double i=1; i<=n; i++){

           sum = sum * i;

       }

       return sum;

    }

}

 

73. 用sin(x)≈x-x^3/3!+x^5/5!-……+(-1)^(n-1)*(x^(2n-1))/(2n-1)!的公式求近似值。设x=7,n=15

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(7, 15);

       System.out.println("近似值 = "+d);

    }

    // 求和

    public static double getSum(double x,double n) {

       double sum = 0;

       int t = 1;

       for (int i = 1; i <= n; i++) {

           if (t <= (2 * n - 1)) {

              double a1 = getNum(x, t);

              double a2 = getJieChengSum(t);

              double k = getNum(-1, (i - 1));

              sum = sum + k * (a1 / a2);

              t = t + 2;

           }

       }

       return sum;

    }

    // 计算xn次方

    public static double getNum(double x,double n) {

       double k = x;

       if (n == 0) {

           return 1;

       } else {

           for (double i = 1; i < n; i++) {

              k = k * x;

           }

       }

       return k;

    }

    // 计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n) {

       double sum = 1;

       for (double i = 1; i <= n; i++) {

           sum = sum * i;

       }

       return sum;

    }

}

 

74. 用sin(x)≈x-x^3/3!+x^5/5!-……+(-1)^(n-1)*(x^(2n-1))/(2n-1)!的公式求近似值,直到最后一项绝对值小于0.00001为止。设x=7,n=15

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(7, 15);

       System.out.println("近似值 = "+d);

    }

    // 求和

    public static double getSum(double x,double n) {

       double sum = 0;

       int t = 1;

       for (int i = 1; i <= n; i++) {

           if (t <= (2 * n - 1)) {

              double a1 = getNum(x, t);

              double a2 = getJieChengSum(t);

              double k = getNum(-1, (i - 1));

              sum = sum + k * (a1 / a2);

               if(k * (a1 / a2)>0){

                  if(k * (a1 / a2)<0.00001){

                     break;

                  }

              }else{

                  if((-1*(k * (a1 / a2)))<0.00001){

                     break;

                  }

              }

              t = t + 2;

           }

       }

       return sum;

    }

    // 计算xn次方

    public static double getNum(double x,double n) {

       double k = x;

       if (n == 0) {

           return 1;

       } else {

           for (double i = 1; i < n; i++) {

              k = k * x;

           }

       }

       return k;

    }

    // 计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n) {

       double sum = 1;

       for (double i = 1; i <= n; i++) {

           sum = sum * i;

       }

       return sum;

    }

}

 

75. 用cos(x)≈1-x^2/2!+x^4/4!-……+(-1)^(n)*(x^(2n))/(2n)!的公式求近似值,设x=9,n=15

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(9,15);

       System.out.println("近似值 = "+(1-d));

    }

    // 求和

    public static double getSum(double x,double n) {

       double sum = 0;

       int t = 2;

       for (int i = 1; i <= n; i++) {

           if (t <= (2 * n)) {

              double a1 = getNum(x, t);

              double a2 = getJieChengSum(t);

              double k = getNum(-1, (i+1));

              sum = sum + k * (a1 / a2);

              t = t + 2;

           }

       }

       return sum;

    }

    // 计算xn次方

    public static double getNum(double x,double n) {

       double k = x;

       if (n == 0) {

           return 1;

       } else {

           for (double i = 1; i < n; i++) {

              k = k * x;

           }

       }

       return k;

    }

    // 计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n) {

       double sum = 1;

       for (double i = 1; i <= n; i++) {

           sum = sum * i;

       }

       return sum;

    }

}

 

76. 用cos(x)≈1-x^2/2!+x^4/4!-……+(-1)^(n)*(x^(2n))/(2n)!的公式求近似值,直到最后一项绝对值小于0.00001为止。设x=7,n=15

实现代码:

public class Test001 {

    public static void main(String[] args) {

       double d = getSum(7,15);

       System.out.println("近似值 = "+(1-d));

    }

    // 求和

    public static double getSum(double x,double n) {

       double sum = 0;

       int t = 2;

       for (int i = 1; i <= n; i++) {

           if (t <= (2 * n)) {

              double a1 = getNum(x, t);

              double a2 = getJieChengSum(t);

              double k = getNum(-1, (i+1));

              sum = sum + k * (a1 / a2);

              if(k * (a1 / a2)>0){

                  if(k * (a1 / a2)<0.00001){

                     break;

                  }

              }else{

                  if((-1*(k * (a1 / a2)))<0.00001){

                     break;

                  }

              }

              t = t + 2;

           }

       }

       return sum;

    }

    // 计算xn次方

    public static double getNum(double x,double n) {

       double k = x;

       if (n == 0) {

           return 1;

       } else {

           for (double i = 1; i < n; i++) {

              k = k * x;

           }

       }

       return k;

    }

    // 计算某一数字的阶乘,并返回

    public static double getJieChengSum(double n) {

       double sum = 1;

       for (double i = 1; i <= n; i++) {

           sum = sum * i;

       }

       return sum;

    }

}

 

77. 已知Sn=A1+A2+A3+...+An, 其中,当n为奇数时An=n-1,当n为偶数时,An=n+1.例如:S6=0+3+2+5+4+7, 求:S60=A1+A2+A3+...+A60.

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=60; i++){

           if(i % 2==0){

              sum = sum + getSum2(i);

           }else{

              sum = sum + getSum1(i);

           }

       }

       System.out.println("A1+A2+A3+...+A60 = "+sum);

    }

    //n为奇数时

    public static int getSum1(int i){

       int sum = 0;

       sum = i-1;

       return sum;

    }

   

    //n为偶数时

    public static int getSum2(int j){

       int sum = 0;

       sum = j+1;

       return sum;

    }

}

 

78.求在 1,2,3,...,100中, 任选两个不同的数,要求它们的和能被3和7整除的数的对数(注意:3+5和5+3认为是同一对数)。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       int flag = 0;

       for(int i=1; i<=100; i++){

           for(int j=1; j<=100; j++){

              sum = i+1;

              if((sum%3 == 0) && (sum%7 == 0)){

                  if(i != j){

                     flag++;

                  }

              }

           }

       }

       System.out.println("共有"+flag+"");

    }

}

 

79. 已知S1=1, S2=1+2, S3=1+2+3,...,SN=1+2+3+...+N, 求在S1,S2,S3,...,S100中,所有能被3和7整除的数之和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=100; i++){

           if((getSn(i)%3 == 0) && (getSn(i)%7==0)){

              sum = sum + getSn(i);

           }

       }

       System.out.println("所求之和 = "+sum);

    }

    //Sn

    public static int getSn(int k){

       int sum = 0;

       for(int i=1; i<=k; i++){

           sum = sum + i;

       }

       return sum;

    }

}

 

80. 已知菲波纳契数列{ X }中,X(1)=0,X(2)=1,X(n)=X(n-1)+X(n-2),编程求数列前30个数中,所有质数的和

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=30; i++){

           list.add(getSum(i));

       }

       for(int i=0; i<list.size(); i++){

           if(list.get(i)>1){

              if(list.get(i)%2 != 0){

                  sum = sum + list.get(i);

              }

           }

       }

       System.out.println("所有质数的和为:"+sum);

    }

    //X(n)的和

    public static int getSum(int k){

       if(k<=1){

           return 0;

       }else if(k<=2 && k>1){

           return 1;

       }else{

           return getSum(k-1)+getSum(k-2);

       }

    }

}

 

81. 求三位数中,个位数字与十位数字之和除以10所得的余数是百位数字,且百位数字是偶数的数的个数。

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=100; i<=999; i++){

           int a = i%10;

           int aa = i/10%10;

           int aaa = i/100;

           if((aaa%2 == 0) && ((a+aa)%10) == aaa){

           flag++;

//         System.out.print(i+" ");//打印出所有满足题意的三位数

           }

       }

       System.out.println("共有"+flag+"");

    }

}

 

82. 一个素数称之为超级素数,若该素数依次去掉个位,十位,...等等,每次所得的数仍然是素数。例如239就是超级素数。求[100,9999]之内超级素数的个数

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       int flag = 0;

       for (int i = 100; i <= 9999; i++) {

           if (i <= 999) {

              if (JudgeNum(i) &&JudgeNum(i / 10) && JudgeNum(i / 100)) {

                  flag++;

                  list.add(i);

              }

           }

           if (i >= 1000) {

              if (JudgeNum(i) &&JudgeNum(i/10) && JudgeNum(i/100) && JudgeNum(i/1000)) {

                  flag++;

                  list.add(i);

              }

           }

       }

       System.out.println("[100,9999]之内超级素数有:");

       for(Integer in:list){

           System.out.print(in+" ");

       }

       System.out.println("\r\n共有"+flag+"");

    }

    // 判断一个数是否为素数

    public static boolean JudgeNum(int number) {

       boolean flag = true;

       for (int i = 2; i <= Math.sqrt(number); i++) {

           if (number % i == 0) {

              flag = false;

           } else if (flag == false) {

              break;

           }

       }

       return flag;

    }

}

 

83. 求杨辉三角形中,第20行第10列的数为多少?

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int[][] strs = new int[20][20];

       //给数组的所有单元都赋值为1

       for (int i = 0; i < 20; i++) {

           for (int j = 0; j <= i; j++) {

              strs[i][j] = 1;

           }

       }

       //从第2行、第1列开始,对应位置的数据是其肩上两个数据的和

       for(int i=2; i<20; i++){

           for(int j=1; j<=i; j++){

              strs[i][j] = strs[i-1][j-1] +strs[i-1][j];

           }

       }

       //将数组打印出来

//     for (int i = 0; i < 20; i++) {

//         for (int j = 0; j <= i; j++) {

//            System.out.print(strs[i][j]+"\t");

//         }

//         System.out.println();

//     }

       System.out.println("杨辉三角形中,第20行第10列的数为:"+strs[19][9]);

    }

}

 

84. 一个数如果刚好与它所有的因子之和相等,则称该数为“完数”,如6=1+2+3,则6就是个完数。编程求[8000,9000]之间完数的个数

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for (int i = 8000; i < 9000; i++) {

           int t = 0;//t表示所有因子的和

           for (int j = 1; j <= 8128 / 2; j++) {//j表示i的因子

              if (i % j == 0) {

                  t = t + j;

              }

           }

           if (t == i) {

               flag++;

           }

       }

       System.out.println("[8000,9000]之间完数有"+flag+"");

    }

}

 

85. 我国古代数学家在《算经》中出了一道题:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。百钱买百鸡,问鸡翁、母、雏各几何?”意为公鸡5吊钱1只,母鸡3吊钱1只,3只小鸡值1吊钱。编程实现用100吊钱买100只鸡,公鸡、母鸡、小鸡每种鸡至少一只,有多少种买法?

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int flag = 0;

       for(int i=1; i<=100; i++){

           for(int j=1; j<=100; j++){

              for(int k=1; k<=100; k++){

                  if(i*5+j*3+k*(1.0/3.0) == 100){

                     flag++;

//                   System.out.println("公鸡:"+i+",母鸡:"+j+",雏鸡:"+k);

                  }

              }

           }

       }

       System.out.println("共有"+flag+"种买法");

    }

}

 

86. Fibonacci数列的前几个数为:0,1,1,2,3,5,…,其规律是: F1 = 0 (n = 1) F2 = 1 (n = 2) Fn = Fn-1+Fn-2 (n ≥ 3) 编程求此数列的前40项之和

实现代码:

import java.util.ArrayList;

import java.util.List;

public class Test001 {

    static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

       int sum = 0;

       for(int i=1; i<=40; i++){

           sum = sum + getSum(i);

       }

       System.out.println("40项之和为:"+sum);

    }

    //X(n)的和

    public static int getSum(int k){

       if(k<=1){

           return 0;

       }else if(k<=2 && k>1){

           return 1;

       }else{

           return getSum(k-1)+getSum(k-2);

       }

    }

}

 

87. 抓交通肇事犯:

一辆卡车违反交通规则,撞人后逃跑。现场有三人目击事件,但是没有记住车号,只记下车号的一些特征。甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的,但与前两位不同;丙说:四位的车号刚好是一个整数的平方

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int i=1000; i<=9999; i++){

           int a = i/1000;//获得千位数字

           int aa = i/100%10;//获得百位数字

           int aaa = i/10%10;//获得十位数字

           int aaaa = i%10;//获得个位数字

           if((a == aa) && (aaa == aaaa) && (aa != aaa)){

              if(Test(i)){

                  System.out.println("肇事辆卡车牌是:"+i);

              }

           }

       }

    }

    //判断车牌号是哪个整数的平方

    public static boolean Test(int k){

       boolean flag = false;

       for(int i=1; i<=100; i++){

           if(i*i == k){

              flag = true;

              break;

           }

       }

       return flag;

    }

}

 

88.  4位反序数

设N是一个四位数,它的9倍恰好是其反序数,求N。(反序数就是将整数的数字倒过来形成的整数,如1234的反序数是4321。)

实现代码:

public class Test001 {

    public static void main(String[] args) {

       for(int i=1000; i<=9999; i++){

           if(i*9 == getNum(i)){

              System.out.println("N = "+i);

           }

       }

    }

    //求任意一个四位数的反序数

    public static int getNum(int i){

       int m = 0;

       int a = i/1000;//获得千位数字

       int aa = i/100%10;//获得百位数字

       int aaa = i/10%10;//获得十位数字

       int aaaa = i%10;//获得个位数字

       m = aaaa*1000 + aaa*100 + aa*10 +a;

       return m;

    }

}

 

89. 高次方程尾数的问题:求13的298次方的最后三位数是多少?编程实现之

实现代码:

public class Test001 {

    public static void main(String[] args) {

       long d = 1;

       for(int i=1; i<=298; i++){

           d = d * 13;

       }

       String s = d+"";

       String s2 = s.substring(s.length()-3, s.length());;

       System.out.println("13298次方的最后三位数是:"+s2);

    }

}

 

90. 辗转相除法

从键盘输入两个数51211314和84131421,利用辗转相除法求它们的最大公约数。求需要经过多少次辗转

实现代码:

import java.util.Scanner;

public class Test001 {

    static int flag = 0;

    public static void main(String[] args) {

       System.out.println("请输入第一个整数:");

       Scanner sc = new Scanner(System.in);

       int one = sc.nextInt();

       System.out.println("请输入第二个整数:");

       Scanner sc2 = new Scanner(System.in);

       int two = sc2.nextInt();

       if(one>two){

           int a = getNum(one,two);

           System.out.println("需要经过"+(a+1)+"此辗转");

       }else{

           int a = getNum(two,one);

           System.out.println("需要经过"+(a+1)+"此辗转");

       }

    }

    //对用户输入的两个数进行处理(采用了递归)

    public static int getNum(int a,int b) {

       int k = 0;

       k = a % b;

       if(k == 0 || k == 1){

           System.out.println("最大公约数是:"+b);

           return flag;

       }

       else{

           flag++;

           return getNum(b,k);

       }

    }

}

 

91.拍七游戏

打印出"拍七"游戏规则: 大家依次从1开始顺序数数,数到含有7或7的倍数的要拍手或其它规定的方式表示越过(比如:7,14,17等都不能数出),下一人继续数下面的数字。违反规则者受罚。

实现代码:

public class Test001 {

    //包含77的倍数都打印出*

    public static void main(String[] args) {

       for (int i = 1; i < 100; i++) {

           if (i % 7 == 0)

              System.out.printf("*\n");

           else if ((String.valueOf(i).contains("7")))

              System.out.printf("*\n");

           else

              System.out.printf("%d\n", i);

       }

    }

}

 

92.出圈算法典例

题目:50个人围城一圈,数到3或3的倍数时出圈,问剩下的人是谁?在原来的位置是多少?

 

分析:最好的办法就是使用取余的办法,就可以始终得到3的倍数,无论它的倍数是多少,也不管它的元素个数是多少;由于每次去掉元素以后,元素的个数会少一个,因此下一个3的倍数其实只需要走两步,在为其下标赋值的时候,需要减一,保持每次去掉的元素都是3的倍数。如果使用0开始的下标开始计算,那么初始化的时候应该使用-1,这样就可以模拟元素已经减少一个了;至于元素的保存,可以使用数组,也可以使用链表。数组的元素去掉以后,它的下一个元素是不会自动往前移的,不太好使用,但是也可以使用。这里,最好使用java.util.List链表来表示,它既有下标,又可以很方便的获得元素的当前个数,尽管效率比数组稍微低一些,不过已经足够了

 

代码实现:

import java.util.LinkedList;

import java.util.List;

public class Test001 {

    public static void main(String[] args) {

       System.out.println("剩下的人在原来的位置是:" +cycle(50, 3));

    }

    // 测试方法

    public static int cycle(int total,int k) {//功能方法

       List<Integer> dataList = new LinkedList<Integer>();//创建链表对象

       for (int i = 0; i < total; i++)

           dataList.add(new Integer(i + 1));

       // 定义下标,模拟已经去掉一个元素,因此从-1开始

       int index = -1;

       // 一致循环去除数据,直到只剩下一个元素

       while (dataList.size() > 1) {

           index = (index + k) % dataList.size();//得到应该出局的下标

           dataList.remove(index--);

       }

       return ((Integer) dataList.get(0).intValue());

    }

}

 

93.打印出菱形图案

题目:打印出如下图案(菱形)

    *

   ***

 ******

********

 ******

  ***

   *

 

分析:

 

1.先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。问题比较简单,就不进行编程了。

2为了使问题具有一般性,现在我们输入菱形的行数,编程实现菱形的绘制。

分析:

1、输入的列数必须为行数

2、假设输入的数字为n,则第(n+1)/2为中间一行,进行一些简单的数学推导,便能推出具体某一行的打印情况

实现代码:

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Test001 {

    public static void main(String[] args)throws Exception {

       int n = 0;

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("请输入菱形行数:");

       n = (new Integer(stdin.readLine()));

       if (n % 2 != 0) {

           for (int i = 1; i <= (n + 1) / 2; i++) {

              for (int j = 1; j <= (n - 1) / 2 + i; j++) {

                  if (j <= (n + 1) / 2 - i) {

                     System.out.print(" ");

                  } else

                     System.out.print("*");

              }

              System.out.println("");

           }

           for (int i = (n + 3) / 2; i <= n; i++) {

              for (int j = 1; j <= (3 * n + 1) / 2 - i; j++) {

                  if (j <= i - (n + 1) / 2) {

                     System.out.print(" ");

                  } else

                     System.out.print("*");

              }

              System.out.println("");

           }

       } else

           System.out.print("请确保行数为奇数!");

    }

}

 

94. 输入年份和月份即可打印出当月的日历

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Scanner;

public class Test001 {

    int month; //该变量用于存储月

    int year; //该变量用于存储年

    public Test001(finalint dismonth, finalint disyear) {

        this.month = dismonth;

        this.year = disyear;

    }

    //该方法返回月

    public String checkMonth() {

        String[] months = {"1","2","3","4","5","6","7","8","9",

                          "10","11","12"};

        return months[month];

    }

    //该方法返回天

    public int checkDays() {

        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        return days[month];

    }

    //打印日历

    public void print() {

        int initSpace = 0;//将该月份起始的天数留空

        try {

            //获取月份名称

            String monthName = checkMonth();

            System.out.println("");

            System.out.println("\t\t\t" +year + "" + monthName);

            System.out.println("");

        } catch (Exception ex) {

            System.out.println("超出范围.....");

            System.exit(0);

        }

        GregorianCalendar gc = new GregorianCalendar(year,month, 1);

        System.out.println("\t\t\t\t\t\t\t");

        //得到该月的第一天是一个星期的第几天,然后预留空格

        initSpace = gc.get(Calendar.DAY_OF_WEEK) - 1;

        //获取天数

        int daysInMonth = checkDays();

        //检查是否为闰年,为二月份增加一天(使用isLeapYear()方法判断是否为闰年)

        if (gc.isLeapYear(gc.get(Calendar.YEAR)) &&month == 1) {

            ++daysInMonth;

        }

        for (int i = 0; i < initSpace; i++) {

            System.out.print("\t");

        }

        for (int i = 1; i <= daysInMonth; i++) {

            System.out.print("\t" + i);

            if ((initSpace + i) % 7 == 0) {

                System.out.println();

            }

        }

        System.out.println("");

    }

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        System.out.println("请输入年份:");

        int year = s.nextInt();

        System.out.println("请输入月份:");

        int month = s.nextInt();

        Test001 vm = new Test001(month - 1, year);

        vm.print();

    }

}

 

95.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和

实现代码:

public class Test001 {

    public static void main(String[] args) {

       int x = 2, y = 1, t;

       double sum = 0;

       for (int i = 1; i <= 20; i++) {

           sum += (double) x / y;

           t = y;

           y = x;

           x = y + t;

       }

       System.out.println("20项的和 = "+sum);

    }

}

 

96. 将如下的乘法口诀表写入到“D://a.txt”文件中

1*1=1      

2*1=2       2*2=4      

3*1=3       3*2=6       3*3=9      

4*1=4       4*2=8       4*3=12     4*4=16    

5*1=5       5*2=10     5*3=15     5*4=20     5*5=25    

6*1=6       6*2=12     6*3=18     6*4=24     6*5=30     6*6=36    

7*1=7       7*2=14     7*3=21     7*4=28     7*5=35     7*6=42     7*7=49    

8*1=8       8*2=16     8*3=24     8*4=32     8*5=40     8*6=48     8*7=56     8*8=64    

9*1=9       9*2=18     9*3=27     9*4=36     9*5=45     9*6=54     9*7=63     9*8=72     9*9=81

写到指定文件中:

方法一

实现代码:

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Test001 {

    public static void main(String[] args) {

       try {

           FileOutputStream fos = new FileOutputStream("D://a.txt",true);

           for(int i=1; i<=9; i++){

              for(int j=1; j<=i; j++){

                  fos.write((i+"*"+j+"="+(i*j)+"\t").getBytes());

              }

              fos.write("\r\n".getBytes());

           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

方法二

实现代码:

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

public class Test003 {

    public static void main(String[] args) {

       try {

           BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://a.txt")));

           for(int i=1; i<=9; i++){

              for(int j=1; j<=i; j++){

                  bw.write((i+"*"+j+"="+(i*j)+"\t"));

              }

              bw.write("\r\n");

           }

           bw.flush();

           bw.close();

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

 

然后再将这个文件中的口诀表读到控制台上:

方法一

实现代码:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

public class Test002 {

    public static void main(String[] args) {

       try {

           FileInputStream fis = new FileInputStream("D://a.txt");

           byte[] buff = new byte[1024];

           while(fis.read(buff)!= -1){

              System.out.println(new String(buff,0,buff.length).trim());

           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

 

方法二

实现代码:

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

public class Test003 {

    public static void main(String[] args) {

       try {

           BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D://a.txt")));

           String str = "";

           while((str = br.readLine())!=null){

              System.out.println(str);

           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

 

97. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高

 

实现代码:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Test001 {

    public static void main(String[] args)throws IOException {

       float heigh = 100;

       int cishu = 10;

       float sum = 0;

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("输入初始下落高度:");

       heigh = (new Float(stdin.readLine()));

       sum = heigh;

       System.out.print("输入落地碰撞次数:");

       cishu = (new Integer(stdin.readLine()));

       for (int i = 1; i < cishu; i++) {

           heigh = heigh / 2;

           sum = sum + heigh * 2;

       }

       System.out.println("" + cishu +"次反弹高度为:" + heigh);

       System.out.println("" + cishu +"次落地总经过长度为:" + sum);

    }

}

 

98. 输入某年某月某日,判断这一天是这一年的第几天?

方法一

实现代码:

import java.util.Scanner;

public class Test001 {

    public static void main(String[] args) {

       int count = 0;

       Scanner in = new Scanner(System.in);

       System.out.print("请输入年:");

       int year = in.nextInt();

       System.out.print("请输入月:");

       int month = in.nextInt();

       System.out.print("请输入日:");

       int day = in.nextInt();

       switch (month) {

       case 12:

           count += 30;

       case 11:

           count += 31;

       case 10:

           count += 30;

       case 9:

           count += 31;

       case 8:

           count += 30;

       case 7:

           count += 31;

       case 6:

           count += 31;

       case 5:

           count += 30;

       case 4:

           count += 31;

       case 3:

           count += 28;

       case 2:

           count += 31;

       case 1:

           count += 0;

       }

       count += day;

       if (year % 4 == 0 && year % 100 != 0 && month >= 3) {

           count += 1;

       }

       System.out.print(year + "-" + month + "-" + day + ""+year+"年中的第" + count

              + "");

    }

}

 

方法二

实现代码:

import java.util.Scanner;

public class Test002 {

    public static void main(String[] args)throws Exception {

       int y, m, d;

       int sum = 0;

       int feb = 28;

       Scanner in = new Scanner(System.in);

       System.out.print("请输入年份:");

       y = in.nextInt();

       System.out.print("请输入月份:");

       m = in.nextInt();

       System.out.print("请输入几号:");

       d = in.nextInt();

 

       if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {

           feb = 29;

       }

 

       switch (m) {

       case 1:

           sum = d;

           break;

       case 2:

           sum = 31 + d;

           break;

       case 3:

           sum = 31 + feb + d;

           break;

       case 4:

           sum = 31 + feb + 31 + d;

           break;

       case 5:

           sum = 31 + feb + 31 + 30 + d;

           break;

       case 6:

           sum = 31 + feb + 31 + 30 + 31 + d;

           break;

       case 7:

           sum = 31 + feb + 31 + 30 + 31 + 30 + d;

           break;

       case 8:

           sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + d;

           break;

       case 9:

           sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + d;

           break;

       case 10:

           sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + d;

           break;

       case 11:

           sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;

           break;

       case 12:

           sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + d;

       }

       System.out.println(y + " " + m +" " + d +"号是"+y+"年的第" + sum +"");

    }

}

 

99. 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

 

实现代码:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

public class Test001 {

    public static void main(String[] args)throws Exception {

       System.out.print("请输入需要排列的数字(以空格键隔开,不能输入小数):");

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       Scanner scan = new Scanner(stdin.readLine());

       List<Long> p = new ArrayList<Long>();

       for (int i = 0; scan.hasNext(); i++) {

           p.add(scan.nextLong());

       }

       Collections.sort(p); // 建议在JDK多看看这个方法

       System.out.print("排列后的顺序为:");

       for (int i = 0; i < p.size(); i++)

           System.out.print(p.get(i) +" ");

       System.out.println("");

       System.out.print("请输入需要插入的数值(不能输入小数)");

       long m = new Long(stdin.readLine());

       for (int i = 0; i < p.size(); i++) {

           if (m >= p.get(i))// 等号是必要的,可以减少一个数据的后移

              continue;

           p.add(i, m);

           break;

       }

       System.out.print("插入后的顺序为:");

       for (int i = 0; i < p.size(); i++)

           System.out.print(p.get(i) +" ");

    }

}

 

100. 输入两个正整数m和n,输出其最大公约数和最小公倍数

 

实现代码:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Vector;

public class Test001 {

    public static void fenJie(Vector<Integer> m,int n) {

       for (int i = 2; i <= n; i++) {

           if (n == i) {

              m.addElement(i);

              return;

           }

           if (n > i && (n % i == 0)) {

              n = n / i;

              m.addElement(i);

              fenJie(m, n);

              break;

           }

       }

    }

    public static int gongBeiShu(Vector<Integer> m,int a, int b) {

       int chengji = 1;

       if (a % b == 0)

           return a;

       for (int i = 0; i < m.size(); i++) {

           chengji = chengji * m.elementAt(i);

           if ((a * chengji) % b == 0) {

              return a * chengji;

           }

       }

       return a * b;

    }

    public static int bigYinZi(int a,int b) {

       int r = a % b;

       int m = 0;

       if (r == 0)

           return b;

       else {

           a = b;

           b = r;

           m = bigYinZi(a, b);

           return m;

       }

    }

    public static void main(String[] args)throws IOException {

       Integer shu1 = 0;

       Integer shu2 = 0;

       int t = 0;

       Vector<Integer> pool = new Vector<Integer>();

       BufferedReader stdin = new BufferedReader(new InputStreamReader(

              System.in));

       System.out.print("请输入第一个正整数:");

       shu1 = (new Integer(stdin.readLine()));

       System.out.print("请输入第二个正整数:");

       shu2 = (new Integer(stdin.readLine()));

       if (shu1 < shu2) {

           t = shu2;

           shu2 = shu1;

           shu1 = t;

       }

       fenJie(pool, shu2);

       t = bigYinZi(shu1, shu2);

       System.out.println("最大公约数是:" + t);

       t = gongBeiShu(pool, shu1, shu2);

       System.out.print("最小公倍数:" + t);

    }

}