目录
7.计算1 - 1/2 + 1/3 - 1/4 + 1/5 …… + 1/99 - 1/100
1.判断一个数是否为素数
素数是指质数,一个大于1的自然数,除了1 和它自身外,不能整除其他自然数的数叫做质数;否则称为合数。规定1既不是质数也不是合数。
思路:
首先输入一个大于1的正整数n;
再根据素数的定义 除了自己本身和1之外没有因数 即从2 到n-1;
利用for循环 从2除到n-1 利用if语句判断 若在此过程中除余为零 则此数不是素数,若除到n-1还没有出现除余为零的情况 则此数为素数;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个大于1的整数:");
int num = scanner.nextInt();
int i = 2;
for (;i<=num-1;i++){
if (num%i == 0){
System.out.println("该数字不是素数");
break;
}
}//此时循环结束 i = num
if (num == i){
System.out.println("该数字是素数");//使用if是考虑输入的num=2时
}
}
}
2.输出1-100 之间的所有素数
思路:嵌套for循环
外部for循环 i从2到100 (因为素数是一个大于1的整数,所以从2开始)
内部for循环 j从2 到 i-1 (因为除了1和 i 本身外,不能被其他整数整除,所以获取2到 i -1)。
在内部for循环内使用if选择结构,判断 j 能否被 i整除。
在外部for循环内使用if选择结构,判断 j 与 i 是否相等。
public class Main {
public static void main(String[] args) {
//1不是素数 i从2开始到100循环判断
for (int i = 2;i<=100;i++){
int j = 2;//除了1和i本身,则j从2到i-1
for (;j < i;j++){
if (i%j==0){
break;//如果有整除,则不是素数,应跳出内部循环,进入下一个i的循环
}//if一直不满足整除,则进行j自增 如5%2、5%3、5%4
}//当j自增到等于i时还未有整除,此时循环结束,说明i是素数,此时i=j
if (i==j){//使用if是考虑到i=2时
System.out.print(i+" ");
}
}
}
}
3.输出1000 - 2000 年之间的闰年
闰年:公历年份是4的倍数且不是100的倍数
for (int year = 1000;year <= 2000;year++){
if (year % 4 == 0 && year % 100 != 0){
System.out.println(year);
}
}
4.计算 1到 100 的所有整数中出现多少个数字9
public class Main {
public static void main(String[] args) {
int count = 0;
for (int n = 1;n <= 100;n++){
while (n % 10 == 9){
count++;
break;//不跳出当前循环 则n=1时一直在判断while语句 进入死循环
}
}
System.out.println(count);
}
}
5.输出一个整数在内存当中存储时,二进制1的个数
如:15:0000 1111 四个1
思路:一个数和1相与 如果末位是1,则结果为1 、如果末位是0,则结果为0
0000 1111 & 0000 0001 == 0000 0001
将该数和1进行按位与运算,运算后再进行移位运算,交替计算,直到该数移位到零为止。根据结果可计算出二进制位中1的个数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = scanner.nextInt();
int count = 0;
while (num != 0){
if ((num & 1) == 1){
count++;
}
num = num >> 1;//移位运算要赋值,不能单独写
}
System.out.println(count);
}
}
6.求两个数的最大公约数
如输入:8 16 最大公约数 :8
方法一:欧几里德算法又称辗转相除法,是指用于计算两个非负整数a,b的最大公约数
以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数.
算法流程图:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.println("请分别输入两个数:");
int a = scanner1.nextInt();
int b = scanner2.nextInt();
int r;
while(a%b!=0)
{
r = a%b;
a = b;
b = r;
}
System.out.println("最大公约数为:"+b);
}
}
方法二:更相减损法, 出自于中国古代的《九章算术》
①先判断两个数的大小,如果两数相等,则这个数本身就 是就是它的最大公约数。
②如果不相等,则用大数减去小数,然后用这个较小数与它们相减的结果相比较,如果相等, 这个差就是它们的最大公约数,而如果不相等,则继续执行②操作。
算法流程图:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.println("请分别输入两个数:");
int a = scanner1.nextInt();
int b = scanner2.nextInt();
while(a != b) {
if(a>b) {
a = a - b;
}
else {
b = b - a;
}
}
System.out.println("最大公约数为:"+a);
}
}
7.计算1 - 1/2 + 1/3 - 1/4 + 1/5 …… + 1/99 - 1/100
public class Main {
public static void main(String[] args) {
float result = 0;
// 1 / n 分母n为奇数时符号为正,为偶数时符号为负
for(byte n=1;n<=100;n++) {
if(n%2!=0) {
result += (float) 1 / n;
// 若未进行强转 就要写成1.0 / n
}
else {
result -= (float) 1 / n;
// 若未进行强转 就要写成1.0 / n
}
}
System.out.println("result=" + result);
}
}
8.水仙花数
求出0~999之间的所有“水仙花数”并输出
“水仙花数”是指一个三位数,其各位数字的立方和恰好等于该数本身,如;153=1^3+5^3+3^3,则153是一个“水仙花数“。
思路:因为水仙花数是三位数,所以我们在100-1000之间进行判断即可。
利用for循环,遍历100-1000之间的数。输出水仙花数
public class Main {
public static void main(String[] args) {
int n;
//for循环遍历100-1000
for(n=100; n< 1000; n++){
int n1= n / 100; //百位
int n2= (n % 100) / 10;//十位
int n3= n % 10;//个位
// 判断是否为水仙花数
if(n == (n1*n1*n1 + n2*n2*n2 + n3*n3*n3)){
System.out.println(n);
}
}
}
}
9.实现猜数字游戏
系统自动生成一个随机整数(1- 100),然后由用户输入一个猜测的数字,如果输入数字比生成数大,则提示“猜大了”;如果输入数字比生成数小,则提示“猜小了”;如果输入数字与生成数一样,则提示“猜对了”。
思路:需要使用Random生成随机数,使用while循环来猜数字,每次猜的数字进行对比;使用if语句来判断猜的大还是小或是猜对了,猜对时跳出循环即可。
scanner语句放在循环内外都可以,但是放在里面效率会低一些,因为每次循环都会创建对象;放在外面,在循环中可以通过scanner.nextInt() 的方式读取数字。
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random(); //创建对象
Scanner sc = new Scanner(System.in);
int num = random.nextInt(100)+1;//生成随机数
// 括号内是随机数的范围0~99共100个数字
// 因为题目要求1-100,可以在后面+1从而变成生成1-100 共100个整数。
while (true){
System.out.println("请输入1~100以内的数字:");
int toGuess = sc.nextInt();//系统读取所输入猜测的数字
if (toGuess>num){
System.out.println("猜大了");
} else if (toGuess<num) {
System.out.println("猜小了");
}else{
System.out.println("猜对啦");
break;//猜对时跳出循环
}
}
}
}
10.打印 X 图形
使用若干 * 来组成一个图形
思路:使用5x5的表格,用 * 组成X:
* | * | |||
* | * | |||
* | ||||
* | * | |||
* | * |
定义 行为 i 列为 j 则 i j范围从1-5 使用嵌套for循环
在表格中发现,当 i == j 或者 i + j == 6 时 输出 * 其他时候打印空格 同一行打印不换行
当j == 5 时,需要换行 打印下一行
public class Main {
public static void main(String[] args) {
for (int i =1;i<=5;i++){
for (int j =1;j<=5;j++){
if ( i==j || (i+j==6) ){
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();//j=5时循环结束,到第五列要换行
}
}
}
11.打印乘法口诀表
思路: i * j 输出乘法表
i 为行 i 从9 ~ 1递减
j 为列 第1列 i == j 、第2列 j +1 、 第3列 j + 2 ......所以 j 从 i ~ 9 递增
当 j == 9 时换行
i | i * j | ||||
9 | 9 * 9 | ||||
8 | 8 * 8 | 8 * 9 | |||
7 | 7 * 7 | 7 * 8 | 7 * 9 | ||
..... | |||||
1 | 1 * 1 | 1 * 2 | 1 * 3 | ....... | 1 * 9 |
public class Main {
public static void main(String[] args) {
for (int i =9;i>=1;i--){
for (int j =i;j<=9;j++){
System.out.print(i+"x"+j+"="+i*j+"\t");//使用转义字符\t对齐
}
System.out.println();//j=9时内部循环结束,要换行
}
}
}
结果:
12.输出一个整数的每一位
输出一个整数的每一位,如:123的每一位是3,2,1
思路:如1234 % 10 = 4 得到个位数 再将原数字 (1234 / 10 )%10= 123 %10 = 3 得到原数字的十位数 。循环进行求余、除法运算即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("请输入一个数字:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.print(n+"的每一位为: ");
while(n > 0) {
System.out.print(n % 10 + " ");//求余数,即个位数
n /= 10;//再将该数除以10
}
}
}
13.模拟登录
编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int count=0;//尝试次数
String password="123456";//定义密码
while(count<3) {
System.out.println("请输入密码:");
Scanner p = new Scanner(System.in);
String str = p.next();
if(!str.equals(password)) {
count++;
System.out.println("密码错误!");
}
if(str.equals(password)) {
System.out.println("登录成功!");
break;
}
}
if(count==3)
System.out.println("登录失败三次,将退出程序!");
}
}
14.二进制序列
输入一个数,将其二进制序列中所有的偶数位和奇数位分别输出二进制序列
如 10 :二进制 0000 1010 从左到右依次为最高位到最低位,即第8~1位。
偶数位: 0 0 1 1 奇数位: 0 0 0 0
思路:
按照默认int整型来计算,int占4个字节,二进制一共是4 * 8 = 32位
最高位是第32位,偶数位,将其右移31次后,再和1进行相与运算,得到1则该偶数位为1 反之为零。再右移29次,得到第30位、右移27次,得到28位.......依次循环右移即可。同理可得到奇数位
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = sc.nextInt();//输入整数
System.out.println("偶数位为:");
for (int i = 31; i >= 1; i -= 2){//右移31 29 ... 3 1位 得到偶数位
System.out.print(num>>i &1); //右移,然后与1得到该位的值
}
System.out.println();//换行
System.out.println("奇数位为:");
for (int i = 30; i >= 0; i -= 2){//右移30 28 ... 2 0位 得到奇数位
System.out.print(num>>i &1);
}
}
}
结果:
15.四舍五入
输入一个数字,要求四舍五入后输出
如输入13.99 输出 14
利用强制类型转换与int自动取整的原理,直接在原数字上加0.5即可
同时考虑到输入负数的情况,此时原数字减去0.5即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double d= scanner.nextDouble();
int i = d >0? (int)(d +0.5):(int)(d-0.5);
System.out.println(i);
}
}
16.交换变量值
输入两个数,在不使用第三个变量的情况下,交换二者的变量值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
a = a+b;//a为二者的和 此时a代表和
b = a-b;//和减去b结果为a 此时 原a被赋值给了b
a = a-b;//和减去b(a)结果为b 此时 原b被赋值给了a
System.out.println(a+" "+b);
}
}
17.三目运算符新用法
某商场推出满100全额打9折;满500全额打8折;满2000全额打7折;满5000全额打6折;
且商场有抹零活动,不足一元的部分不需要付款;输入一个帐单花销,输出对应打折后的花费。
思路:if - else语句即可 ,但也可以使用三目运算符的多重选择
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int price = console.nextInt();
int cost;
//考虑到购买5000元东西的人很少,应从小到大判断,以减少判断次数
cost=(int)(price<100 ? price : price<500
? price*0.9 : price<2000
? price*0.8 : price<5000
? price*0.7 : price*0.6);
System.out.println(cost);
}
}
体重指数 = 体重 (kg) / ( 身高 (m) × 身高 (m) )
小于18.5属于偏瘦,介于18.5和20.9之间(左闭右开)属于苗条,
介于20.9和24.9之间(左闭右闭)属于适中,超过24.9属于偏胖。
输入:用户的身高(m)和用户的体重(kg)
输出:体重指数代表的用户身材状态
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double height = scanner.nextDouble();
double weight = scanner.nextDouble();
double bmi = weight / (height*height);//乘除法的优先级相同,括号不能忘记
String body = bmi<18.5 ? "偏瘦":bmi<20.9? "苗条"
:bmi<=24.9? "适中":"偏胖";
System.out.print(body);
}
}
18.验证邮箱格式
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
//利用matches()方法进行正则匹配
// 如果合法,则输出"邮箱格式合法",否则输出"邮箱格式不合法"
System.out.println(str.matches(emailMatcher)?"邮箱格式合法":"邮箱格式不合法");
}
}
19.数列求和
计算9+99+999+9999+ ...... +9999999999
注意此题不能使用int类型, n 与 add 都要是 long类型
public class Main {
public static void main(String[] args) {
long n = 9;
long add = 0;
for(int i = 1;i<=10;i++){
//9+99+999+...+9999999999
//System.out.print(n+" ");
add = n+add;
n = (n*10)+9;
}
System.out.println(add);
}
}
20.求两个数的最小公倍数
两个数:x y
公式: x * y == 最小公倍数 * 最大公约数
则先根据辗转相除法计算最大公约数 再用两数乘积除以最大公约数 即可
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int m = console.nextInt();
int n = console.nextInt();
int result = getCM(m, n);
System.out.println(result);
}
public static int getCM(int m, int n){
int r;
int num = m*n;//先计算两数乘积
while(m%n != 0){
r = m%n;
m = n;
n = r;
}
return(num/n);//函数返回值
}
}
//利用函数的自我调用与传参来实现:
public static int getCM(int m, int n) {
return m * n / gcd(m, n);
}
public static int gcd(int m, int n) {
if (n == 0) {
return m;
}else{
return gcd(n, m % n);
}
}
21.输入任意多个数字,当输入负数时结束,求出他们的平均数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0;
int add = 0;
while(true){
int num = scan.nextInt();
if(num<0){
break;
}
add += num;
count++;
}
double avg =(double) add / count;//右侧两个int计算后结果仍是int 所以要使用强制转换
//double avg = add / count;
//虽然左侧使用double接收 但两个int计算后为整型,接收后只是加了小数点 如:14 -> 14.00
System.out.println(String.format("%.2f",avg));
}
}
22.自由落体小球弹起n次所经过的路程与高度
一球从h米高度自由落下,每次落地后反弹回原高度的一半再落下,求它在第n次落地时共经过了多少米?第n次反弹多高?
输入:小球的初始高度和落地的次数(先输入小球初始高度再输入反弹次数)
输出:小球反弹的高度和经过的距离(先输出反弹的高度再输出经过的距离,中间用空格隔开)