安徽科技学院Contest1116

来源:互联网 发布:mt4软件手机版 编辑:程序博客网 时间:2024/04/27 14:35

Problems

Contest Statistics
Standing

A: 筛排处理

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 422 Solved: 143
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N<=100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作

Input

每组输入数据的第一行含有一个正整数N,表示后面行中有N个随机整数。若N=0,表示处理结束。

Output

对应每组输入数据,输出一组数据,该数据由单独一行开头,内含一个N,表示后面有N个排好序的整数,整数之间空一格。每组输出数据之间空一行。

Sample Input

12 2 4 6 17 20 40 43 45 60 64 85 98 17 35 75 40 61 56 21 85 61 50 83
52 22 44 68 51 80 38 0

Sample Output

12 2 4 6 17 20 40 43 45 60 64 85 98
16 21 22 35 38 40 44 50 51 52 56 61 68 75 80 83 85

HINT

import java.util.Collection;import java.util.Iterator;import java.util.Scanner;import java.util.TreeSet;/** * Created by c on 2017/7/24. */public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while(scanner.hasNext()){            int n = scanner.nextInt();            if(n==0)break;            TreeSet<Integer> treeSet = new TreeSet<>();            for(int i = 0; i < n; i++){                Integer x = scanner.nextInt();                treeSet.add(x);            }            System.out.println(treeSet.size());            Iterator it = treeSet.iterator();            System.out.print(it.next());            while(it.hasNext()){                System.out.print(" " + it.next());            }            System.out.println();            System.out.println();        }    }}

B: dota2 and nba2k

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 301 Solved: 149
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

无重力学长喜欢玩dota2,狙击枪学长喜欢玩nba2k,但是他们不喜欢英文字母,所以想请大家帮个忙。

Input

第一行给出输入组数t,共有t组测试数据,每组给出一个字符串

Output

字符串中的数字

Sample Input

2 dota2 nba2k

Sample Output

2 2

HINT

import java.io.PrintWriter;import java.util.Scanner;public class Main {    static Scanner cin = new Scanner(System.in);    static PrintWriter cout = new PrintWriter(System.out);    public static void main(String[] args) {        for (int n = cin.nextInt(), i = 1; i <= n; i++) {            cout.println(cin.next().replaceAll("\\D+", ""));        }        cout.close();        cin.close();    }}

C: 算法实现题 3-4 多重幂计数问题

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 28 Solved: 4
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

设给定 n 个变量 x1 , x2 ,…, xn 。将这些变量依序作底和各层幂,可得 n 重幂如下

x1x2 x3 xn

这里将上述 n 重幂看作是不确定的,当在其中加入适当的括号后,才能成为一个确定的

n 重幂。不同的加括号方式导致不同的 n 重幂。例如,当 n=4 时,全部 4 重幂有 5 个。

«算法设计:

对 n 个变量计算出有多少个不同的 n 重幂。

Input

只有一行,提供一个数n 。

Output

输出一行将找到的序关系数

Sample Input

4

Sample Output

5

HINT


D: 求s=a+aa+aaa+aaaa+aa…a的值

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 561 Solved: 315
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个一位的整数。
例如2+22+222+2222+22222(此时共有5个数相加)

Input

整数a和n(n个数相加,1<= n, a<=9)

Output

s的值

Sample Input

2 2

Sample Output

24

HINT

import java.util.*;public class Main{    public static void main(String[] args)    {        Scanner in = new Scanner(System.in);        System.out.println(fun(in.nextInt(), in.nextInt()));        in.close();    }    private static int fun(int a, int n){        int sum = 0;        StringBuffer sb = new StringBuffer();        for(int i = 1;i <= n; i++){            sb.append(a);            sum += Integer.valueOf(sb.toString());        }        return sum;    }}

E: 选数

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 72 Solved: 28
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n)。从 n 个整数中任选 k 个整数相加,可分别得到一系列的和。例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为:
3+7+12=22  3+7+19=29  7+12+19=38  3+12+19=34。
现在,要求你计算出和为素数共有多少种。
例如上例,只有一种的和为素数:3+7+19=29)。
Input

n , k (1<=n<=20,k<n) x1,x2,…,xn (1<=xi<=5000000

Output

一个整数(满足条件的种数)。

Sample Input

4 3 3 7 12 19

Sample Output

1

代码:

import java.util.Scanner;public class Main {    static int n, k, a[], ans;    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        while (cin.hasNext()) {            n = cin.nextInt();            k = cin.nextInt();            a = new int[n];            for (int i=0; i<n; i++)                a[i] = cin.nextInt();            ans = 0;            dfs(0, 0, 0);            System.out.println(ans);        }        cin.close();    }    private static void dfs(int cur, int cnt, int sum) {        if (cnt == k) {            if (isPrime(sum)) {                ans++;                return;            }        }else {            for (int i=cur; i<n; i++) {                dfs(i + 1, cnt + 1, sum + a[i]);            }        }    }    private static boolean isPrime(int n) {        if (n % 2 == 0 && n  != 2 || n < 2)            return false;        for (int i=3; i*i<=n; i+=2) {            if (n % i == 0)                return false;        }        return true;    }}

F: 图形输出

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 264 Solved: 73
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

算法提高 图形输出
时间限制:1.0s 内存限制:512.0MB

  编写一程序,在屏幕上输出如下内容:
   X | X | X
  —+—+—
   | |
  —+—+—
  O | O | O
  注意:本题请同学们严格按照图形的格式输出,对齐,其中X和O为大写,否则系统会判为错误。看不懂格式的话往下看HINT部分

Input

Output

Sample Input

Sample Output

HINT

X | X | X
—+—+—
| |
—+—+—
O | O | O

九宫格形状。。。横竖要对齐,后台样例输入的时候貌似字符大小间距会变小啊,大家将就着脑补哈

#include<iostream>#include<cstdio>using namespace std;int main(){    cout<<" X | X | X "<<endl;    cout<<"---+---+---"<<endl;    cout<<"   |   |   "<<endl;    cout<<"---+---+---"<<endl;    cout<<" O | O | O "<<endl;}

G: [分支训练] 这个月有多少天

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 357 Solved: 250
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

输入年月,判断这年的这个月有多少天~

注意闰年

Input

年月 y m

Output

天数 d

Sample Input

2015 2

Sample Output

28

HINT

注意闰年~

import java.util.Calendar;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int y = in.nextInt();        int m = in.nextInt();        Calendar c = Calendar.getInstance();        c.set(y, m, 1);//将今天设置为m+1月的1日        c.add(Calendar.DATE, -1);//然后日减一天//      System.out.println(c.get(Calendar.DATE));        System.out.println(c.get(Calendar.DAY_OF_MONTH));        in.close();    }}

H: 口袋的天空(Kruscal)

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 44 Solved: 24
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空。
有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖。
给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在一起。
现在小杉要把一些云朵连在一起,做成K个棉花糖,一个棉花糖最少要用掉一朵云,小杉想知道他怎么连,花费的代价最小。
Input

每组测试数据的 第一行有三个数N,M,K(1<=N<=1000,1<=M<=10000,1<=K<=10)
接下来M个数每行三个数X,Y,L,表示X云和Y云可以通过L的代价连在一起。(1<=X,Y<=N,0<=L<10000)
30%的数据N<=100,M<=1000

Output

对每组数据输出一行,仅有一个整数,表示最小的代价。 如果怎么连都连不出K个棉花糖,请输出’No Answer’。

Sample Input

3 1 2
1 2 1

Sample Output

1

HINT

import java.util.Arrays;import java.util.Scanner;public class Main{    static Scanner sc=new Scanner(System.in);    static class Node implements Comparable<Node>{        int x,y,l;        Node(int x,int y,int l){            this.x=x;            this.y=y;            this.l=l;        }        @Override        public int compareTo(Node o) {            return this.l-o.l;        }    }    public static void main(String[] args) {        while(sc.hasNext()){            int n=sc.nextInt(),                m=sc.nextInt(),                k=sc.nextInt();            Node[] node=new Node[m];            for(int i=0;i<m;i++){                node[i]=new Node(sc.nextInt(),                        sc.nextInt(),                        sc.nextInt());            }            if(k>n){                System.out.println("No Answer");                continue;            }            if(k==n){                System.out.println("0");                continue;            }            Arrays.sort(node);            if(!Kruscal(node,n,m,k))                System.out.println("No Answer");        }        sc.close();    }    static int count,cost;    private static boolean Kruscal(Node[] node, int n, int m,int k) {        int[] flag=new int[n+1];        for(int i=1;i<=n;i++)            flag[i]=i;        count=n;cost=0;        for(int i=0;i<m;i++){            merge(node[i].x,node[i].y,node[i].l,flag);            if(count==k) {                System.out.println(cost);                return true;            }        }        return false;    }    private static int find(int[] flag,int a){        if(a!=flag[a])            flag[a]=find(flag,flag[a]);        return flag[a];    }    private static void merge(int a,int b,int c,int[] flag){        int fa=find(flag,a);        int fb=find(flag,b);        if(fa!=fb){            flag[fa]=fb;            count--;            cost+=c;        }    }}

I: 字符串比较

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 128 Solved: 93
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

算法提高 字符串比较
时间限制:1.0s 内存限制:512.0MB

独立实现标准字符串库的strcmp函数,即字符串比较函数,从键盘输入两个字符串,按字典序比较大小,前者大于后者输出1,前者小于后者输出-1,两者相等输出0。
样例输入:

apple one

样例输出:

-1

样例输入:

hello he

样例输出:

1

样例输入:

hello hello

样例输出:

0

import java.util.Scanner;/** * Created by zhaop on 2017/8/27. */public class Main {    public static void main(String[] args) {        Scanner cin=new Scanner(System.in);        while (cin.hasNext()){            String[] a=cin.nextLine().split("\\s+");           // String b=cin.nextLine();            int min=999999,count=0;           if (a[0].compareTo(a[1])>0) System.out.println("1");           else if (a[0].compareTo(a[1])==0) System.out.println("0");           else System.out.println("-1");        }    }}

J: 剪格子

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 21 Solved: 15
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

历届试题 剪格子
时间限制:1.0s 内存限制:256.0MB

问题描述
如下图所示,3 x 3 的格子中填写了一些整数。
+–*–+–+
|10* 1|52|
+–**–+
|20|30* 1|
*–+
| 1| 2| 3|
+–+–+–+
我们沿着图中的星号线剪开,得到两个部分,每个部分的数字和都是60。
本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以分割为两个部分,使得这两个区域的数字和相等。
如果存在多种解答,请输出包含左上角格子的那个区域包含的格子的最小数目。
如果无法分割,则输出 0。
输入格式
程序先读入两个整数 m n 用空格分割 (m,n< 10)。
表示表格的宽度和高度。
接下来是n行,每行m个正整数,用空格分开。每个整数不大于10000。
输出格式
输出一个整数,表示在所有解中,包含左上角的分割区可能包含的最小的格子数目。
样例输入1

3 3

10 1 52

20 30 1

1 2 3

样例输出1

3

样例输入2

4 3

1 1 1 1

1 30 80 2

1 1 1 100

样例输出2

10


K: IP判断

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 370 Solved: 257
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

在基于Internet的程序中,我们常常需要判断一个IP字符串的合法性。
合法的IP是这样的形式:
A.B.C.D
其中A、B、C、D均为位于[0, 255]中的整数。为了简单起见,我们规定这四个整数中不允许有前导零存在,如001这种情况。
现在,请你来完成这个判断程序吧^_^

Input

输入由多行组成,每行是一个字符串,输入由“End of file”结束。
字符串长度最大为30,且不含空格和不可见字符

Output

对于每一个输入,单独输出一行
如果该字符串是合法的IP,输出Y,否则,输出N

Sample Input

1.2.3.4
a.b.c.d
267.43.64.12
12.34.56.bb
210.43.64.129
-123.4.5.6

Sample Output

Y
N
N
N
Y
N

import java.util.Arrays;import java.util.Scanner;/** * Created by c on 2017/7/24. */class Check{    public static boolean check(String[] s){        for(String i : s){            if(!check(i))                return false;        }        return true;    }    public static boolean check(String s){        int n = 0;        try{            n = Integer.valueOf(s);        }catch (Exception e){            return false;        }        return n<=225&&n>=0;    }}public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while(scanner.hasNext()) {            String s = scanner.nextLine();            String[] word = s.split("\\.");            //System.out.println(Arrays.toString(word));            if(Check.check(word))                System.out.println("Y");            else                System.out.println("N");        }        scanner.close();    }}

L:题目 3 函数求值

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 32 Solved: 3
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

算法提高 题目 3 函数求值
时间限制:1.0s 内存限制:1.0GB

问题描述
设 F(N) 表示正整数 1 到正整数 N 中,数字 1,2 总共出现了多少次。例如 N = 10 时:1, 2, 3, 4, 5, 6, 7, 8, 9, 10 这 10 个数中,数字 1 出现了两次,数字 2 出现了 1 次,所以数字 1, 2 总共出现了 3 次,因此 F (10) = 3。
现在给你正整数 N ,请你求出 F(N) 的值。由于 F(N) 可能很大,你仅需输出 F(N) 除以 20123 的余数。
输入格式

输入数据仅一行,包含一个正整数 N (1 ≤ N ≤ 10^100 ),表示函数 F(N)的参数。

输出格式

输出仅一个整数,为 F(N) 除以 20123 的余数。

样例输入

10

样例输出

3


M: 算法实现题 2-15 有向直线 2 中值问题

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 5 Solved: 0
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

给定一条有向直线 L 以及 L 上的 n+1 个点 x0 < x1 < < xn 。有向直线L 上的每个点xi都有一个权w(xi ) ;每条有向边(xi , xi-1 ) 也都有一个非负边长d(xi , xi-1 ) 。有向直线 L 上的每个点 xi 可以看作客户,其服务需求量为w(xi ) 。每条边(xi , xi-1 ) 的边长d(xi , xi-1 ) 可以看作运输费用。如果在点 xi 处未设置服务机构,则将点 xi 处的服务需求沿有向边转移到点 x j处服务机构需付出的服务转移费用为w(xi ) * d(xi , x j ) 。在点 x0 处已设置了服务机构,现在要在直线 L 上增设 2 处服务机构,使得整体服务转移费用最小。

«算法设计:

对于给定的有向直线 L,计算在直线 L 上增设 2 处服务机构的最小服务转移费用。

Input

由文件 input.txt 给出输入数据。第 1 行有 1 个正整数 n,表示有向直线 L 上除了点 x0 外还有 n 个点 x0 < x1 <…< xn 。接下来的 n 行中,每行有 2 个整数。第 i+1 行的 2 个整数分别表示w(xn-i-1 ) 和d(xn-i-1 , xn-i-2 )
Output
将计算的最小服务转移费用输出
Sample Input
9
1 2
2 1
3 3
1 1
3 2
1 6
2 1
1 2
1 1
Sample Output
26
HINT


N: 不容易系列2

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 120 Solved: 86
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!
做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样。
话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的。比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情。如果套用一句经典的评语,我们可以这样总结:一个人做错一道选择题并不难,难的是全部做错,一个不对。
不幸的是,这种小概率事件又发生了,而且就在我们身边:
事情是这样的――HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟!
现在的问题是:请大家帮可怜的8006同学计算一下,一共有多少种可能的错误方式呢?

Input

输入数据包含多个多个测试实例,每个测试实例占用一行,每行包含一个正整数n(2<n<=20),n表示8006的网友的人数。

Output

对于每行输入请输出可能的错误方式的数量,每个实例的输出占用一行。

Sample Input

2
3

Sample Output

1
2

HINT

装错信封问题
这个问题是由 18 世纪初的法国数学家蒙摩提出来的。
某人给五个朋友写信,邀请他们来家中聚会。请柬和信封交由助手去处理。粗心的助手却把请柬全装错了信封。请问:助手会有多少种装错的可能呢?

import java.util.PriorityQueue;import java.util.Scanner;/** * Created by c on 2017/8/3. */public class Main {    public static int n,t;    public static int[] out,vis;    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while(scanner.hasNext()) {            n = scanner.nextInt();            t = 0;            vis = new int[n];            out = new int[n];            dfs(0);            System.out.println(t);        }    }    private static void dfs(int step) {        if(step >= n){            t++;            return;        }        for(int i = 0; i < n; i++){            if (vis[i] == 0 &&step != i) {                vis[i] = 1;                out[step] = i;                dfs(step + 1);                vis[i] = 0;            }        }    }}

O: 买不到的数目

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 81 Solved: 61
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

历届试题 买不到的数目
时间限制:1.0s 内存限制:256.0MB

问题描述

小明开了一家糖果店。他别出心裁:把水果糖包成4颗一包和7颗一包的两种。糖果不能拆包卖。
小朋友来买糖的时候,他就用这两种包装来组合。当然有些糖果数目是无法组合出来的,比如要买 10 颗糖。
你可以用计算机测试一下,在这种包装情况下,最大不能买到的数量是17。大于17的任何数字都可以用4和7组合出来。
本题的要求就是在已知两个包装的数量时,求最大不能组合出的数字。

输入格式

两个正整数,表示每种包装中糖的颗数(都不多于1000)

输出格式

一个正整数,表示最大不能买到的糖数

样例输入1

4 7

样例输出1

17

样例输入2

3 5

样例输出2

7

#include <iostream>using namespace std;int main(){    int a,b;    cin>>a>>b;    cout<<a*b-a-b<<endl;}

P: 人见人爱A+B

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 554 Solved: 232
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

AOJ面已经有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

1
1 2 3 4 5 6

Sample Output

5 7 9

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        int N = cin.nextInt();        while (N-- > 0) {            int ah = cin.nextInt();            int am = cin.nextInt();            int as = cin.nextInt();            int bh = cin.nextInt();            int bm = cin.nextInt();            int bs = cin.nextInt();            int carry = 0;            int cs = (as + bs + carry);            carry = cs / 60;            cs %= 60;            int cm = (am + bm + carry);            carry = cm / 60;            cm %= 60;            int ch = (ah + bh + carry);            System.out.println(String.format("%d %d %d", ch, cm, cs));        }        cin.close();    }}

Q: 计算:T(m)=1−122−132−⋅⋅⋅−1m2

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 288 Solved: 222
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

给定m根据以下公式计算T(m):
T(m)=1−122−132−⋅⋅⋅−1m2

Input

整型变量m

Output

计算T(m)(保留六位小数)

Sample Input

10

Sample Output

0.450232

import java.util.*;public class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        int m = cin.nextInt();        double t = 1;        if (m == 1)            System.out.printf("%.6f", 1.0);        else {            for (double i = 2; i <= m; i++) {                t -= (1 / (i * i));            }            System.out.printf("%.6f\n", t);        }        cin.close();    }}

R: 算法实现题 3-17 最小 m 段和问题

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 7 Solved: 2
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

给定 n 个整数组成的序列,现在要求将序列分割为 m 段,每段子序列中的数在原序列中连续排列。如何分割才能使这 m 段子序列的和的最大值达到最小?
算法设计:
给定 n 个整数组成的序列,计算该序列的最优 m 段分割,使 m 段子序列的和的最大值达到最小。

Input

输入的第 1 行中有 2 个正整数 n 和 m。正整数 n 是序列的长度;正整数 m 是分割的段数。
接下来的一行中有 n 个整数。

Output

输出的第 1 行中的数是计算出的 m 段子序列的和
的最大值的最小值。

Sample Input

1 1
10

Sample Output

10

HINT


S: 近似计算

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 157 Solved: 99
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

计算pi/4=1-1/3+1/5-1/7+….+1/n,

Input

n

Output

根据该算式计算的pi的值(精确6位有效数字)

Sample Input

100
10000
1

Sample Output

3.121595
3.141393
4.000000

HINT

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();            double sum=1,j=-1.0;            for(int i=3;i<=n;i+=2){                  sum=sum+j/i;                  j=-j;            }            System.out.printf("%.6f\n",sum*4);        }        sc.close();    }}

T: Friends

Time Limit: 1 Sec Memory Limit: 1280 MB
Submit: 12 Solved: 8
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

In a country, the relationship between people can be indicated by a tree. If two people are acquainted with each other, there will be an edge between them. If a person can get in touch with another through no more than five people, we should consider these two people can become friends. Now, give you a tree of N people’s relationship. ( 1 <= N <= 1e5), you should compute the number of who can become friends of each people?

Input

In the first line, there is an integer T, indicates the number of the cases.
For each case, there is an integer N indicates the number of people in the first line.
In the next N-1 lines, there are two integers u and v, indicate the people u and the people
v are acquainted with each other directly. People labels from 1.

Output

For each case, the first line is “Case #k :”, k indicates the case number.
In the next N lines, there is an integer in each line, indicates the number of people who can become the ith person’s friends. i is from 1 to n.

Sample Input

1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8

Sample Output

Case #1:
6
7
7
7
7
7
7
6

HINT


U: 最多约数问题

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 89 Solved: 10
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

正整数x 的约数是能整除x 的正整数。正整数x 的约数个数记为div(x)。例如,1,2,
5,10 都是正整数10 的约数,且div(10)=4。设a 和b 是2 个正整数,a≤b,找出区间【a,b】
之间约数个数最多的数x

Input

输入2 个正整数a≤b,编程找出区间【a,b】
之间约数个数最多的数x

Output

程序运行结束时,找到【a ,b】 之间约数个数最多的数是x,将div(x)输出

Sample Input

1 36

Sample Output

9

HINT

import java.util.Scanner;/** * Created by Administrator on 2016/10/7. */public class Main {    public static final int MAXP = 31622;    public static final int PCOUNT = 3401;    private static int max, numb, l, u;    private static int[] prim;    public static void main(String[] args) {        Scanner keyboard = new Scanner(System.in);        primes();        l = keyboard.nextInt();        u = keyboard.nextInt();        if (l == 1 && u == 1) {            max = 1;            numb = 1;        } else {            max = 2;            numb = 1;            search(1, 1, 1, l, u);        }        System.out.println(max);    }    public static void primes() {        prim = new int[PCOUNT + 1];        boolean[] get = new boolean[MAXP + 1];        for (int i = 0; i < PCOUNT; i++) prim[i] = 0;        for (int i = 2; i <= MAXP; i++) get[i] = true;        for (int i = 2; i <= MAXP; i++)            if (get[i])                for (int j = 2 * i; j <= MAXP; j += i)                    get[j] = false;        for (int i = 2, j = 0; i <= MAXP; i++)            if (get[i])                prim[++j] = i;    }    public static void search(int from, int tot, int num, int low, int up) {        if (num >= 1)            if ((tot > max) || (tot == max) && (num < numb)) {                max = tot;                numb = num;            }        if ((low == up) && (low > num)) search(from, tot * 2, num * low, 1, 1);        for (int i = from; i <= PCOUNT; i++)            if (prim[i] > up)                return;            else {                int j = prim[i], x = low - 1, y = up, n = num, t = tot, m = 1;                while (true) {                    m++;                    t += tot;                    x /= j;                    y /= j;                    if (x == y)                        break;                    n *= j;                    search(i + 1, t, n, x + 1, y);                }                m = 1 << m;                if (tot < max / n) return;            }    }}

V: 数列有序

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 213 Solved: 98
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4

HINT

import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String[] args) {    Scanner cin=new Scanner(System.in);    while(cin.hasNext()){        int n=cin.nextInt(),                m=cin.nextInt();        if(n==0&&m==0)break;        int[] b=new int[n+1];        for (int i = 0; i <n; i++)             b[i]=cin.nextInt();             b[n]=m;            Arrays.sort(b);            for (int i = 0; i < b.length-1; i++)             System.out.print(b[i]+" ");            System.out.println(b[b.length-1]);      }    cin.close();}}

W: 算法实现题 3-16 最大 k 乘积问题(习题 5-24)

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 6 Solved: 4
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

设 I 是一个 n 位十进制整数。如果将 I 划分为 k 段,则可得到 k 个整数。这 k 个整数的乘积称为 I 的一个 k 乘积。试设计一个算法,对于给定的 I 和 k,求出 I 的最大 k 乘积。
算法设计:
对于给定的 I 和 k,计算 I 的最大 k 乘积。

Input

输入的第 1 行中有 2 个正整数 n 和 k。正整数 n 是序列的长度;正整数 k 是分割的段数。
接下来的一行中是一个 n 位十进制整数。(n<=10)

Output

输出的第 1 行中的数是计算出的最大 k 乘积。

Sample Input

2 1
15

Sample Output

15

HINT


X: 断案

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 172 Solved: 121
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

算法提高 断案
时间限制:1.0s 内存限制:512.0MB

问题描述

公安人员审问甲、乙、丙、丁四个嫌疑犯,已确知,这四个人当中仅有一人是偷窃者,还知道这四个人的答话,要么完全诚实,要么完全说谎。在回答公安人员的问话中:
  甲说:“乙没有偷,是丁偷的。”
  乙说:“我没有偷,是丙偷的。”
  丙说:“甲没有偷,是乙偷的。”
  丁说:“我没有偷,我用的那东西是我家里的。”
  请根据上述四人答话,判断谁是偷窃者。
  输入格式:无输入。
  输出格式:输出一个字符,表示偷窃者是谁,A表示甲,B表示乙,C表示丙,D表示丁。

Input

Output

Sample Input

Sample Output

HINT

public class Main {    public static void main(String[] args) {        System.out.println("B");    }}

Y: 剔除相关数

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 43 Solved: 25
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

一个数与另一个数如果含有相同数字和个数的字符,则称两数相关。现有一堆乱七八糟的整数,里面可能充满了彼此相关的数,请你用一下手段,自动地将其剔除。

Input

每组数据前有一个N(<1000),表示跟随的整数P(0

Output

按从小到大的顺序输出非相关数,若没有非相关数,则输出None

Sample Input

8
213 667 3 213 43 34 677 2
3
322 232 232
0

Sample Output

2 3 667 677
None

HINT

import java.util.Arrays;import java.util.Scanner;public class Main{    static Scanner cin = new Scanner(System.in);    static int sort(int n) {        String s = String.valueOf(n);        StringBuffer b = new StringBuffer();        char[] a = new char[s.trim().length()];        a = s.toCharArray();        Arrays.sort(a);        for (int i = 0; i < a.length; i++)            b.append(a[i]);        return Integer.valueOf(b.toString());    }    public static void main(String[] args) {        while (cin.hasNext()) {            int n = cin.nextInt();            if (n == 0)                break;            int[] A = new int[n];            int[] B = new int[n];            int[] C = new int[n];            for (int i = 0; i < n; i++) {                C[i] = cin.nextInt();                if (C[i] < 10)                    A[i] = C[i];                else                    A[i] = sort(C[i]);            }            Arrays.sort(C);            Arrays.sort(A);            int flag = A[0];            boolean f = false;            int k = 0;            int temp = 0;            for (int i = 1; i < A.length; i++) {                if (flag != A[i]) {                    temp = flag;                    if (i == 1 || temp != A[i - 2]) {                        B[k] = temp;                        k++;                    }                    f = true;                }                flag = A[i];            }            if (f) {                if (A[A.length - 2] != A[A.length - 1])                    B[k] = A[A.length - 1];            } else {                System.out.println("None");            }            for (int i = 0; i < C.length; i++) {                for (int j = 0; j < B.length; j++) {                    if (B[j] == sort(C[i])) {                        if (j == B.length-1||i== C.length-1)                            System.out.println(C[i]);                        else                            System.out.print(C[i] + " ");                    }                }            }        }        cin.close();    }}

Z: 计树

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 45 Solved: 30
上一题SubmitStatus标签打分编辑题目信息编辑测试数据下一题
Description

Gondar设计了一种二叉树型数据结构,这种数据结构通过一种概率算法进行维护。为了准确计算该数据结构的运行时间期望,Gondar希望写一个程序计算N个点的不同形态的对称二叉树一共有多少种。
对称二叉树是指以根节点作镜面对称的二叉树(N=5时有两个对称二叉树,如下图)。

Input

多组测试数据,每组一个正整数N(1 <= N<= 100000)表示二叉树的节点个数

Output

每组测试数据输出对称二叉树的个数 MOD 10007。

Sample Input

2
3
5

Sample Output

0
1
2

HINT

卡特兰数暴力打表

Source

ACM/ICPC Anhui Program Contest 1st

#include"stdio.h"void main(){    int n,i,k,sum;    while(scanf("%d",&n)!=EOF)    {        if(n<3 || n%2==0)            printf("0\n");        else        {            sum=1;            k=(n-3)/2;            for(i=0;i<k;i++)                sum*=2;            printf("%d\n",sum%10007);        }    }}
阅读全文
3 0
原创粉丝点击