[P2030问题概述]:汉字统计
/**
* 汉字统计
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45559 Accepted Submission(s): 24828
Problem Description
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Sample Output
14
9
Author
lcy
Source
C语言程序设计练习(五)
*/
代码演示:
package ac;
import java.util.Scanner;
public class P2030{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in );
int n=sc.nextInt();
sc.nextLine();
while (n-->0 ) {
String str=sc.nextLine();
char [] chs=str.toCharArray();
int count=0 ;
for (char ch:chs){
if (ch>128 ){
count++;
}
}
System.out .println(count);
}
}
}
[P2031问题概述]:进制转换
/**
* 进制转换
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49651 Accepted Submission(s): 27284
Problem Description
输入一个十进制数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
Sample Output
111
1B
-11
Author
lcy
Source
C语言程序设计练习(五)
*/
代码演示:
package ac;
import java.util.ArrayList;
import java.util.Scanner;
public class P2031 {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in );
while (sc.hasNext()){
int n=sc.nextInt();
int r=sc.nextInt();
boolean isminus=true ;
if (n<0 ){
n=Math.abs(n);
}else {
isminus=false ;
}
ArrayList<String> list=new ArrayList<String>();
String strm=null ;
while (n!=0 ){
int m=n%r;
n=n/r;
strm=m+"" ;
if (m>=10 ){
strm=(char )(m+55 )+"" ;
}
list.add(strm);
}
if (isminus){
System.out .print("-" );
}
for (int i=list.size()-1 ;i>=0 ;i--){
System.out .print(list.get (i));
}
System.out .println();
}
}
}
[P2032问题概述]:杨辉三角
/**
* 杨辉三角
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 68049 Accepted Submission(s): 28130
Problem Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。
Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。
Sample Input
2 3
Sample Output
1
1 1
1
1 1
1 2 1
Author
lcy
Source
C语言程序设计练习(五)
*/
代码演示(简单方式):
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in );
while (sc.hasNext()) {
int n = sc.nextInt();
int arr[][] = new int [n][n];
for (int i = 0 ; i < n; i++) {
for (int j = 0 ; j <= i; j++) {
if (i == j || j == 0 ) {
arr[i][j] = 1 ;
}
if (i > 1 && j > 0 && j < n) {
arr[i][j] = arr[i - 1 ][j - 1 ] + arr[i - 1 ][j];
}
System.out .print(j == 0 ? arr[i][j] : " " + arr[i][j]);
}
System.out .println();
}
System.out .println();
}
}
}
代码演示(复杂方式):
package ac;
import java.util.Scanner;
public class P2032 {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in );
while (sc.hasNext()){
int n=sc.nextInt();
int [][] a =new int [n][];
for (int i=0 ;i<a.length;i++){
a[i]=new int [i+1 ];
for (int j=0 ;j<a[i].length;j++){
a[i][j]=1 ;
}
}
for (int i=2 ;i<a.length;i++){
for (int j=1 ;j<a[i].length-1 ;j++){
a[i][j]=a[i-1 ][j]+a[i-1 ][j-1 ];
}
}
for (int i=0 ;i<a.length;i++){
for (int j=0 ;j<a[i].length;j++){
if (j!=0 ){
System.out .print(" " +a[i][j]);
}else {
System.out .print(a[i][j]);
}
}
System.out .println();
}
System.out .println();
}
}
}
[P2033问题概述]:人见人爱A+B
/**
* 人见人爱A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45578 Accepted Submission(s): 30167
Problem Description
HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,
也希望这个题目能唤起大家对ACM曾经的热爱。
这个题目的A和B不是简单的整数,而是两个时间,A和B 都是由3个整数组成,分别表示时分秒,
比如,假设A为34 45 56,就表示A所表示的时间是34小时 45分钟 56秒。
Input
输入数据有多行组成,首先是一个整数N,表示测试实例的个数,然后是N行数据,
每行有6个整数AH,AM,AS,BH,BM,BS,分别表示时间A和B所对应的时分秒。题目保证所有的数据合法。
Output
对于每个测试实例,输出A+B,每个输出结果也是由时分秒3部分组成,同时也要满足时间的规则(即:分和秒的取值范围在0~59),
每个输出占一行,并且所有的部分都可以用32位整数表示。
Sample Input
2
1 2 3 4 5 6
34 45 56 12 23 34
Sample Output
5 7 9
47 9 30
Author
lcy
Source
ACM程序设计期末考试(2006/06/07)
*/
代码演示(简单方式):
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in );
int n=sc.nextInt();
while (n-->0 ){
int ah=sc.nextInt();
int am=sc.nextInt();
int as =sc.nextInt();
int bh=sc.nextInt();
int bm=sc.nextInt();
int bs=sc.nextInt();
int s=(as +bs)%60 ;
int r=(as +bs)/60 ;
int m=(am+bm+r)%60 ;
r=(am+bm+r)/60 ;
int h=ah+bh+r;
System.out .println(h+" " +m+" " +s);
}
}
}
代码演示(复杂方式):
package ac;
import java.util.Scanner;
public class P2033 {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in );
int n=sc.nextInt();
while (n-->0 ){
int AH=sc.nextInt();
int AM=sc.nextInt();
int AS=sc.nextInt();
int BH=sc.nextInt();
int BM=sc.nextInt();
int BS=sc.nextInt();
int sumH=AH+BH;
int sumM=AM+BM;
int sumS=AS+BS;
if (sumS/60 >0 ){
sumM=sumM+sumS/60 ;
sumS=sumS%60 ;
}
if (sumM/60 >0 ){
sumH=sumH+sumM/60 ;
sumM=sumM%60 ;
}
System.out .println(sumH+" " +sumM+" " +sumS);
}
}
}
[P2034问题概述]:人见人爱A-B
/**
* 人见人爱A-B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 82038 Accepted Submission(s): 22909
Problem Description
参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,
今天我们这个A-B求的是两个集合的差,就是做集合的减法运算。(当然,大家都知道集合的定义,就是同一个集合中不会有两个相同的元素,
这里还是提醒大家一下)
呵呵,很简单吧?
Input
每组输入数据占1行,每行数据的开始是2个整数n(0<=n<=100)和m(0<=m<=100),分别表示集合A和集合B的元素个数,
然后紧跟着n+m个元素,前面n个元素属于集合A,其余的属于集合B. 每个元素为不超出int范围的整数,元素之间有一个空格隔开.
如果n=0并且m=0表示输入的结束,不做处理。
Output
针对每组数据输出一行数据,表示A-B的结果,如果结果为空集合,则输出“NULL”,否则从小到大输出结果,为了简化问题,每个元素后面跟一个空格.
Sample Input
3 3 1 2 3 1 4 7
3 7 2 5 8 2 3 4 5 6 7 8
0 0
Sample Output
2 3
NULL
Author
lcy
Source
ACM程序设计期末考试(2006/06/07)
*/
代码演示(有输出结果,但是未AC):
package ac;
import java.util.Arrays;
import java.util.Scanner;
public class P2034 {
public static void main (String[] args) {
System.out .println("::::" );
Scanner sc = new Scanner(System.in );
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
int [] a = new int [n];
int [] b = new int [m];
if (n == 0 && m == 0 ) {
break ;
}
for (int i = 0 ; i < a.length; i++) {
a[i] = sc.nextInt();
}
for (int j = 0 ; j < b.length; j++) {
b[j] = sc.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
int all = 0 ;
for (int i = 0 ; i < a.length; i++) {
int count = 0 ;
for (int j = 0 ; j < b.length; j++) {
if (a[i] != b[j]) {
count++;
if (count == b.length) {
System.out .print(a[i] + " " );
all++;
}
}
}
}
if (all == 0 ) {
System.out .print("NULL" );
}
System.out .println();
}
}
}