PAT (Basic Level) Practise (中文) 1057. 数零壹(20)
2017-09-12 21:53
405 查看
1057. 数零壹(20)
时间限制200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一串长度不超过105的字符串,本题要求你将其中所有英文字母的序号(字母a-z对应序号1-26,不分大小写)相加,得到整数N,然后再分析一下N的二进制表示中有多少0、多少1。例如给定字符串“PAT (Basic)”,其字母序号之和为:16+1+20+2+1+19+9+3=71,而71的二进制是1000111,即有3个0、4个1。
输入格式:
输入在一行中给出长度不超过105、以回车结束的字符串。
输出格式:
在一行中先后输出0的个数和1的个数,其间以空格分隔。
输入样例:
PAT (Basic)
输出样例:
3 4
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); String str=input.nextLine(); str=str.toLowerCase(); //System.out.println(str); int sum=0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i)>='a'&&str.charAt(i)<='z') { sum+=(int)str.charAt(i)-96; } } String result=Integer.toBinaryString(sum)+""; //System.out.println(result); int count0=0; int count1=0; for (int i = 0; i < result.length(); i++) { if (result.charAt(i)=='0') { count0++; }else if (result.charAt(i)=='1') { count1++; } } System.out.println(count0+" "+count1); } }
相关文章推荐
- PAT (Basic Level) Practise (中文)1057. 数零壹(20)
- PAT (Basic Level) Practise (中文)1034. 有理数四则运算(20) C语言
- PAT (Basic Level) Practise (中文)1042. 字符统计(20)
- PAT (Basic Level) Practise (中文)---1002 写出这个数 (20)
- PAT (Basic Level) Practise (中文)1028. 人口普查(20)
- PAT (Basic Level) Practise (中文) 1027. 打印沙漏(20)
- PAT (Basic Level) Practise (中文) 1033. 旧键盘打字(20)
- PAT (Basic Level) Practise (中文)1034. 有理数四则运算(20)
- PAT (Basic Level) Practise (中文)1043. 输出PATest(20)
- PAT (Basic Level) Practise (中文) 1037. 在霍格沃茨找零钱(20)
- PAT (Basic Level) Practise (中文)1072. 开学寄语(20)
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20)
- PAT (Basic Level) Practise (中文) 1063. 计算谱半径(20)
- PAT (Basic Level) Practise (中文)1004. 成绩排名 (20)
- PAT (Basic Level) Practise (中文)1022. D进制的A+B (20) C语言
- PAT (Basic Level) Practise (中文)1023. 组个最小数 (20)C语言
- PAT (Basic Level) Practise (中文)- 1014. 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise (中文) 1003.我要通过!(20)
- PAT (Basic Level) Practise (中文) 1012. 数字分类 (20)
- PAT (Basic Level) Practise (中文)1017. A除以B (20)