《Java程序设计》课程代码题(三)

1.《整除数》

接收a,b两个int整数(a<b),要求在[a,b]中能找到被3或4整除,但不能同时被3和4整除的数。
输入要求
多组数据,每一组数据两个整数a,b,整数之间用空格隔开
输出要求
把符合条件的数从大到小,每行10个打印输出(每一行最后无空格)
输入

1 100
100 200

输出

100 99 93 92 90 88 87 81 80 78 76 75 69 68 66 64 63 57 56 54 52 51 45 44 42 40 39 33 32 30 28 27 21 20 18 16 15 9 8 6 4 3
200 198 196 195 189 188 186 184 183 177 176 174 172 171 165 164 162 160 159 153 152 150 148 147 141 140 138 136 135 129 128 126 124 123 117 116 114 112 111 105 104 102 100

代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        while(sc.hasNextInt()){
            int  a=sc.nextInt();
            int b=sc.nextInt();

            List<Integer>   list=new ArrayList<>();
            while(a<=b){
                if((b%3==0||b%4==0)&&(b%3!=0||b%4!=0)){
                    list.add(b);
                }
                b--;

            }

            for(int i=1;i<=list.size();i++){
                if(i%10!=0)
                    System.out.print(list.get(i-1)+""+(char)32);
                else {
                    System.out.print(list.get(i-1));
                    System.out.println();
                }
            }
        }
        sc.close();
    }
}

2.《开心消消乐》

给定一个数字,如果它以及它数字的倒置都是素数,则消除成功,否则失败。
比如:13 和31都是素数,则能被消除
271是素数,但是它的倒置172不是素数,则不能被消除
输入要求
一个整型n
输出要求
如果能被消除,则输出"Remove Successfully"
否则输出"Ops"
输入

13
271

输出

Remove Successfully
Ops

代码


import java.util.Scanner;

public class main {
    public static void main(String[] args) {
    Scanner   sc=new Scanner(System.in);
    while(sc.hasNextInt()){
    int n=sc.nextInt();
    int temp=0;
    Boolean  b=true;
    for(int i=2;i<Math.sqrt(n);i++){
        if(n%i==0)
            b=false;
    }

    while(n>0){
        temp=temp*10+n%10;
        n/=10;
    }


        for(int i=2;i<Math.sqrt(temp);i++){
            if(temp%i==0)
                b=false;
        }


        if(b)
            System.out.println("Remove Successfully");
        else
            System.out.println("Ops");

    }
  sc.close();
    }
}

3.《开心消消乐2.0》

给定一个数字p,如果它是素数2^p-1也是素数,则消除成功,否则失败。
比如:3是素数,它可以写成2^3-1 = 7也为素数,则能被消除
输入要求
一个整型n
输出要求
如果能被消除,则输出"Remove Successfully"
否则输出"Ops"
输入

3
8

输出

Remove Successfully
Ops

代码

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        while(sc.hasNextInt()){

            int p=sc.nextInt();
            boolean [] booleans=new boolean[2];
            booleans[0]=true;
            booleans[1]=true;
            for(int i=2;i<Math.sqrt(p);i++){
                if(p%i==0){
                    booleans[0]=false;
                    break;
                }
            }

            long  p2=(long)Math.pow(2,p)-1;
            for(int i=2;i<Math.sqrt(2);i++){
                if(p2%i==0){
                    booleans[1]=false;
                    break;
                }
            }
            if(booleans[1]&&booleans[0])
                System.out.println("Remove Successfully");
            else
                System.out.println("Ops");


        }
        sc.close();
    }
}

4.《双素数》

输入给定整型n以内的所有双素数
什么是双素数呢?
两个数差值为2且两个数都为素数则是双素数
输入要求
一个整型n
输出要求
输出n以内的双素数,两个数用括号括起来,两个数中间用逗号隔开
输入

100

输出

( 3,5 )
( 5,7 )
( 11,13 )
( 17,19 )
( 29,31 )
( 41,43 )
( 59,61 )
( 71,73 )

代码

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            boolean[] nums = new boolean[n + 1];
            for (int i = 2; i <= (n + 1) / 2; i++) {
                if (!nums[i]) {
                    for(int k=i+i;k<n+1;k+=i){
                        nums[k]=true;
                    }
                }

            }
           
            for(int i=3;i<n-1;i++){
                if(!nums[i]&&!nums[i+2]){
                    int num=i+2;
                    System.out.println("( "+i+","+num+")");
                    }
            }
        } sc.close();


      }
    }

5.《密码任务》

任务要求:
要求密码内容 1)至少8位字符; 2)密码仅能有字母和数字,不能有其他字符; 3)密码至少包括2个数字;
要求程序检查顺序 是否满足8位字符—>是否仅仅包含字符和数字---->是否包含2个数字
当字符串顺序检查查出不符合条件时输出“NO”,后跟随原因(请看输出要求),如果满足所有条件,输出“YES”
输入要求
多组数据,每一组数据是一串长度不为0的字符串
输出要求
当不满足8位字符串条件输出“NO Password at least 8 digits”
当不满足仅仅字符和数字条件输出 “NO Passwords can only consist of characters and numbers”
当不满有两位数字条件输出 “NO Password requires two digits”
满足所有条件 “YES”
输入

qwertyuiopr
qaz1234
qaz12345678
qsxcvbnm2123$#qwe

输出

NO Password requires two digits
NO Password at least 8 digits
YES
NO Passwords can only consist of characters and numbers

代码

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
       String   str=sc.nextLine();
       Boolean [] booleans=new Boolean[3];
       for(int  i=00;i<3;i++){
           booleans[i]=true;
       }
       if(str.length()<8){
           booleans[0]=false;
       }
       int  num=0;
       char [] ch=str.toCharArray();
       for(int i=0;i<ch.length;i++){
           if((ch[i]<'A'||ch[i]>'Z')&&(ch[i]<'a'||ch[i]>'z')&&!Character.isDigit(ch[i]))
               booleans[1]=false;

       }
            for(int i=0;i<ch.length;i++){
                if(Character.isDigit(ch[i])) {
                    num++;
                }

            }
            if(num<2)
                booleans[2]=false;


       if(booleans[0]==false){
           System.out.println("NO Password at least 8 digits");
       }
       else  if(booleans[1]==false)
           System.out.println("NO Passwords can only consist of characters and numbers");
       else  if(booleans[2]==false)
           System.out.println("NO Password requires two digits");
       else
           System.out.println("YES");
        } sc.close();
      }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一角灯辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值