pta-2024年秋面向对象程序设计实验一-java

2024-09-07 08:44

本文主要是介绍pta-2024年秋面向对象程序设计实验一-java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章申明:作者也为初学者,解答仅供参考,不一定是最优解;

一:7-1 sdut-sel-2 汽车超速罚款(选择结构)

答案:

import java.util.Scanner;

        public class Main {

public static void main(String[] arg){

        Scanner sc=new Scanner(System.in);

        int a=sc.nextInt();

        int b=sc.nextInt();

        int c=b-a;

        if(c>=1&&c<=20) {

        System.out.println("You are speeding and your fine is 100.");

                }         

        else if(c>=21&&c<=30){

        System.out.println("You are speeding and your fine is 270.");

                }

        else if(c>=31)System.out.println("You are speeding and your fine is 500.");

        else System.out.println("Congratulations, you are within the speed limit!");

}

}

 

二: 7-2 Java中二进制位运算

答案:

import java.util.Scanner;

public class Main{

        public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int a=sc.nextInt();

        String c=sc.next();

        int b=sc.nextInt();

        switch(c){

        case "&":

        System.out.println(a+" & "+b+" = "+(a&b));

        break;

        case "|":

        System.out.println(a+" | "+b+" = "+(a|b));

        break;

        case "^":

        System.out.println(a+" ^ "+b+" = "+(a^b));

        break;

        default:

        System.out.println("ERROR");

        break;

                }

        }

}

 

三:7-3 sdut-最大公约数和最小公倍数 

 答案:

import java.util.Scanner;

public class Main{

        public static void main(String[] args){

                Scanner sc=new Scanner(System.in);

                  while(sc.hasNext()){

                        int a=sc.nextInt();

                        int b=sc.nextInt();

                        System.out.print(ans(a,b));

                        System.out.print(" "+a*b/ans(a,b));

                        System.out.println();

                                                        }

                                        }

public static int ans( int a, int b){

                if(b>a){

                        int tem=a;

                        a=b;

                        b=tem;

                        }

                        int c=a%b;

                        while(c!=0){

                        a=b;

                        b=c;

                        c=a%b;

                                        }

                        return b;

                                                }

}

 

四:7-4 判断回文 

答案: 

import java.util.Scanner;

public class Main {

        static int ans(int a) {//判断位数

        int count = 0;

        while (a != 0) {

        count++;

        a = a / 10;

        }

        return count;

        }

                        public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in);

                                int a = sc.nextInt();

                                int ret = ans(a);

                                 System.out.println(ret);

                                int sum=0;

                                int tem=a;

                        while (a != 0) {

                                int c = a % 10;

                                int f=ret;

                                        while(f--!=1){

                                                c*=10;

                                                                }

                                        sum+=c;

                                        ret--;

                                        a=a/10;

                                                                        }

                if (tem==sum) System.out.println("Y");

                else System.out.println("N");

        }

}

7-5 字符串操作

 

答案:

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                String input=sc.nextLine();

                StringBuilder nodigit=new StringBuilder();//创建可变字符串类

                for(char a:input.toCharArray()){//调用string中的方法,将input字符串转变为字符数组

                        if(!Character.isDigit(a)){//调用isdigit方法判断是否为数字

                                nodigit.append(a);//可变字符串末尾插入该字符

                        }

                }

                String ans=nodigit.reverse().toString();//对可变字符串翻转并转换为string类型

                System.out.println(ans);

                }

}

 

六:7-6 N个数的排序与查 

 

答案:

import java.util.Scanner;

public class Main {

        static void maopao(int a[]){//写一个冒泡排序吧

                for(int i=0;i<a.length-1;i++){

                        for(int j=0;j<a.length-i-1;j++){

                                if(a[j]>a[j+1]){

                                        int temp=a[j];

                                        a[j]=a[j+1];

                                        a[j+1]=temp;

                                        }

                                }

                }

}

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                int c= sc.nextInt();

                int a[]= new int[c];

                        for(int i=0;i<c;i++){

                        a[i]=sc.nextInt();

                        }

                maopao(a);

                int x=sc.nextInt();

                        for (int i = 0; i < c; i++) {

                                if (a[i]==x) {System.out.println(i+1);

                                return;}

                                }

                System.out.println(-1);

        }

}

 

7-7 二进制的前导的零 

 

答案:

 

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                int a = sc.nextInt();

                int count=0;

                        while(a!=0){

                                a>>>=1;//无符号右移直至全部32位均为0

                                count++;

                        }

                System.out.println(32-count);//

        }

}

解法二:这个好理解一些

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                int a = sc.nextInt();

                int count=0;

                        for (int i = 31; i >= 0; i--) {

                                if ((a & (1 << i)) == 0) {//直到第一位为1停止

                                count++;

                                } else {

                                        break; // 找到第一个1,停止循环

                        }

                }

        System.out.println(count);

        }

}

 

解法三:直接调用 Integer.numberOfLeadingZeros(a)方法,可以直接计算a前导的0的个数

7-8 古时年龄称谓知多少? 

 

答案:

import java.util.Scanner;

public class Main{

public static void main(String[] arg){

Scanner sc=new Scanner(System.in);

int age=sc.nextInt();

if(age>=0&&age<=9)

System.out.println("垂髫之年");

else if(age<=19)System.out.println("志学之年");

else if(age<=29)System.out.println("弱冠之年");

else if(age<=39)System.out.println("而立之年");

else if(age<=49)System.out.println("不惑之年");

else if(age<=59)System.out.println("知命之年");

else if(age<=69)System.out.println("花甲之年");

else if(age<=79)System.out.println("古稀之年");

else if(age<=89)System.out.println("杖朝之年");

else if(age<=99)System.out.println("耄耋之年");

}

}

 

7-9 期末编程作业计分规则 

  

答案:

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                int num=sc.nextInt();//学生总数

                int max=sc.nextInt();//最高分

                int arr[]=new int[num+1];

                for(int i=1;i<=num;i++){

                arr[i]=sc.nextInt();

                }

                int a=sc.nextInt();//第一档百分比

                int b=sc.nextInt();//第二档百分比

                int c=sc.nextInt();//第三档百分比

                int sb1=sc.nextInt();//减分序列1

                int sb2=sc.nextInt()+sb1;//减分序列2

                int sb3=sc.nextInt()+sb2;//减分序列3

                int sb4=sc.nextInt()+sb3;//减分序列4

                double k1=Math.ceil(num*a/100.0);//第一档人数//Math.ceil可以向上取整

                double k2=Math.ceil(num*b/100.0)+k1;//第二档人数

                double k3 =Math.ceil(num*c/100.0)+k2;//第三档人数

                for(int i=1;i<=num;i++){

                if(i<= k1)arr[i]=(int)(arr[i]/(max/100.0))-sb1;

                else if (i<=k2)arr[i]=(int)(arr[i]/(max/100.0))-sb2;

                else if (i<=k3)arr[i]=(int)(arr[i]/(max/100.0))-sb3;

                else arr[i]=(int)(arr[i]/(max/100.0))-sb4;

        }

                for (int i =1; i <=num; i++) {

                System.out.print(arr[i]+" ");

                }

        }

}

 

7-10 sdut-array2-3 二维方阵变变变 

 

答案:写一个翻转函数,不同角度对应不同翻转次数

 

import java.util.Scanner;

public class Main {

        static int n;//行列数

        static int[][] reverse(int arr[][]){                //翻转函数

        int f[][]=new int[n][n];//创建新数组用于反转操作,不用自身进行反转的原因是会产生覆盖问题

        for(int i=0;i<f.length;i++){

                for(int j=0;j<f[i].length;j++){

                f[i][j]=arr[n-1-j][i];

                }

        }

        return f;

    }

                public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in);

                        n = sc.nextInt();

                        int k=sc.nextInt();        //翻转次数

                        if(k==90)k=1;

                        else if(k==180)k=2;

                        else if(k==-90)k=3;

                        int arr[][]=new int[n][n];

                        for (int i = 0; i < n; i++) {

                                for (int j = 0; j < n; j++) {

                                arr[i][j]=sc.nextInt();

                                }

                        }

                        for(int i=0;i<k;i++){        //翻转k次,每次翻转90°,-90°就是顺时针翻转3次

                        arr= reverse(arr);

                        }

                        for(int i=0;i<n;i++){

                                for(int j=0;j<n;j++){

                                        if(j!=n-1){        //本题严格控制行末空格所以不要多打印空格了

                                        System.out.print(arr[i][j]+" ");

                                        }

                                        else System.out.print(arr[i][j]);

                                }

                                if(i!=n-1){        //避免多打印一个换行

                                System.out.println();

                         }

                    }

            }

}

7-11 直角三角形 

 

答案: 

import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                double a = sc.nextDouble();

                double b = sc.nextDouble();

                double c = sc.nextDouble();

                double max=a>b?(a>c?a:c):(b>c?b:c);//求三边最大的值

                if (a*a+b*b==c*c||c*c+b*b==a*a||a*a+c*c==b*b){        //如果能构成直角三角形

                        if(c==max) System.out.println(a*b/2);

                                else if(b==max) System.out.println(a*c/2);

                                        else System.out.println(b*c/2);

                    }

                else System.out.println(0.0);

        }

}

这篇关于pta-2024年秋面向对象程序设计实验一-java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144607

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题

题库来源:安全生产模拟考试一点通公众号小程序 2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题是由安全生产模拟考试一点通提供,流动式起重机司机证模拟考试题库是根据流动式起重机司机最新版教材,流动式起重机司机大纲整理而成(含2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题参考答案和部分工种参考解析),掌握本资料和学校方法,考试容易。流动式起重机司机考试技

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听