蓝桥杯java历年真题及答案整理41~56

41. 低碳生活大奖赛

/*
某电视台举办了低碳生活大奖赛。题目的计分规则相当奇怪:
每位选手需要回答10个问题(其编号为1到10),越后面越有难度。
答对的,当前分数翻倍;答错了则扣掉与题号相同的分数(选手必须回答问题,不回答按错误处理)。
每位选手都有一个起步的分数为10分。
某获胜选手最终得分刚好是100分,如果不让你看比赛过程,
你能推断出他(她)哪个题目答对了,哪个题目答错了吗?
如果把答对的记为1,答错的记为0,则10个题目的回答情况可以用仅含有1和0的串来表示。
例如:0010110011 就是可能的情况。
你的任务是算出所有可能情况。每个答案占一行。
*/
package Question40_49;

public class Question41 {
    public static void exeForward(int question[],int index,int sum,int needSum) {
        if(index<=10){
            for (int i = 0; i <= 1; i++) {
                question[index]=i;
                int t=sum;
                if(i==0){
                    sum-=index;
                }else {
                    sum*=2;
                }
                exeForward(question, index+1, sum, needSum);
                question[index]=(i==1?0:1);
                sum=t;
            }
        }else {
            if(sum==needSum){
                for (int i = 1; i <= 10; i++) {
                    System.out.print(question[i]);
                }
                System.out.println();
                return;
            }else {
                return;
            }
        }

    }
    public static void main(String[] args) {
        int needSum=100;
        int question[]=new int[12];
        exeForward(question, 1, 10, 100);
    }
}

运行结果:
0010110011
0111010000
1011010000


12345678910
0010110011
0111010000
1011010000

42. 益智玩具

import java.math.BigInteger;

/*
* 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。
大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。
大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上(可以借助第三根柱子做缓冲)。
并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。
如图【1.jpg】是现代“山寨”版的该玩具。64个圆盘太多了,所以减为7个,
金刚石和黄金都以木头代替了......但道理是相同的。
据说完成大梵天的命令需要太多的移动次数,以至被认为完成之时就是世界末日!
你的任务是精确计算出到底需要移动多少次。
很明显,如果只有2个圆盘,需要移动3次。
圆盘数为3,则需要移动7次。
那么64个呢?
答案写在“解答.txt”中,不要写在这里!
*/
package Question40_49;

import java.math.BigInteger;
import java.util.Scanner;

public class Question42MustRemember {
    public static BigInteger count=BigInteger.valueOf(0);
    public static void move(int from,int to) {
    //	System.out.println(from+"--->"+to);
        count=count.add(BigInteger.valueOf(1));
    }
    public static void hanoi(int n,int from,int to,int assist) {
        if(n>0){
            hanoi(n-1, from, assist, to);
            move(from, to);
            hanoi(n-1, assist, to, from);
        }

    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
    //	int n=scanner.nextInt();
        for (int i = 1; i <= 100; i++) {
            long startTime = System.currentTimeMillis();	// 程序开始时间
            hanoi(i, 1, 2, 3);
            long endTime = System.currentTimeMillis();	// 程序结束时间
            System.out.println("n="+String.format("%4d", i)+"  时,耗时"+String.format("%f", (endTime-startTime)/1000f)+"秒,  移动了"+count+" 次  ");
            count=BigInteger.valueOf(0);

        }
    }
}

43. 拼酒量

/*
有一群海盗(不多于20人),在船上比拼酒量。过程如下:打开一瓶酒,
所有在场的人平分喝下,有几个人倒下了。再打开一瓶酒平分,又有倒下的,
再次重复...... 直到开了第4瓶酒,坐着的已经所剩无几,海盗船长也在其中。
当第4瓶酒平分喝下后,大家都倒下了。    等船长醒来,发现海盗船搁浅了。
他在航海日志中写到:“......昨天,我正好喝了一瓶.......奉劝大家,开船不喝酒,喝酒别开船......”
请你根据这些信息,推断开始有多少人,每一轮喝下来还剩多少人。
如果有多个可能的答案,请列出所有答案,每个答案占一行。
格式是:人数,人数,...
例如,有一种可能是:20,5,4,2,0
答案写在“解答.txt”中,不要写在这里!
*/
package Question40_49;

public class Question43 {

    public static void main(String[] args) {
        int person[]=new int[5];
        for (person[1] = 1;  person[1]<=20 ; person[1]++) {
            for (person[2] = 1;  person[2]<person[1] ; person[2]++){
                for (person[3] = 1;  person[3]<person[2] ; person[3]++){
                    for (person[4] = 1;  person[4]<person[3] ; person[4]++){
                        if ((1.0)/person[1]+(1.0)/person[2]+(1.0)/person[3]+(1.0)/person[4]==1) {
                            for (int i = 1; i <= 4; i++) {
                                System.out.print(person[i]);
                                if(i==4){
                                    System.out.println(",0");
                                }else {
                                    System.out.print(",");
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
运行结果:
20,5,4,2,0
18,9,3,2,0
15,10,3,2,0
12,6,4,2,0

44. 黄金分割数

import java.math.BigDecimal;

/*
* 黄金分割数0.618与美学有重要的关系。舞台上报幕员所站的位置大约就是舞台宽度的0.618处,
墙上的画像一般也挂在房间高度的0.618处,甚至股票的波动据说也能找到0.618的影子....
黄金分割数是个无理数,也就是无法表示为两个整数的比值。
0.618只是它的近似值,其真值可以通过对5开方减去1再除以2来获得,
我们取它的一个较精确的近似值:0.618034
有趣的是,一些简单的数列中也会包含这个无理数,这很令数学家震惊!
1 3 4 7 11 18 29 47 .... 称为“鲁卡斯队列”。它后面的每一个项都是前边两项的和。
如果观察前后两项的比值,即:1/3,3/4,4/7,7/11,11/18 ... 会发现它越来越接近于黄金分割数!
你的任务就是计算出从哪一项开始,这个比值四舍五入后已经达到了与0.618034一致的精度。
请写出该比值。格式是:分子/分母。比如:29/47
*/
package Question40_49;

public class Question44 {


    public static void main(String[] args) {
        int a=1,b=3,t;
        while(true){
            if(Math.abs((double)a/b-0.618034)<0.000001){
                System.out.println(a+"/"+b+" = "+(double)a/b);
                break;
            }
            t=a;
            a=b;
            b+=t;
        }
    }
}
运行结果:
1364/2207

45. 坐标

/*
*	已知平面上若干个点的坐标。

需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。
比如有4个点:a,b,c,d,则平均距离是指:ab, ac, ad, bc, bd, cd 这6个距离的平均值。
每个点的坐标表示为:横坐标,纵坐标
坐标的取值范围是:1~1000
所有点的坐标记录在in.txt中,请读入该文件,然后计算。
注意:我们测试您的程序的时候,in.txt 可能会很大,比如包含上万条记录。
举例:
如果,in.txt 内的值为:
10,10
20,20
80,50
10,20
20,10
则程序应该输出:
11.38
*/
package Question40_49;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class Question45ErrorDontUnderstand {

    public static double exeForward(Vector<Point>vpoints,Vector<Point>tpoints,int index) {
//		for (Point point : tpoints) {
//			System.out.print("["+point.x+","+point.y+"]");
//		}
//		System.out.println();
        if(tpoints.size()==4){
            double t=tpoints.get(0).distance(tpoints.get(1))+tpoints.get(0).distance(tpoints.get(2))+tpoints.get(0).distance(tpoints.get(3))
                    +tpoints.get(1).distance(tpoints.get(2))+tpoints.get(1).distance(tpoints.get(3))+tpoints.get(2).distance(tpoints.get(3));
            t/=6;
            //System.out.println(t);
            return t;
        }else if (index<vpoints.size()) {
            Vector<Point>vector1=new Vector<Point>(tpoints);
            Vector<Point>vector2=new Vector<Point>(tpoints);
            vector2.add(vpoints.get(index));


            double min1=exeForward(vpoints, vector1, index+1);

            double min2=exeForward(vpoints, vector2, index+1);


            return Math.min(min1, min2);
        }

        return Double.MAX_VALUE;
    }
    public static void main(String[] args) {
        try {
            File file=new File("in.txt");
            FileInputStream fileInputStream=new FileInputStream(file);
            InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
            Vector<Point>vpoints=new Vector<Point>();
            String ts;
            while((ts=bufferedReader.readLine())!=null){
                String tss[]=ts.split("\,");
                Point point=new Point(Integer.parseInt(tss[0]), Integer.parseInt(tss[1]));
                vpoints.add(point);
            }


            Vector<Point> tpoints=new Vector<Point>();

            System.out.println(String.format("%.2f", exeForward(vpoints, tpoints, 0)));



            bufferedReader.close();
        } catch (FileNotFoundException e) {
            // TODO: handle exception
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}运行结果:
11.38

46. 递归算法

package temp;
/**
* 递归算法:将数据分为两部分,递归将数据从左侧移右侧实现全排列
*
* @param datas
* @param target
*/
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class T06 {
    // 输出
    public static void print(List target){
        for(Object o: target){
            System.out.print(o);
        }
        System.out.println();
    }
    // 递归排列
    public static void sort(List datas,List target,int n){
        if(target.size()==n){
            print(target);
            return;
        }
        for(int i=0;i<datas.size();i++){
            List newDatas = new ArrayList(datas);
            List newTarget = new ArrayList(target);
            newTarget.add(newDatas.get(i));
            newDatas.remove(i);

            sort(newDatas,newTarget,n);
        }
    }
    // 主函数
    public static void main(String[] args){
        String[] s = {"a","b","c"};
        sort(Arrays.asList(s),new ArrayList(),s.length);
    }
}
运行结果:
abc
acb
bac
bca
cab
cba


方法二:
public class AllSort{
     public static void perm(String[] buf,int start,int end){    	
            if(start==end){//当只要求对数组中一个字母进行全排列时,只要按该数组输出即可
                for(int i=0;i<=end;i++){
                    System.out.print(buf[i]);
                }
                System.out.println();
            }
            else{//多个字母全排列
                for(int i=start;i<=end;i++){
                    String temp=buf[start];//交换数组第一个元素与后续的元素
                    buf[start]=buf[i];
                    buf[i]=temp;

                    perm(buf,start+1,end);//后续元素递归全排列

                    temp=buf[start];//将交换后的数组还原
                    buf[start]=buf[i];
                    buf[i]=temp;
                }
            }
        }
    public static void main(String[] args) {
        String buf[]={"a","b","c"};
        perm(buf,0,buf.length-1);
    }
}
运行结果:
abc
acb
bac
bca
cba
cab

47. 字符串全拜列

/*
* 字符串全拜列
*/
public class T03{
    // 输出字符数组
    public static void print(char[] arr){
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]);
        }
        System.out.println();
    }
    // 递归遍历
    public static void perm(char[] arr,int start,int end){
        if(start==end){
            print(arr);	// 输出
        }else{
            for(int i=start;i<=end;i++){
                // 换位
                char c = arr[start];
                arr[start] = arr[i];
                arr[i] = c;
                // 递归
                perm(arr,start+1,end);
                // 恢复数组原位
                c = arr[start];
                arr[start] = arr[i];
                arr[i] = c;
            }
        }
    }
    // 字符串转字符数组
    public static void f(String s){
        char[] arr = s.toCharArray();
        perm(arr,0,arr.length-1);
    }
    public static void main(String[] args){
        String s = "abc";
        f(s);
    }
}
运行结果:
abc
acb
bac
bca
cba
cab

48. 亲密数

/*
假设有a、b两个数,若a的所有因子之和等于b,b的所有因子之和等于a,
并且a不等于b,则称a和b是一对亲密数。如284和220就是一对亲密数。
分析:
若要找出10000以内的亲密数,可使用以下算法:
(1)对每一个数i,将其因子分解出来,并将因子保存到一个数组中,再将因子之和保存到变量b1。
(2)将因子之和b1再进行因子分解,并将因子保存到一个数组中,将因子之和保存到变量b2中。
(3)若b2等于i,并且b1不等于b2,则找到一对亲密数为i和b1,可将其输出。
(4)重复步骤(1)~(3),即可找出指定范围的亲密数。
*/
package Question40_49;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

public class Question48 {
    public static int obtain(int n) {
        int sum = 0;
        for (int i = 1; i < n; i++) {
            if (n % i == 0) {
                sum += i;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        Set<Set<Integer>> sets = new LinkedHashSet<Set<Integer>>();
        for (int i = 1; i <= n; i++) {
            int t = obtain(i);
            if (i != t) {
                if (obtain(t) == i) {
                    Set<Integer> set = new LinkedHashSet<Integer>();
                    set.add(i);
                    set.add(t);
                    sets.add(set);
                }

            }
        }
        for (Iterator iterator = sets.iterator(); iterator.hasNext();) {

            Set<Integer> set = (Set<Integer>) iterator.next();
            for (Iterator iterator2 = set.iterator(); iterator2.hasNext();) {
                Integer integer = (Integer) iterator2.next();
                System.out.print(integer);
                if (iterator2.hasNext() == false) {
                    System.out.println();
                } else {
                    System.out.print("   ");
                }

            }

        }

    }
}
运行结果:
10000
220   284
1184   1210
2620   2924
5020   5564
6232   6368

49. 标题: 世纪末的星期

    曾有邪教称1999年12月31日是世界末日。当然该谣言已经不攻自破。

    还有人称今后的某个世纪末的12月31日,如果是星期一则会....

    有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!! 

    于是,“谣言制造商”又修改为星期日......

    1999年的12月31日是星期五,请问:未来哪一个离我们最近的一个世纪末年(即xx99年)的12月31日正好是星期天(即星期日)?

    请回答该年份(只写这个4位整数,不要写12月31等多余信息)
import java.util.Scanner;


public class Question1UnAmendment {
    public static void main(String[] args) {
        int day=0;
        for (int year = 2000; ; year++) {
            day+=new Question1UnAmendment().dayOfyear(year);
            if(year%100==99&&day%7==2){
                System.out.println(year);
                break;
            }
        }
    }
    private int dayOfyear(int year) {
        if ((year%4==0&&year%100!=0)||year%400==0) {
            return 366;
        }
        else {
            return 365;
        }
    }
}

50. 标题: 马虎的算式

    小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

    有一次,老师出的题目是:36 x 495 = ?

    他却给抄成了:396 x 45 = ?

    但结果却很戏剧性,他的答案竟然是对的!!

    因为 36 * 495 = 396 * 45 = 17820

    类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

    假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

    能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?


请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。


答案直接通过浏览器提交。
注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

import java.util.Scanner;

public class Question2UnAmendment {
    public static void main(String[] args) {
        int a, b, c, d, e;
        int count=0;
        for (a = 1; a < 10; a++) {
            for (b = 1; b < 10; b++) {
                if (b != a) {
                    for (c = 1; c < 10; c++) {
                        if (c != b && c != a) {
                            for (d = 1; d < 10; d++) {
                                if (d != a && d != b && d != c) {
                                    for (e = 1; e < 10; e++) {
                                        if (e != a && e != b && e != c
                                                && e != d) {
                                            if ((a * 10 + b)
                                                    * (c * 100 + d * 10 + e) == (a
                                                    * 100 + d * 10 + b)
                                                    * (c * 10 + e)) {
                                                System.out.printf("%d%d*%d%d%d = %d%d%d*%d%d
",a, b, c, d, e,a, d, b, c, e);
                                                count++;
                                            }
                                        }
                                    }
                                }

                            }
                        }

                    }
                }
            }
        }
        System.out.println(count);
    }
}

51. 标题: 振兴中华

    小明参加了学校的趣味运动会,其中的一个项目是:跳格子。

    地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)

从我做起振
我做起振兴
做起振兴中
起振兴中华


    比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。


    要求跳过的路线刚好构成“从我做起振兴中华”这句话。

    请你帮助小明算一算他一共有多少种可能的跳跃路线呢?

答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。


import java.util.Scanner;

public class Question3UnAmendment {
    String indexsString[] = { "从", "我", "做", "起", "振", "兴", "中", "华" };
    String s[][] = {  
{ "从", "我", "做", "起", "振" },
{ "我", "做", "起", "振", "兴" },
                { "做", "起", "振", "兴", "中" },
 { "起", "振", "兴", "中", "华" } 
};

    private int stepForward(int i, int j, int index) {
        if (i < 4 && j < 5) {
            if (s[i][j] == indexsString[index]) {
                if (indexsString[index] == "华") {
                    return 1;
                } else {
                    return (new Question3UnAmendment()).stepForward(i + 1, j,
                            index + 1)
                            + (new Question3UnAmendment()).stepForward(i,
                                    j + 1, index + 1);
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println((new Question3UnAmendment()).stepForward(0, 0, 0));
    }
}

52. 标题:有理数类

    有理数就是可以表示为两个整数的比值的数字。一般情况下,我们用近似的小数表示。但有些时候,不允许出现误差,必须用两个整数来表示一个有理数。

    这时,我们可以建立一个“有理数类”,下面的代码初步实现了这个目标。为了简明,它只提供了加法和乘法运算。

class Rational
{
    private long ra;
    private long rb;

    private long gcd(long a, long b){
        if(b==0) return a;
        return gcd(b,a%b);
    }
    public Rational(long a, long b){
        ra = a;
        rb = b;	
        long k = gcd(ra,rb);
        if(k>1){ //需要约分
            ra /= k;  
            rb /= k;
        }
    }
    // 加法
    public Rational add(Rational x){
        return new Rational(this.ra*x.rb+this.rb+x.ra, this.rb*x.rb);  //填空位置
    }
    // 乘法
    public Rational mul(Rational x){
        return new Rational(ra*x.ra, rb*x.rb);
    }
    public String toString(){
        if(rb==1) return "" + ra;
        return ra + "/" + rb;
    }
}

使用该类的示例:
    Rational a = new Rational(1,3);
    Rational b = new Rational(1,6);
    Rational c = a.add(b);
    System.out.println(a + "+" + b + "=" + c);


请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!


53.	标题:三部排序

    一般的排序有许多经典算法,如快速排序、希尔排序等。

    但实际应用时,经常会或多或少有一些特殊的要求。我们没必要套用那些经典算法,可以根据实际情况建立更好的解法。

    比如,对一个整型数组中的数字进行分类排序:

    使得负数都靠左端,正数都靠右端,0在中部。注意问题的特点是:负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!!

    以下的程序实现了该目标。

    static void sort(int[] x)
    {
        int p = 0;
        int left = 0;
        int right = x.length-1;

        while(p<=right){
            if(x[p]<0){
                int t = x[left];
                x[left] = x[p];
                x[p] = t;
                left++;
                p++;
            }
            else if(x[p]>0){
                int t = x[right];
                x[right] = x[p];
                x[p] = t;
                right--;			
            }
            else{
                        p++       ;  //代码填空位置
            }
        }
    }

54. 标题:错误票据

    某涉密单位下发了某种票据,并要在年终全部收回。

    每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。

    因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。

    你的任务是通过编程,找出断号的ID和重号的ID。

    假设断号不可能发生在最大和最小号。

要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000)
每个整数代表一个ID号。

要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID

例如:
用户输入:
2
5 6 8 11 9 
10 12 9

则程序输出:
7 9


再例如:
用户输入:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158 
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

则程序输出:
105 120


资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗  < 2000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

import java.io.IOException;
import java.util.PriorityQueue;
import java.util.Scanner;


class Question7UnAmendment{


    public static void main(String[] args) throws IOException {
        String s[]=new String[10];
        int lines;
        char ch;
        PriorityQueue<Integer>priorityQueue=new PriorityQueue<Integer>();
        lines= System.in.read()-'0';
        lines++;
        int sum=0;
        while(lines>0){
            ch=(char) System.in.read();
            if(ch==' '){
                if(sum!=0){
                    priorityQueue.add(sum);
                    sum=0;
                }
            }else if(ch>='0'&&ch<='9'){
                if(ch=='0'&&sum==0){
                    priorityQueue.add(0);
                    continue;
                }
                sum*=10;
                sum+=ch-'0';
            }else if(ch=='
'){
                lines--;
                if(sum!=0){
                    priorityQueue.add(sum);
                    sum=0;
                }
            }
        }
        int m =-1,n = -1;
        int i=priorityQueue.peek();
        while(!priorityQueue.isEmpty()){
            if(i!=priorityQueue.peek()){
                if(priorityQueue.peek()-i==1){
                    m=i;
                    priorityQueue.add(i);
                }else if (i-priorityQueue.peek()==1) {
                    n=priorityQueue.peek();
                    priorityQueue.poll();
                }
            }
            i++;
            priorityQueue.poll();
        }
        System.out.println(m+" "+n);
    }
}

55. 标题:幸运数

    幸运数是波兰数学家乌拉姆命名的。它采用与生成素数类似的“筛法”生成。

    首先从1开始写出自然数1,2,3,4,5,6,....

    1 就是第一个幸运数。
    我们从2这个数开始。把所有序号能被2整除的项删除,变为:

    1 _ 3 _ 5 _ 7 _ 9 ....

    把它们缩紧,重新记序,为:

    1 3 5 7 9 .... 。这时,3为第2个幸运数,然后把所有能被3整除的序号位置的数删去。注意,是序号位置,不是那个数本身能否被3整除!! 删除的应该是5,11, 17, ...

    此时7为第3个幸运数,然后再删去序号位置能被7整除的(19,39,...) 

    最后剩下的序列类似:

    1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, ...

本题要求:

输入两个正整数m n, 用空格分开 (m < n < 1000*1000)
程序输出 位于m和n之间的幸运数的个数(不包含m和n)。

例如:
用户输入:
1 20
程序输出:
5

例如:
用户输入:
30 69
程序输出:
8



资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗  < 2000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。


import java.util.Scanner;
import java.util.Vector;




class Question8UnAmendment{


    public static void main(String[] args) {
        Vector<Integer> vector=new Vector<Integer>();
        int m,n;

        Scanner scanner=new Scanner(System.in);
        m=scanner.nextInt();
        n=scanner.nextInt();
        for(int i=0;i<=n;i++){
            vector.add(i);
        }


        //除掉序号能被2整除的
        for(int i=vector.size()-1;i>=1;i--){
            if(i%2==0){
                vector.remove(i);
            }
        }




        int index=2;
        while(index<vector.size()){
            for (int i = vector.size()-1; i>=1; i--) {
                if(i%vector.elementAt(index)==0){
                    vector.remove(i);
                }
            }
            index++;
        }



        int count=0;
        for(int i=vector.size()-1;i>=1;i--){
            if(vector.elementAt(i)==n){
                continue;
            }
            else if(vector.elementAt(i)>m){
                count++;
            }else {
                break;
            }
        }
        System.out.println(count);
    }
}

56. 标题:连号区间数

    小明这些天一直在思考这样一个奇怪而有趣的问题:

    在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是:

    如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增排序后能得到一个长度为R-L+1的“连续”数列,则称这个区间连号区间。

    当N很小的时候,小明可以很快地算出答案,但是当N变大的时候,问题就不是那么简单了,现在小明需要你的帮助。

输入格式:
第一行是一个正整数N (1 <= N <= 50000), 表示全排列的规模。
第二行是N个不同的数字Pi(1 <= Pi <= N), 表示这N个数字的某一全排列。

输出格式:
输出一个整数,表示不同连号区间的数目。

示例:
用户输入:
4
3 2 4 1

程序应输出:
7

用户输入:
5
3 4 2 5 1

程序应输出:
9

解释:
第一个用例中,有7个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [2,2], [3,3], [4,4]
第二个用例中,有9个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [3,3], [4,4], [5,5]


资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗  < 5000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
import java.util.Scanner;
import java.util.Vector;






class Question10UnAmendment{
    Vector<Integer> vectorPrimary=new Vector<Integer>();
    private boolean isContinuous(int begin,int end) {
        int max=vectorPrimary.elementAt(begin),min=vectorPrimary.elementAt(begin);
        for(int i=begin;i<=end;i++){
            if(max<vectorPrimary.elementAt(i)){
                max=vectorPrimary.elementAt(i);
                continue;
            }
            if(min>vectorPrimary.elementAt(i)){
                min=vectorPrimary.elementAt(i);
            }
        }
        if(max-min==end-begin){
            return true;
        }else {
            return false;
        }

    }
    public Vector<Integer> getVectorPrimary() {
        return vectorPrimary;
    }

    public void setVectorPrimary(Vector<Integer> vectorPrimary) {
        this.vectorPrimary = vectorPrimary;
    }


    public static void main(String[] args) {
        int N,t,count=0;
        Scanner scanner=new Scanner(System.in);
        Question10UnAmendment question10UnAmendment=new Question10UnAmendment();
        question10UnAmendment.getVectorPrimary().add(0);

        N=scanner.nextInt();
        for(int i=1;i<=N;i++){
            t=scanner.nextInt();
            question10UnAmendment.getVectorPrimary().add(t);
        }
        for(int i=1;i<question10UnAmendment.getVectorPrimary().size();i++){
            for (int j = i+1; j < question10UnAmendment.getVectorPrimary().size(); j++) {
                if(question10UnAmendment.isContinuous(i, j)){
                    count++;
                }
            }
        }
        System.out.println(count+N);


    }


}

57. 标题: 黄金连分数

    黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现。有时需要把这个数字求得很精确。

    对于某些精密工程,常数的精度很重要。也许你听说过哈勃太空望远镜,它首次升空后就发现了一处人工加工错误,对那样一个庞然大物,其实只是镜面加工时有比头发丝还细许多倍的一处错误而已,却使它成了“近视眼”!!


    言归正传,我们如何求得黄金分割数的尽可能精确的值呢?有许多方法。

    比较简单的一种是用连分数:

                  1
    黄金数 = ---------------------
                        1
             1 + -----------------
                          1
                 1 + -------------
                            1
                     1 + ---------
                          1 + ...



    这个连分数计算的“层数”越多,它的值越接近黄金分割数。

    请你利用这一特性,求出黄金分割数的足够精确值,要求四舍五入到小数点后100位。

    小数点后3位的值为:0.618
    小数点后4位的值为:0.6180
    小数点后5位的值为:0.61803
    小数点后7位的值为:0.6180340
   (注意尾部的0,不能忽略)

你的任务是:写出精确到小数点后100位精度的黄金分割值。

注意:尾数的四舍五入! 尾数是0也要保留!

显然答案是一个小数,其小数点后有100位数字,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。


import java.math.BigInteger;

public class test5 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigInteger one = BigInteger.ONE;
        BigInteger ten = BigInteger.TEN;
        BigInteger a = one;
        BigInteger b = one.add(one.add(one));
        BigInteger c = b;
        BigInteger sum = BigInteger.ONE;
        for(int i=0;i<300;i++){//需要精确到100位,所以循环次数要适当
            sum = a;
            for(int j=0;j<101;j++){//需要把小数点向右移动101位
            sum = ten.multiply(sum);
            }
            BigInteger res = sum.divide(c);
            c = a.add(b);
            a = b;
            b = c;
            if(res.toString().length()==101){//要判断到101位,需要四舍五入

                System.out.println(res);

            }
        }

    }
}

58. 标题:带分数

    100 可以表示为带分数的形式:100 = 3 + 69258 / 714

    还可以表示为:100 = 82 + 3546 / 197

    注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

    类似这样的带分数,100 有 11 种表示法。

题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!


例如:
用户输入:
100
程序输出:
11

再例如:
用户输入:
105
程序输出:
6


资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗  < 3000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

import java.util.Scanner;
public class DaiFenShu
{
    static int kinds=0;
    static int a[]=new int[10];
    static boolean vis[]=new boolean[10];//全排列避免重复
    static void check(int a[],int n,int num)
    {
        int begin=String.valueOf(num).length();
        String str="";
        for(int i=1;i<n;i++) str+=a[i];
        for(int k=1;k<begin+1;k++)
        {
            int num1=Integer.valueOf(str.substring(0,k));
            if(num1<num)
            {
                for(int j=k+(n-k)/2;j<n-1;j++)
                {
                    int num2=Integer.valueOf(str.substring(k,j));
                    int num3=Integer.valueOf(str.substring(j,n-1));
                    if(num2>num3 && num2%num3==0)
                    {
                        if(num==num1+num2/num3) 
                        {    
//                            System.out.println(num+" = "+num1+"+"+num2+"/"+num3);
                            kinds++;
                        }
                    }
                }
            }
        }
    }
    static void dfs(int start,int n,int num)
    {
        if(start==n)
        {
            check(a,n,num);
        }
        else
        {
            for(int i=1;i<n;i++)
            {
                if(!vis[i])
                {
                    a[start]=i;
                    vis[i]=true;
                    dfs(start+1,n,num);
                    vis[i]=false;
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        int num=cin.nextInt();
        long start=System.currentTimeMillis();
        dfs(1,10,num);
        long end=System.currentTimeMillis();
//        System.out.println(end-start);//运行时间
        System.out.println(kinds);    
    }
}
原文地址:https://www.cnblogs.com/liyuquan/p/8678367.html