JavaOJ 作业5

版权声明:本文为博主原创文章,转载请注明出处,谢谢。 https://blog.csdn.net/qingchenldl/article/details/84576371

153 - 判断回文

Description

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

Input

整数

Output

几位数
是否是回文数

Sample Input

12121

Sample Output

5
Y

MyAnswer

/*
主要利用String的方法,把数字转换成字符串,之后判断
*/
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        String s = String.valueOf(num);
        System.out.println(s.length());
        if(judgeNum(num)){
            System.out.println("Y");
        }else{
            System.out.println("N");
        }
    }

    public static boolean judgeNum(int num){
        String s = String.valueOf(num);
        int i,j;
        for (i=0, j=s.length()-1; i<j; i++,j--){
            if(s.charAt(i)!=s.charAt(j)){
//                System.out.println("N");
                return false;
            }
        }
        return true;
    }
}

162 - 字符串

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

MyAnswer

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 对于有空格的长串字符串
        String s = scan.nextLine();
        char c = scan.next().charAt(0);
        String str = scan.next();

        // 统计次数
        System.out.println(Count(s,c));
        // 逆序输出
        System.out.println(new StringBuffer(s).reverse());
        // 统计出现位置,利用IndexOf方法
        int flag=0;
        int tmp=0;
        int pos=s.indexOf(str);
        while(pos != -1){
            if(flag==0){
                System.out.print(pos);
                flag += 1;
            }else{
                System.out.print(" "+pos);
            }
            tmp = pos+str.length();
            pos = s.indexOf(str,tmp);
        }
        System.out.print("\n");
        // 转成首字母大写
        System.out.println(Upper(s));


    }
    // 计数
    public static int Count(String s,char c){
        int i,j;
        int num=0;
        for(i=0; i<s.length(); i++){
            if(s.charAt(i) == c){
                num++;
            }
        }
        return num;
    }

    // 转成大写
    public static String Upper(String s){
        int i;
        char[] ch = s.toCharArray();
//        System.out.println(ch);
        if(ch[0]>='a'&&ch[0]<='z')
            ch[0] = (char)(ch[0]-32);
        for(i=1;i<s.length();i++){
            if(ch[i-1]==' '&&ch[i]>='a'&&ch[i]<='z')
                ch[i] = (char)(ch[i]-32);
        }
        s = new String(ch);
        return s;
    }
}

163 - 各类字符数

Description

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

Input

字符串

Output

大写英文字母数
小写英文字母数
非英文字母数

Sample Input

Hello My Dear Friend, I Miss You Very Much!

Sample Output

9
24
10

MyAnswer

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        int up=0,low=0,other=0;
        int i,j;
        for(i=0; i<s.length(); i++){
            char c = s.charAt(i);
            if(c<='z'&&c>='a')
                low++;
            else if(c>='A'&&c<='Z')
                up++;
            else
                other++;
        }
        System.out.println(up);
        System.out.println(low);
        System.out.println(other);

    }
}

165 - 数据类型判断

Description

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

Input

字符串

Output

数据类型

Sample Input

2.1 true 123 abcde

Sample Output

double boolean int String

MyAnswer

import java.util.*;
/*
* reference: http://blog.163.com/quanquan127@126/blog/static/68847725201252995854785/
*            https://ask.csdn.net/questions/236125
* title: 判断字符串属于那种数据类型
* author: V0W
* */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
       //hasNext判断键盘输入
        int flag=0;
        while(scan.hasNext()){
            if(flag!=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();
            flag++;
        }
    }
}

164 - 解析二维数组

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

MyAnswer

import java.util.*;
/*
* title: 通过split将字符串分割为二维数组
* author: V0W
* */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String[] row = s.split(";");
        int i,j;
        for(i=0; i<row.length; i++){
            String[] col = row[i].split(",");
            for(j=0; j<col.length; j++){
                if(j!=0)
                    System.out.print(" ");
                System.out.print("d["+i+","+j+"] = "+col[j]);
            }
            System.out.print("\n");
        }
    }
}

158 - 打印双休日

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

MyAnswer

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
* title: 打印双休日
* reference: https://blog.csdn.net/qy20115549/article/details/52756652
*           https://blog.csdn.net/tingwufeixiang/article/details/70755713
* knowledge: Date,Format
* author: V0W
* */
public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);
        int year = scan.nextInt();
        int month = scan.nextInt();
        int d;
        int daynum = getDaysByYearMonth(year,month);
        for(d=1; d<=daynum; d++){
            // 注意格式01,02
            String dd = year+"-"+String.format("%02d",month)+"-"+String.format("%02d",d);
            if(isWeekend(dd))
                System.out.println(dd);
        }

    }

    //获取指定月份的天数
    //每个月的天数是不一定的,也可以根据大小月,但是要考虑平闰年,这里Oj判断的不严谨,或者样例太少,直接<31也能过,但是不合逻辑
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }


    public static boolean isWeekend(String bDate) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(bDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;

    }

}

166 - 比较日期

Description

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

Input

两个日期

Output

日期大小关系
间隔天数(正数)

Sample Input

04,12,2012 04,21,2012

Sample Output

<
9

HINT

月份是从0开始

MyAnswer

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
* title: 比较日期
* reference: https://blog.csdn.net/u011479990/article/details/52954200
* knowledge: Date,Format
* author: V0W
* */
public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);
        String d1 = scan.next();
        String d2 = scan.next();
        DateFormat format1 = new SimpleDateFormat("MM,dd,yyyy");
        Date date1 = format1.parse(d1);
        Date date2 = format1.parse(d2);
        if (date1.equals(date2)) {
            System.out.println("=");
        } else if (date1.before(date2)) {
            System.out.println("<");
        } else System.out.println(">");
        long days = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);
        System.out.println(Math.abs(days));
    }
}

猜你喜欢

转载自blog.csdn.net/qingchenldl/article/details/84576371
今日推荐