Java经典算法50题(41~50题)

第四十一题:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
分析: 需要用循环,判定的条件为初始在执行运算后的数值可以被四整除,且该操作需连续四次判定为正确
需要注意的点:在得到结果后需跳出while循环

package test2;

public class test41 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int temp = 0, j, i = 4, count;
		while (true) {
			j = i;//先保存i的值
			count = 0;//表示判定成功的次数
			for (int k = 0; k < 5; k++) {
				temp = i / 4 * 5 + 1;
				i = temp;
				if (temp % 4 == 0) {//如果该数是四的倍数
					count++;
				} else {
					break;
				}
			}
			i = j;
			if (count == 4) {
				System.out.println(temp);
				break;
			}
			i += 4;//因为变量i表示最后剩的桃子,而该数一定是四的倍数,所以该变量自加四
		}
	}

}

第四十二题:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
分析:原来题目的这个等式无解,去掉加一就有解了,但是这个等式也没有什么意义了,因为它对于任何数字都成立,所以只用管这题的第二个条件即可
需要注意的点:如果用while循环记得在得到结果后跳出循环,我在写的时候就经常犯这个错误

在这里插入代码片package test2;

public class test42 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 10;
		while (i < 100) {
			if (8 * i > 10 && 8 * i < 100 && 9 * i >= 100 && 9 * i < 1000) {
				System.out.println(i + " " + 809 * i);
				break;
			}
			i++;
		}
	}

}

第四十三题:题目:求0—7所能组成的奇数个数。
//组成1位数是4个。
//组成2位数是74个。
//组成3位数是7
84个。
//组成4位数是7
884个。
//…
分析:虽然题目没有明说数字可不可以重复,但从下面组成数字的个数我们可以看出数字是可以重复的,直接用循环搞定
需要注意的点:注意条件是奇数

package test2;

public class test43 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int m = 0, i = 1;
		while (i < 76543210) {
			if (i % 2 != 0) {
				m++;
			}
			i++;
		}
		System.out.println(m);
	}

}

第四十四题:一个偶数总能表示为两个素数之和。
分析:先写出求素数的方法
需要注意的点:由于用除sqrt(n)的方法求出的素数不包括2和3,因此在判断是否是素数程序中人为添加了一个3。

package test2;

import java.util.Scanner;

public class test44 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int j = 2; j < n; j++) {
			if (mi(j) && mi(n - j)) {
				System.out.println(j + " " + (n - j));
				break;
			}
		}
	}

	static boolean mi(int i) {
		for (int j = 2; j < i / 2; i++) {
			if (i % j == 0) {
				return false;
			}
		}
		return true;
	}
}

第四十五题:判断一个素数能被几个9整除 ,题目错了吧?能被9整除的就不是素数了!所以改成整数了。(这题目是我直接在网上复制的)
分析:直接while,没什么好说的
需要注意的点:无

package test2;

import java.util.Scanner;

public class test45 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int count = 0;
		while (m % 9 == 0) {
			count++;
			m /= 9;
		}
		System.out.println(count++);
	}

}

第四十六题:两个字符串连接程序
分析:直接用加号就行(我直接粘贴网上的代码了)
需要注意的点:无

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

第四十七题:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
分析:无(不想写了,直接粘贴网上的)
需要注意的点:最好加上对读取的数值的判断

import java.util.*;
public class lianxi47 {
    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 ++;
        }
    }
}

第四十八题:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
分析:直接while即可
需要注意的点:需要判断输入的数字

package test2;

import java.util.Scanner;

public class test38 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int[] m = new int[4];
		while (true) {
			int s = sc.nextInt();
			if (s > 999 && s < 9999) {
				m[0] = s / 1000;
				m[1] = s / 100 % 10;
				m[2] = s / 10 % 10;
				m[3] = s % 10;
				break;
			} else {
				System.out.println("请输入四位数");
			}
		}
		for (int i = 0; i < 4; i++) {
			m[i] += 5;
			m[i] %= 10;
		}
		int temp;
		temp = m[0];
		m[0] = m[3];
		m[3] = temp;
		temp = m[1];
		m[1] = m[2];
		m[2] = temp;
		int n = m[0] * 1000 + m[1] * 100 + m[2] * 10 + m[3];
		System.out.println(n);
	}

}
第四十九题:计算字符串中子串出现的次数   
分析:先输入字符串和子串,再将所有字符串中所有符合子串长度的子串放在String数组中,最后进行比较
需要注意的点: 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串,需要再配对成功后改变i的值
```package test2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test49 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		String str1 = r.readLine();
		String str2 = r.readLine();
		String[] m = new String[(str1.length() - str2.length() + 1)];
		int count = 0;
		for (int i = 0; i < m.length; i++) {
			m[i] = str1.substring(i, i + str2.length());
			if (m[i].equals(str2)) {
				count++;
				// 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串
				i = i + str2.length();
			}
		}
		System.out.println(count);
	}

}

第五十题:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
分析:定义一个学生类,包含学生的信息和成绩和一个计算平均成绩的方法
需要注意的点:

package test2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

class Student {
	String num;
	String name;
	double[] score;
	double agv;

	Student() {
		num = null;
		name = null;
		score = new double[3];
		for (int i = 0; i < 3; i++) {
			score[i] = 0;
		}
	}

	Student(String num, String name, double s1, double s2, double s3) {//本题目没有要求
		this.num = num;
		this.name = null;
		score = new double[3];
		score[0] = s1;
		score[1] = s2;
		score[2] = s3;
	}

	double agv(double[] n) {
		double v = 0;
		for (int i = 0; i < n.length; i++) {
			v += n[i];
		}
		return v / n.length;
	}
}

public class test50 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Student[] student = new Student[5];
		;
		for (int i = 0; i < 5; i++) {
			student[i] = new Student();
		}
		Scanner sc = new Scanner(System.in);
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		for (int i = 0; i < 5; i++) {
			System.out.println("请输入第" + (i + 1) + "位同学的成绩");
			student[i].num = sc.next();
			student[i].name = sc.next();
			student[i].score[0] = sc.nextDouble();
			student[i].score[1] = sc.nextDouble();
			student[i].score[2] = sc.nextDouble();
			student[i].agv = (student[i].score[0] + student[i].score[1] + student[i].score[2]) / 3;
		}
		try {
			BufferedWriter w = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream("C:\\360Downloads\\stud")));
			for (int i = 0; i <5; i++) {
				w.write(student[i].num);
				w.write(student[i].name);
				w.write(String.valueOf(student[i].score[0]));//将double类型的分数转换为String类型写入
				w.write(String.valueOf(student[i].score[1]));//因为write方法不能写入double类型的数值
				w.write(String.valueOf(student[i].score[2]));
			}
			w.flush();
			w.close();
			System.out.println("ture");//成功保存输出ture
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值