美文网首页
Java第五次作业

Java第五次作业

作者: z坎坷 | 来源:发表于2018-11-08 15:48 被阅读0次

    153 - 判断回文

    Time Limit: 1000  Memory Limit: 65535

    Submit: 187  Solved: 124

    Description

    用户从键盘输入一个整数,程序将判断这个数是几位数并输出其位数,并判断这个数是否是回文数,是则输出Y,否则输出N。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、3223都是回文数。

    Input

    整数

    Output

    几位数

    是否是回文数

    Sample Input

    12121

    Sample Output

    5

    Y

    162 - 字符串

    Time Limit: 1000  Memory Limit: 65535

    Submit: 128  Solved: 74

    Description

    对于输入字符串s(假设字符串只包含字母构成的单词和空格),完成如下功能:

    1. 统计该字符串中字母c出现的次数

    2. 求该字符串的逆

    3. 输出该字符串中子串str的所有位置(无需考虑子串叠加现象)

    4. 将字符串中每个单词的第一个字母变成大写并输出

    Input

    字符串s

    字母c

    子串str

    Output

    c在s中出现的次数

    s的逆

    str在s中的所有位置

    所有单词首字母大写后的字符串

    Sample Input

    I scream you scream we all scream for icecream

    m

    eam

    Sample Output

    4

    maerceci rof maercs lla ew maercs uoy maercs I

    5 16 30 43

    I Scream You Scream We All Scream For Icecream

    __________________________________________________

    import java.util.*;

    public class Main {

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            Scanner scan=new Scanner(System.in);

            String s=scan.nextLine();

            //1. 统计该字符串中字母c出现的次数

            String c=scan.nextLine();

            int count=0;

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

                char tmp=s.charAt(i);

                if(tmp==c.charAt(0)){

                    count++;

                }

            }

            System.out.println(count);

            //2. 求该字符串的逆

            StringBuffer sb=new StringBuffer(s);

            System.out.println(sb.reverse());

            //3. 输出该字符串中子串str的所有位置(无需考虑子串叠加现象)

            String str=scan.nextLine();

            int i=0;

            while(s.indexOf(str,i)!=-1){

                if(i!=0){

                    System.out.print(" ");

                }

                System.out.print(s.indexOf(str,i));

                i=str.length()+s.indexOf(str,i);

            }

            System.out.println();

            //4. 将字符串中每个单词的第一个字母变成大写并输出

            String[] split=s.split(" ");

            for(int ii=0;ii<split.length;ii++){

                String ss=split[ii].substring(0,1).toUpperCase()+split[ii].substring(1);

                System.out.print(ss+" ");

            }

        }

    }

    ###############################################

    163 - 各类字符数

    Time Limit: 1000  Memory Limit: 65535

    Submit: 97  Solved: 76

    Description

    从键盘输入一个字符串,程序输出该字符串中的大写英文字母数,小写英文字母数以及非英文字母数

    Input

    字符串

    Output

    大写英文字母数

    小写英文字母数

    非英文字母数

    Sample Input

    Hello My Dear Friend, I Miss You Very Much!

    Sample Output

    9

    24

    10

    ____________________________________________

    import java.util.*;

    public class Main {

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            Scanner scan=new Scanner(System.in);

            String s=scan.nextLine();

            int counta=0;

            int countA=0;

            int count_=0;

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

                if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){

                    countA++;

                }

                else if(s.charAt(i)>='a'&&s.charAt(i)<='z'){

                    counta++;

                }

                else count_++;

            }

            System.out.println(countA);

            System.out.println(counta);

            System.out.println(count_);

        }

    }

    #########################################################

    164 - 解析二维数组

    Time Limit: 1000  Memory Limit: 65535

    Submit: 117  Solved: 61

    Description

    读入一个字符串,该字符串表示一个整型二维数组d,数组中的元素通过解析字符串参数获得。例如,字符串参数:“1,2;3,4,5;6,7,8”,对应的数组为:

    d[0,0] = 1 d[0,1] = 2 

    d[1,0] = 3 d[1,1] = 4 d[1,2] = 5

    d[2,0] = 6 d[2,1] = 7 d[2,2] = 8

    打印这个数组各元素的内容

    Input

    字符串

    Output

    二维数组各元素

    Sample Input

    1,2;3,4,5;6,7,8

    Sample Output

    d[0,0] = 1 d[0,1] = 2

    d[1,0] = 3 d[1,1] = 4 d[1,2] = 5

    d[2,0] = 6 d[2,1] = 7 d[2,2] = 8

    ____________________________________

    import java.util.*;

    public class Main{

    public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);

            String s=scan.next();

            String[] st=s.split(";");

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

            String[] st2=st[i].split(",");

            for(int j=0;j<st2.length;j++) {

            if(j!=0) System.out.print(" ");

            System.out.print("d["+i+","+j+"] = "+st2[j]);

            }

            System.out.println();

            }

        }

    }

    ###############################################

    165 - 数据类型判断

    Time Limit: 1000  Memory Limit: 65535

    Submit: 102  Solved: 67

    Description

    从键盘分别输入通过空格分割的整型(int)、浮点型(double)、字符型(String)、布尔型(boolean),根据读取的内容判断他们的类型并将他们解析为正确的对象,并都放到一个数组中。输出各个对象的类型

    Input

    字符串

    Output

    数据类型

    Sample Input

    2.1 true 123 abcde

    Sample Output

    double boolean int String

    ____________________________________________

    import java.util.*;

    public class Main {

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            Scanner scan=new Scanner(System.in);

            int f=0;

            while(scan.hasNext()){

                if(f!=0)

                    System.out.print(' ');

                if(scan.hasNextInt())

                    System.out.print("int");

                else if(scan.hasNextDouble())

                    System.out.print("double");

                else if(scan.hasNextBoolean())

                    System.out.print("boolean");

                else

                    System.out.print("String");

                scan.next();

                f++;

            }

        }

    }

    ##################################################

    158 - 打印双休日

    Time Limit: 1000  Memory Limit: 65535

    Submit: 100  Solved: 57

    Description

    输入年份和月份,打印当月所有双休日日期,打印格式为:“2018-06-16”

    Input

    年份和月份

    Output

    双休日日期

    Sample Input

    2018 6

    Sample Output

    2018-06-02

    2018-06-03

    2018-06-09

    2018-06-10

    2018-06-16

    2018-06-17

    2018-06-23

    2018-06-24

    2018-06-30

    ___________________________

    import java.util.*;

    public class Main{

        public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);

            int year=scan.nextInt();

            int month=scan.nextInt();

            Calendar c = Calendar.getInstance();

            c.set(Calendar.YEAR, year);

            c.set(Calendar.MONTH, month - 1);

            int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);

            int start = 1;

            while (start <= max) {

                c.set(Calendar.DAY_OF_MONTH, start);

                if (isWeekenday(c)) {

                    System.out.println(year+"-"+String.format("%02d",month)+"-"+String.format("%02d",start));

                }

                start++;

            }

        }

        public static boolean isWeekenday(Calendar c) {

            return c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || c.get(Calendar.DAY_OF_WEEK)  == Calendar.SATURDAY;

        }

    }

    ##################################################

    166 - 比较日期

    Time Limit: 1000  Memory Limit: 65535

    Submit: 99  Solved: 54

    Description

    从命令行输入两个日期(格式为MM,dd,yyyy),程序解析日期,判断两个日期的大小,以及两个日期的间隔天数。

    Input

    两个日期

    Output

    日期大小关系

    间隔天数(正数)

    Sample Input

    04,12,2012 04,21,2012

    Sample Output

    <

    9

    HINT

    月份是从0开始

    _________________________________________________

    import java.text.ParseException;

    import java.text.SimpleDateFormat;

    import java.util.*;

    public class Main{

        public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);

            String d1=scan.next();

            String d2=scan.next();

            String[] st1=d1.split(",");

            String[] st2=d2.split(",");

            Date date1 = new Date(Integer.parseInt(st1[2]),Integer.parseInt(st1[0]),Integer.parseInt(st1[1]));

            Date date2 = new Date(Integer.parseInt(st2[2]),Integer.parseInt(st2[0]),Integer.parseInt(st2[1]));

            /* Calendar c1 = Calendar.getInstance();

            c1.set(Calendar.YEAR, Integer.parseInt(st1[2]));

            c1.set(Calendar.MONTH, Integer.parseInt(st1[0]));

            c1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st1[1]));

            Calendar c2 = Calendar.getInstance();

            c2.set(Calendar.YEAR, Integer.parseInt(st2[2]));

            c2.set(Calendar.MONTH, Integer.parseInt(st2[0]));

            c2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st2[1]));*/

            if (date1.equals(date2)) {

                System.out.println("=");

            } else if (date1.before(date2)) {

                System.out.println("<");

            } else System.out.println(">");

            long d = (date2.getTime()-date1.getTime())/86400000;

            System.out.println(Math.abs(d));

            scan.close();

        }

    }

    #####################################################

    相关文章

      网友评论

          本文标题:Java第五次作业

          本文链接:https://www.haomeiwen.com/subject/uxpwxqtx.html