【ACM】P2030、P2031、P2032、P2033、P2034代码演示

[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();
            //遍历每个字符是否大于128
            char[] chs=str.toCharArray();
            int count=0;
            for(char ch:chs){
                //如果大于128就计数
                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;

/*按照要求对输出结果进行处理:
 * 1、输出的第一个m应该在最后输出,最后输出的m应该在最开始输出---翻转
 * 2、如果m>10,则用其对应的大写字母---造表、查表
 */
public class P2031 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            //接收n,r
            int n=sc.nextInt();
            int r=sc.nextInt();
            //判断n是否为负数且做标记isminus
            boolean isminus=true;
            //如果n<0,取其绝对值;否则改变标记的状态值为false
            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++){
                    //暂时将二维数组中的所有值赋值为1
                    a[i][j]=1;
                }
            }

            //从第二行开始,对其每一行的第[ 1 ~ a[i].length-1 ]中的每个数重新根据规律计算赋值
            for(int i=2;i<a.length;i++){
                for(int j=1;j<a[i].length-1;j++){
                    //找到规律:a[i][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()) {
            // 接收n,m
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] a = new int[n];
            int[] b = new int[m];
            if (n == 0 && m == 0) {
                break;
            }
            // 创建a,b数组
            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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值