汉字统计
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample
Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Output
14
9
汉字的Ascii码是负数,一个汉字占两个字节,所以要除二
package reshen2;
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++){
int count=0;
byte[]arr=sc.nextLine().getBytes();
for(byte c:arr){
if(c<0){
count++;
}
}
System.out.println(count/2);
}
}
}
进制转换
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample
Input
7 2
23 12
-4 3
Output
111
1B
-11
除多少取余
package reshen2;
import java.util.Scanner;
public class E {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()){
int[] arr=new int[500];
int c=0;
int n=sc.nextInt();int r=sc.nextInt();
if(n==0) {
System.out.println(0);
continue;
}
if(n<0){
System.out.print("-");
n=-n;
}
while(n!=0){
arr[c]=n%r;
c++;
n/=r;
}
for(int i=c-1;i>=0;i--){
if(arr[i]>9){
System.out.printf("%c",'A'+arr[i]-10);
} else {
System.out.print(arr[i]);
}
}
System.out.println();
}
}
}