【Java】openjudge

java中int是32位有符号整数类型

java 遍历字符串

1.charAt()

  • length():返回此字符序列的长度。
  • charAt(int index):返回指定索引处的char值。

描述

对于给定的一个字符串,统计其中数字字符出现的次数

输入

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

输出

对于每个测试实例,输出该串中数值的个数,每个输出占一行。

样例输入

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

样例输出

6
9

答案

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
//        System.out.println("Hello World");
        Scanner cin=new Scanner(System.in);
        int n= cin.nextInt();
        cin.nextLine();
        for(int i=1;i<=n;i++){
            int sum=0;
            String str=cin.nextLine();
            for(int j=str.length();j-->0;){
                int chr=str.charAt(j);
                if(chr>48 && chr<57){
//                    System.out.println(chr);
                    sum++;
                }
            }
            System.out.println(sum);
        }
    }
}

判断字符串是否为数字

(20条消息) java判断字符串是否为数字的几种方式_Morning sunshine的博客-CSDN博客

ASCII 表 | 菜鸟教程 (runoob.com)

遍历字符串

(20条消息) java字符串遍历的几种方法_humor2020的博客-CSDN博客

65-90 97-122 48-57  95_

描述

将十六进制数转换成十进制数

输入

第一行有一个整数T,表示共有T组数据
接下来T行,每一行为一个16进制无符号正整数,位数不超过8位,数中的a-f均为大写字母,数前没有多余的0

输出

输出共T行,每一行是一组数据的十进制表示,数字前不得有多余的0。
十进制数小于2^31

样例输入

4
A
F
FFFE
10001

样例输出

10
15
65534
65537

答案

import java.io.*;
import java.util.*;

//Math.pow    str.toCharArray()
public class Main {
    public static void main(String[] args) {
//        System.out.println("Hello World");
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        cin.nextLine();
        for(int i=1;i<=n;i++){
            String str = cin.nextLine();
            char[] chars = str.toCharArray();
            long sum=0;
            int l = chars.length;
            for(int j=0;j<l;j++){
                if((int)chars[j]>=65){
                    if(chars[j]=='A'){
                        sum+=10*Math.pow(16,l-j-1);
                    }
                    else if(chars[j]=='B'){
                        sum+=11*Math.pow(16,l-j-1);
                    }
                    else if(chars[j]=='C'){
                        sum+=12*Math.pow(16,l-j-1);
                    }
                    else if(chars[j]=='D'){
                        sum+=13*Math.pow(16,l-j-1);
                    }
                    else if(chars[j]=='E'){
                        sum+=14*Math.pow(16,l-j-1);
                    }
                    else if(chars[j]=='F'){
                        sum+=15*Math.pow(16,l-j-1);
                    }
                }
                else{
                    sum+=((int)chars[j]-48)*Math.pow(16,l-j-1);
                }
            }
            System.out.println(sum);
        }
    }
}

(22条消息) 【蓝桥杯】基础练习 十六进制转十进制(Java实现)_allyyhh的博客-CSDN博客_蓝桥杯十六进制转十进制

(22条消息) JAVA实现16进制转10进制_奥特曼下象棋的博客-CSDN博客_java十六进制转十进制

 System.out.println(Long.parseLong("473",  16));  
 System.out.println(Long.parseLong("473",10)); 

(22条消息) Java:十六进制转换成十进制_zl1zl2zl3的博客-CSDN博客

描述

输入n个整数,统计每个数出现的次数.

输入

第一行是一个整数n(1<=n<=1000),接下来n行每行一个整数.

输出

第一行输出总共有多少个不同的整数.
接下来每行输出一个整数及这个整数出现的次数,用空格分隔.
输出的整数的顺序与它们在输入中第一次出现的顺序一致(即在输入中先出现的数,也会在输出中先出现)

样例输入

5
2
3
2
1
2

样例输出

3
2 3
3 1
1 1

答案

import java.io.*;
import java.util.*;

//Math.pow    str.toCharArray()
public class Main {
    public static void main(String[] args) {
//        System.out.println("Hello World");
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        int sum=1;
        int[] num = new int[11];//定义数组默认值为0
        int[] ord = new int[11];
        for(int i=0;i<n;i++){
            int m=cin.nextInt();
            num[m]++;
            int flag=1;
            for(int j=0;j<i;j++){
                if(ord[j]==m){
                    flag=0;
                    break;
                }
            }
            if(flag==1){
                ord[sum++]=m;
            }
        }
        System.out.println(sum-1);
//        Arrays.sort(nums, new Comparator<int[]>() {
//            @Override
//            public int compare(int[] o1, int[] o2) {
//                return o1[1]-o2[1];
//            }
//        });
        for(int j=1;j<=sum-1;j++){
            System.out.println(ord[j]+" "+num[ord[j]]);
        }
    }
}

其他:

统计数字出现的次数

(22条消息) java —— 数字的出现次数_逃逃日塔灰的博客-CSDN博客_java统计数组中数字出现的次数

描述

求10000以内n的阶乘。

输入

只有一行输入,整数n(0<=n<=10000)。

输出

一行,即n!的值。

样例输入

100

样例输出

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

答案

runtime error

import java.io.*;
import java.util.*;

//Math.pow    str.toCharArray()
public class Main {
    public static void main(String[] args) {
//        System.out.println("Hello World");
        Scanner cin=new Scanner(System.in);
        int n=0,w=1;        //l 位数
        n=cin.nextInt();
        int[] a=new int[10000];
        a[0]=0;     //
        a[1]=1;         //避免相乘全部为0
        for(int i=1;i<=n;i++){
            for(int j=1;j<=w;j++){
                a[j]*=i;
                a[j]+=a[j-1]/10;
                a[j-1]%=10;
                if(a[j]>=10 && j>=w) w++;
            }
        }
        for(int i=w;i>=1;i--)
            System.out.print(a[i]);
    }
}

Accepted

import java.util.*;
import java.math.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin =new Scanner(System.in);
        int n =cin.nextInt();
        BigInteger bigInteger = new BigInteger("1");
        for(int i=1;i<=n;i++)
            bigInteger=bigInteger.multiply(BigInteger.valueOf(i));
        System.out.println(bigInteger);
    }
}

(22条消息) java大数总结_pxlsdz的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

返返返

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

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

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

打赏作者

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

抵扣说明:

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

余额充值