分享

JAVA经典算法50题(5)【面试+工作】

 Java帮帮 2020-01-02


JAVA经典算法50题(5)【面试+工作】

【程序41】   题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
本题源码:
public class Demo41 {
    static int ts = 0;// 桃子总数
    int fs = 1;// 记录分的次数
    static int hs = 5;// 猴子数
    int tsscope = 5000;// 桃子数的取值范围,太大容易溢出。
    public int fT(int t) {
        if (t == tsscope) {
            // 当桃子数到了最大的取值范围时取消递归
            System.out.println("结束");
            return 0;
        } else {
            if ((t - 1) % hs == 0 && fs <= hs) {
                if (fs == hs) {
                    System.out.println("桃子数=" + ts + "时满足分桃条件");
                }
                fs += 1;
                return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的总数
            } else {
                // 没满足条件
                fs = 1;// 分的次数重置为1
                return fT(ts += 1);// 桃子数加+1
            }
        }
    }
    public static void main(String[] args) {
        new Demo41().fT(0);
    }
}
个人修改:
public class Demo41 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 6;; i++) {// 最少6个分最后一次
            sum = i;// 桃子数
            for (int j = 0; j < 5; j++) {// 分的次数循环
                if ((sum - 1) % 5 == 0 && j < 5) {// 如果扔一个后能均分5份,继续分
                    sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子数
                    if (j == 4) {// 如果已分5次,且仍能除尽,输出,退出程序
                        System.out.println(i);
                        System.exit(0);
                    }
                }
            }
        }
    }
}

【程序42】   题目:809*??=800*??+9*??+1。其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
(本题为无解,去掉1有解)
public class Demo42 {
    public static void main(String[] args) {
        for (int i = 10; i < 100; i++) {
            if (809 * i == (800 * i + 9 * i + 1) && 8 * i >= 10 && 8 * i < 100
                    && 9 * i >= 100 && 9 * i < 1000) {
                System.out.println("?? =" + i);
                System.out.println("809*??="+ 809 * i);
                System.exit(0);
            }
        }
    }
}

【程序43】   题目:求0—7所能组成的奇数个数。
暴力算法:
public class Demo43 {
    public static boolean isJiShu(int n) {
        if (n % 2 != 0) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean fun(char c) {
        if (c >= '0' && c <= '7') {
            return true;
        } else {
            return false;
        }
    }
    public static void main(String[] args) {
        int count = 0;
        String s;
        for (int i = 0; i < 100000000; i++) {
            s = "" + i;
            boolean flag = true;
            char[] c = s.toCharArray();
            for (int j = 0; j < c.length; j++) {
                if (!fun(c[j])) {
                    flag = false;
                    break;
                }
            }
            if (flag && isJiShu(i)) {
                count++;
            }
            s = "";
        }
        System.out.println("共" + count + "个。");
    }
}
数学算法:
public class Demo43 {
    public static void main(String[] args) {
        // 因为是奇数,所以个位只能是1,3,5,7共4种,前面可随便排列
        int count = 4;// 个位的4种
        // 2位时,十位有8种,个位4种,8×4
        // 3位时,8×8×4……
        for (int i = 1; i < 8; i++) {
            count = 8 * count;
            System.out.println("count:" + count);
        }
    }
}
个人算法:
//组成1位数是4个。
//组成2位数是7*4个。
//组成3位数是7*8*4个。
//组成4位数是7*8*8*4个。
//......
public class Demo43 { 
    public static void main (String[] args) { 
        int sum=4; 
        int j;
        System.out.println("组成1位数是 "+sum+" 个"); 
        sum=sum*7; 
        System.out.println("组成2位数是 "+sum+" 个");
        for(j=3;j<=9;j++){ 
            sum=sum*8; 
            System.out.println("组成"+j+"位数是 "+sum+" 个"); 
        } 
    }
}

【程序44】   题目:一个偶数总能表示为两个素数之和。(注:哥德巴赫猜想是想证明对任何大于6的自然数n之内的所有偶数可以表示为两个素数之和)
public class Demo44 {
    public static boolean isSuShu(int x) {
        if (x == 0 || x == 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        // 求了下100以内的情况
        for (int i = 0; i < 100; i = i + 2) {
            for (int j = 0; j <= (i + 1) / 2; j++) {
                if (isSuShu(j) && isSuShu(i - j)) {
                    System.out.println(i + "=" + j + "+" + (i - j));
                }
            }
        }
    }
}

public class Demo44{    
    public static void main(String[] args){    
        for (int i=6;i<=100 ;i+=2 ){    
            for (int j=2;j<100 ;j++ ){        
                if(!isPrime(j)||!isPrime(i-j)||j>=i) 
                continue;            
                System.out.println(i+"="+j+"+"+(i-j));            
                break;        
            }        
        }    
    }   

    public static boolean isPrime(int n){    
        for (int i=2;i<n ;i++ ){        
            if(n%i==0)return false;    
        }            
        return true;    
    }
}

【程序45】   题目:(1)判断几个9能被一个素数整除。(2)判断一个整数能被几个9整除。(原题:一个素数能被几个9整除)
(一)
public class Demo45 {
    public static boolean isSuShu(int x) {
        if (x == 0 || x == 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int[] a = new int[100];
        int n = 0;
        int num = 0;
        // 长度100的素数数组
        while (n < 100) {
            if (isSuShu(num)) {
                a[n] = num;
                n++;
                num++;
            } else {
                num++;
            }
        }
        /* for (int t : a) {
         System.out.println(t);
         }*/
        String s = "9";
        int index = 0;
        while (s.length() < 9) {
            if (new Integer(s).intValue() % a[index] == 0) {
                System.out.println(s + "%" + a[index] + "=0");
                if (index < 100 - 1) {
                    index++;
                } else {
                    index = 0;
                    s = s + "9";
                }
                // System.exit(0);
            } else {
                if (index < 100 - 1) {
                    index++;
                } else {
                    index = 0;
                    s = s + "9";
                }
            }
        }
    }
}

(二)
import java.util.*; public class Demo45 {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int num = in.nextInt();
        int tmp = num;
        int count = 0;
        for(int i = 0 ; tmp%9 == 0 ;){
            tmp = tmp/9;
            count ++;
        }
        System.out.println(num+" 能够被 "+count+" 个9 整除。");
    }
}


【程序46】   题目:两个字符串连接程序。
import java.util.Scanner;
public class Demo46 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入第一个字符串:");
        String s1 = in.next();
        System.out.println("输入第一个字符串:");
        String s2 = in.next();
        System.out.println("连接后:\n" + s1 + s2);
    }
}

import java.util.*;
public class Demo46 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String str1 = in.nextLine();
        System.out.print("请再输入一个字符串:");
        String str2 = in.nextLine();
        String str = str1+str2;
        System.out.println("连接后的字符串是:"+str);
    }
}

【程序47】   题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的。
import java.util.*;
public class Demo47 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n=1,num;
        while(n<=7){
            do{
                System.out.print("请输入一个1--50 之间的整数:");
                num= s.nextInt();
            }while(num<1||num>50);
            for(int i=1;i<=num;i++)
            {System.out.print("*");
            }
            System.out.println();
            n ++;
        }
    } 
}

import java.util.Scanner;
public class Demo47 {
    public static void print(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print("*");

   }
        System.out.println();
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 7; i++) {
            int temp = in.nextInt();
            print(temp);
        }
    }
}

【程序48】   题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
import java.util.Scanner;
public class Demo48{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个4位数字:");
        String str = in.next();
        if (!((str).matches("\\d{4}"))) {
            System.out.println("输入的不是4位数字!");
            System.exit(0);
        }
        char[] c = str.toCharArray();
        int[] a = new int[4];
        for (int i = 0; i < a.length; i++) {
            a[i] = ((int) (c[i] - '0') + 5) % 10;
        }
        int t;
        t = a[0];
        a[0] = a[3];
        a[3] = t;
        t = a[1];
        a[1] = a[2];
        a[2] = t;
        System.out.println("结果是:" + a[0] + a[1] + a[2] + a[3]);
    }
}

import java.util.*; 
public class Demo48 { 
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in); 
        int num=0,temp; 
        do{ 
            System.out.print("请输入一个4位正整数:");
            num = s.nextInt(); 
        }while (num<1000||num>9999);
        int a[]=new int[4]; 
        a[0] = num/1000; //取千位的数字 
        a[1] = (num/100)%10; //取百位的数字 
        a[2] = (num/10)%10; //取十位的数字 
        a[3] = num%10; //取个位的数字 
        for(int j=0;j<4;j++) { 
            a[j]+=5; a[j]%=10;
        }
        for(int j=0;j<=1;j++) {
            temp = a[j]; a[j] = a[3-j]; a[3-j] =temp;
        } 
        System.out.print("加密后的数字为:");
        for(int j=0;j<4;j++) System.out.print(a[j]); 
    } 
}

【程序49】   题目:计算字符串中子串出现的次数。
import java.util.Scanner;
public class Demo49 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入主串:");
        String str1 = in.nextLine();
        System.out.println("请输入子串:");
        String str2 = in.nextLine();
        // 生成子串长度的N个字符串数组
        String[] sa = new String[str1.length() - str2.length() + 1];
        for (int i = 0; i < sa.length; i++) {
            sa[i] = str1.substring(i, i + str2.length());
        }
        int sum = 0;
        // 子串与N个拆开的子串比对
        for (int i = 0; i < sa.length; i++) {
            if (sa[i].equals(str2)) {
                // 成功配对,计数器+1;
                sum++;
                // 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串
                i = i + str2.length();
            }
        }
        System.out.println("主串中共包含" + sum + "个字串");
    }
}

【程序50】    题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
class Student {
    private int number = 0;
    private String name = "";
    private double[] a = new double[3];
    public double getAve() {
        return (a[0] + a[1] + a[2]) / 3;
    }
    public Student(int number, String name, double[] a) {
        super();
        this.number = number;
        this.name = name;
        this.a = a;
    }
    @Override
    public String toString() {
        return "学号:" + this.number + "\t姓名:" + this.name + "\r\n各科成绩:\r\n" + a[0] + "\t" + a[1] + "\t" + a[2] + "\r\n平均成绩:\r\n"
                + this.getAve();
    }
}
public class Demo50 {
    public static Student input() {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入学号:");
        int num = s.nextInt();
        System.out.println("请输入姓名:");
        String name = s.next();
        System.out.println("请分别输入3门成绩:");
        double[] a = new double[3];
        for (int i = 0; i < 3; i++) {
            a[i] = s.nextDouble();
        }
        return new Student(num, name, a);
    }
    public static void main(String[] args) throws Exception {
        Student[] st = new Student[2];
        for (int i = 0; i < st.length; i++) {
            st[i] = input();
        }
        File f = new File("d:" + File.separator + "123.txt");
        FileWriter output = new FileWriter(f);
        for (int i = 0; i < st.length; i++) {
            output.write(st[i].toString() + "\r\n");
            output.write("\r\n");
        }
        output.close();
    }
}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多