洛谷------ 基础 1427-小鱼的数字游戏 and 5015 标题统计

1427题目描述

小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字ai​(长度不一定,以 0 结束),记住了然后反着念出来(表示结束的数字 0 就不要念出来了)。这对小鱼的那点记忆力来说实在是太难了,你也不想想小鱼的整个脑袋才多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。

输入格式

一行内输入一串整数,以 0 结束,以空格间隔。

输出格式

一行内倒着输出这一串整数,以空格间隔。

输入输出样例

输入 

3 65 23 5 34 1 30 0

输出 

30 1 34 5 23 65 3

说明/提示

数据规模与约定

对于100% 的数据,保证  0 ≤ a i​≤ (2^31)−1,数字个数不超过 100。

分析:时间限制 1s
输入 
    一串数字 以 0 结束
输出
    倒着输出 这串数字

采用数组存储这串数字,然后反向输出


import java.util.Scanner;

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

        int a[] = new int[10000001];
        int flag = 1;
        int i=0; //while
        int i; //for   

//        for (i = 0; i < a.length; i++) {
//            a[i] = sc.nextInt();
//            if (a[i] == 0) {
//                break;
//            }
//        }

        while(flag!=0){
            a[i] = sc.nextInt();
            if(a[i]==0){
                flag=0;
            }
            ++i;
            System.out.println(i);
        }

        for (int j= i-2; j >=0 ; j--) { //需要跳过 0
            System.out.print(a[j]+" ");
        }
    }
}

题目描述

凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大、小写英文字母、数字字符、空格和换行符。统计标题字 符数时,空格和换行符不计算在内。

输入格式

输入文件只有一行,一个字符串 s。

输出格式

输出文件只有一行,包含一个整数,即作文标题的字符数(不含空格和换行符)。

输入输出样例

输入

234 

输出

3

输入 

Ca 45 

输出 

4

说明/提示

【输入输出样例 1 说明】
标题中共有 3 个字符,这 3 个字符都是数字字符。

【输入输出样例 2 说明】 标题中共有5 个字符,包括 1 个大写英文字母, 1 个小写英文字母和 2 个数字字符, 还有 1 个空格。由于空格不计入结果中,故标题的有效字符数为 4 个。

【数据规模与约定】
规定 |s| 表示字符串 s 的长度(即字符串中的字符和空格数)。
对于 40% 的数据,1≤∣s∣≤5,保证输入为数字字符及行末换行符。
对于 80% 的数据,1≤∣s∣≤5,输入只可能包含大、小写英文字母、数字字符及行末换行符。
对于 100% 的数据,1≤∣s∣≤5,输入可能包含大、小写英文字母、数字字符、空格和行末换行符。

分析:我们分别采用三种方法来实现字符串的统计(可分别统计字符、数字 和 其他类型的字符)

        1.通过字符统计

        2.通过ASCLL码统计

        3.通过正则表达式统计

import java.util.Scanner;
import java.util.regex.Pattern;

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

        int count = 0; //用于字符统计
        int sum =  0; //用于ASCLL码统计
        int he=0;     //用于正则校验

        String s = sc.nextLine();

//        1.通过字符统计
        for (int i = 0; i < s.length() ; i++) {
            char temp = s.charAt(i);
            if(temp>='a' && temp<='z' || temp>='A' && temp<='Z'|| temp>='0' && temp<='9'){
                          count++;
            }
            else{
                continue;
            }
        }

/*//        2.通过ASCLL值统计
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            if(temp>=97 && temp<=122 || temp>=65 && temp<=90|| temp>=48 && temp<=57){
                sum++;
            }
            else {
                continue;
            }
        }

//        3.通过正则校验
        String  numRegular ="^[0-9]$",charRegular="^[a-zA-Z]$";
        Pattern p1 = Pattern.compile(numRegular);
        Pattern p2 = Pattern.compile(charRegular);
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            if(p1.matcher(""+temp).matches() || p2.matcher(""+temp).matches()){
                    he++;
            }else {
                continue;
            }
        }*/

        System.out.println(count);
/*        System.out.println(sum);
        System.out.println(he);*/
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_64428129/article/details/128359964