热身练习题2

进制转换

输入一个十进制数N,将它转换成R进制数输出。

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Input
7 2
23 12
-4 3
Output
111
1B
-11
import java.util.Scanner;

public class Main {//就除多少取余,注意负数的处理和逆序输出
    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();
        }
    }
}

人见人爱A+B

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位整数表示。
Input	
2
1 2 3 4 5 6
34 45 56 12 23 34
Output
5 7 9
47 9 30
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();
            for(int i=0;i<n;i++){
                int[]num1=new int[3];
                int[]num2=new int[3];
                int[]rs=new int[3];
                for(int j=0;j<3;j++){
                    num1[j]=sc.nextInt();
                }
                for(int j=0;j<3;j++){
                    num2[j]=sc.nextInt();
                }
                int temp=0;
                for(int j=2;j>=0;j--){
                    rs[j]=(num1[j]+num2[j]+temp);
                    if(j!=0){
                        if(rs[j]>59){//这里注意小时那一栏就不要处理了
                            rs[j]=rs[j]%60;
                            temp=1;
                        } else{
                            temp=0;
                        }
                    }
                }
                for(int j=0;j<3;j++){
                    if(j==0){
                        System.out.print(rs[j]);
                    } else {
                        System.out.print(" "+rs[j]);
                    }
                }
                System.out.println();
            }
        }
    }

}

人见人爱A-B

参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{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”,否则从小到大输出结果,为了简化问题,每个元素后面跟一个空格.
Input	
3 3 1 2 3 1 4 7
3 7 2 5 8 2 3 4 5 6 7 8 
0 0
Output
2 3 
NULL
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            int m=sc.nextInt();
            List<Integer>a=new ArrayList<>();
            List<Integer>b=new ArrayList<>();
            for(int i=0;i<n;i++){
                a.add(sc.nextInt());
            }
            for(int i=0;i<m;i++){
               b.add(sc.nextInt());
            }
            if(!(n==0&&m==0)){
                a.removeAll(b);//还得是Java
                Collections.sort(a);
                if (a.isEmpty())
                    System.out.println("NULL");
                else {
                    for (Integer integer : a) {
                        System.out.print(integer + " ");
                    }
                    System.out.println();
                }
            }

        }
    }
}

人见人爱A^B

求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”

Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Input
2 3
12 6
6789 10000
0 0
Output
8
984
1
import java.util.Scanner;

public class Main {

    static int power(int a,int n)
    {
        int ans=1;
        while(n!=0)
        {
            if(n%2==1) ans=(ans*a)%1000; // 奇数情况
            a=(a*a)%1000; // 底数平方取后3位
            n=n/2; // 指数减半
        }
        return ans;
    }


    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(!(b==0&&a==0)){
                System.out.println(power(a,b));
            }
        }
    }
}

改革春风吹满地

Input
输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1, y1, x2, y2... xn, yn),为了简化问题,这里的所有坐标都用整数表示。
输入数据中所有的整数都在32位整数范围内,n=0表示数据的结束,不做处理。
Output
对于每个测试实例,请输出对应的多边形面积,结果精确到小数点后一位小数。
每个实例的输出占一行。
Input	
3 0 0 1 0 0 1
4 1 0 0 1 -1 0 0 -1
0
Output
0.5
2.0
import java.util.Scanner;
//s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ]

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){

            int n=sc.nextInt();
            double[][]arr=new double[105][2];
            double s=0;
            for(int i=0;i<n;i++){
                arr[i][0]=sc.nextDouble();
                arr[i][1]=sc.nextDouble();
            }
            if(n==0)continue;
            arr[n][0]=arr[0][0];
            arr[n][1]=arr[0][1];
            for(int i=0;i<n;i++){
                s+=(arr[i][0]*arr[i+1][1]-arr[i+1][0]*arr[i][1]);
            }
            System.out.printf("%.1f",s/2);
            System.out.println();

        }

    }
}

LELE的RPG难题

人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,"可乐",经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:

有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法.

以上就是著名的RPG难题.

如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧?
Input
输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0<n<=50)。
Output
对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行。
Input
1
2
Output
3
6
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        long[]arr=new long[51];
        arr[1]=3;arr[2]=arr[3]=6;
        for(int i=4;i<51;i++){
            arr[i]=arr[i-1]+arr[i-2]*2;
        }
        while(sc.hasNext()){
            System.out.println(arr[sc.nextInt()]);
        }
    }
}

解析

阿牛的EOF牛肉串

今年的ACM暑期集训队一共有18人,分为6支队伍。其中有一个叫做EOF的队伍,由04级的阿牛、XC以及05级的COY组成。在共同的集训生活中,大家建立了深厚的友谊,阿牛准备做点什么来纪念这段激情燃烧的岁月,想了一想,阿牛从家里拿来了一块上等的牛肉干,准备在上面刻下一个长度为n的只由"E" "O" "F"三种字符组成的字符串(可以只有其中一种或两种字符,但绝对不能有其他字符),阿牛同时禁止在串中出现O相邻的情况,他认为,"OO"看起来就像发怒的眼睛,效果不好。

你,NEW ACMer,EOF的崇拜者,能帮阿牛算一下一共有多少种满足要求的不同的字符串吗?

PS: 阿牛还有一个小秘密,就是准备把这个刻有 EOF的牛肉干,作为神秘礼物献给杭电五十周年校庆,可以想象,当校长接过这块牛肉干的时候该有多高兴!这里,请允许我代表杭电的ACMer向阿牛表示感谢!

再次感谢!

Input
输入数据包含多个测试实例,每个测试实例占一行,由一个整数n组成,(0<n<40)。
Output
对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行。
Input	
1
2
Output
3
8

解析

神、上帝以及老天爷

HDU 2006'10 ACM contest的颁奖晚会隆重开始了!
为了活跃气氛,组织者举行了一个别开生面、奖品丰厚的抽奖活动,这个活动的具体要求是这样的:

首先,所有参加晚会的人员都将一张写有自己名字的字条放入抽奖箱中;
然后,待所有字条加入完毕,每人从箱中取一个字条;
最后,如果取得的字条上写的就是自己的名字,那么“恭喜你,中奖了!”

大家可以想象一下当时的气氛之热烈,毕竟中奖者的奖品是大家梦寐以求的Twins签名照呀!不过,正如所有试图设计的喜剧往往以悲剧结尾,这次抽奖活动最后竟然没有一个人中奖!

我的神、上帝以及老天爷呀,怎么会这样呢?

不过,先不要激动,现在问题来了,你能计算一下发生这种情况的概率吗?

不会算?难道你也想以悲剧结尾?!
Input
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(1<n<=20),表示参加抽奖的人数。

Output
对于每个测试实例,请输出发生这种情况的百分比,每个实例的输出占一行, 结果保留两位小数(四舍五入),具体格式请参照sample output。

Input
1
2
Output
50.00%

错排

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double[]arr=new double[21];
        arr[1]=0;arr[2]=1;
        for(int i=3;i<21;i++){
            arr[i]=(i-1)*(arr[i-1]+arr[i-2]);
        }
        while(sc.hasNext()){
            int n=sc.nextInt();
            for(int i=0;i<n;i++){
                int num=sc.nextInt();
                double sum=1;
                for(int j=1;j<=num;j++){
                    sum*=j;
                }
                System.out.printf("%.2f%%",(arr[num]/sum)*100);
                System.out.println();
            }
        }
    }
}

折线分割平面

题解

Switch Game

There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
Sample
Input
1
5
Output
1
0

Consider the second test case:

The initial condition : 0 0 0 0 0 …
After the first operation : 1 1 1 1 1 …
After the second operation : 1 0 1 0 1 …
After the third operation : 1 0 0 0 1 …
After the fourth operation : 1 0 0 1 1 …
After the fifth operation : 1 0 0 1 0 …

The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.

开关闭合,就是看约数是奇数还是偶数

import java.util.Scanner;

public class Main {

    static boolean solve(int n){
        int count=0;
        for(int i=1;i<=n;i++){
            if(n%i==0){
                count++;
            }
        }
        return count % 2 != 0;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            if(solve(n)){
                System.out.println(1);
            } else {
                System.out.println(0);
            }
        }
    }
}

慢慢的心境也变得平和了,变了但也没变,我还是很喜欢那朵花
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是君倩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值