杭电acm P2017字符串的统计
来源:互联网 发布:nba2k16捏脸数据 编辑:程序博客网 时间:2024/05/16 19:02
自己写的:
import java.util.Scanner; public class P2017{public static void main(String[] args) {Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int[] count=new int[n]; String[] str = new String[n]; for(int i=0;i<n;i++){ str[i]=sc.next(); int num=str[i].length(); for(int j=0;j<num;j++){ if(str[i].charAt(j)>='0'&&str[i].charAt(j)<='9'){ count[i]++; } } } for(int i=0;i<n;i++){ System.out.println(count[i]); } }
}
}
/*
* sc.nextInt(): 只接收整数,,整数后面的换行符还留在流内不读出来
* sc.next(): 接收到空白符就丢,直到接收到一个非空白符的字符串,以遇到下一个空白符为停止标记
* sc.nextLine(): 接收到换行符就停,把换行符及前面部分从流中取出来, 把换行符丢掉,剩下的部分就是返回值
*/
更优方法:
public class P2017 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine();//吸掉多余换行符 while(n-->0){ //String str = sc.next(); String str = sc.nextLine(); int count=0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch>='0' && ch<='9'){ count++; } } System.out.println(count); }}
}
阅读全文
0 0
- 杭电acm P2017字符串的统计
- ACM——p2017 字符串统计
- 杭电acm 2017统计字符串中数字的个数
- 杭电ACM 2017 字符串统计
- 杭电ACM 2017 字符串统计
- 杭电ACM 2017 字符串统计
- 杭电ACM 2017:字符串统计
- 杭电 acm 2017(字符串统计)
- 杭电ACM----2017字符串统计
- 杭电acm 2017字符串统计(字符串处理)
- 杭电ACM----统计汉字的个数
- 杭电ACM 汉字统计
- 杭电acm统计字符
- 杭电ACM 统计元音
- 杭电oj 2017 字符串的统计
- 字符串统计(杭电2017)
- 杭电acm2017字符串统计
- 杭电ACM2017-字符串统计
- 基于kolla部署多节点OpenStack(Ocata版)
- Spring Boot Servlet
- vue div contenteditable属性,模拟v-model双向数据绑定功能
- 【前端】--console.log和alert的区别
- HTML(十七)
- 杭电acm P2017字符串的统计
- 工作周报-2017年12月3日
- flask:使用flask-mail客户端访问触发发送邮件
- Spring Boot 过滤器、监听器
- sanic下通过信号(blinker)创建cassandra的表索引
- dedecms循环输出时limit用法
- CentOS 7安装python3.5版本
- 图解物联网IoT未来技术
- 图结构及其应用