第三届程序设计知识竞赛网络赛

A字符串扩展

题目描述

Tom有些时候为了记录的方便,常常将一些连续的字符用扩展符'-'简单表示。比如abcdefg可以简写为a-g,即用起始的字符和终止字符中间加上一个扩展符'-'来表示这个字符串。但是为了处理的方便,Tom又必须将这些我们简单记法扩展成原来的字符串。很明显要是人工来做的话必定很麻烦,Tom知道计算机可以帮助他完成这个任务,但是他却不会编程,这的确让他很上火。他知道今天是山东理工大学第三届ACM校赛的日子,届时来自全校的编程爱好者都会来参加比赛,他很兴奋,因为这个困惑他良久的问题终于要被解决了。给你一个含有扩展符'-'的字符串,你的任务就是将他还原成原来的字符串。要求是只处理[a-z][A-Z][0-9]范围内的字符扩展,即只有当扩展符前后的字符同时是小写字母、大写字母或数字时并且扩展符前面的字符不大于后面的字符才进行扩展,其它情况不进行扩展,原样输出。例如:a-RD-e0-b4-B等字符串都不进行扩展。

输入

第一行是一个正整数T,表示共有T组测试数据(T < 100)下面的T行,每一行包括一个长度不大于1000的待扩展字符串.

输出

每组测试数据输出一行扩展后的字符串。

示例输入

3
ADEa-g-m02
acm-0-5-a-ac-cm-m-A-AC-CM-M
Welcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-1-3-A-z-a-Z

示例输出

ADEabcdefghijklm02
acm-012345-aaccmm-AACCMM
Welcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-123-A-z-a-Z
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 1010
 4 int main()
 5 {
 6     int T,i,j,len;
 7     char str[N];
 8     scanf("%d\n",&T);
 9     while(T--)
10     {
11         gets(str);
12         len=strlen(str);
13         for(i=0;i<len;i++)
14         {
15             if(str[i]!='-')
16                 printf("%c",str[i]);
17             else if((str[i-1]>='0'&&str[i-1]<='9'&&str[i+1]<='9'&&str[i+1]>='0'&&str[i-1]<=str[i+1])||(str[i-1]>='A'&&str[i-1]<='Z'&&str[i+1]<='Z'&&str[i+1]>='A'&&str[i-1]<=str[i+1])||(str[i-1]>='a'&&str[i-1]<='z'&&str[i+1]>='a'&&str[i+1]<='z'&&str[i-1]<=str[i+1]))
18 
19                     {
20                         for(j=str[i-1]+1;j<str[i+1];j++)
21                         printf("%c",j);;
22                     }
23 
24                     else
25                         printf("%c",str[i]);
26                     j++;
27             }
28             printf("\n");
29         }
30      return 0;
31 
32     }

B飞行棋

题目描述

飞行棋是在一个长度为n的棋盘上走动棋子的游戏。游戏开始时有一个棋子在棋盘的开始,位置是1。然后每一步玩家掷一次骰子,并将棋子往前跳骰子正面大小个格子。
当棋子跳出飞行棋的棋盘时游戏结束。问游戏结束时玩游戏的人掷骰子次数的期望。

输入

 第一行输入一个数T代表测试用例组数(T<=200),接下来T组测试用例,每组测试数据为棋盘大小。

输出

对于每个棋盘,输出玩家要掷骰子次数的期望(结果保留到小数点后4位)。每行输出一个结果。

示例输入

2
1
2

示例输出

1.0000
1.1667









 1 #include<stdio.h>
 2 int main()
 3 {
 4     int T,n,i;
 5     double a[1010];
 6     a[1]=1;
 7     a[2]=1.0/6*a[1]+1;
 8     a[3]=1.0/6*(a[1]+a[2])+1;
 9     a[4]=1.0/6*(a[1]+a[2]+a[3])+1;
10     a[5]=1.0/6*(a[1]+a[2]+a[3]+a[4])+1;
11     a[6]=1.0/6*(a[1]+a[2]+a[3]+a[4]+a[5])+1;
12     for(i=7; i<=1000; i++)
13         a[i]=1.0/6*(a[i-6]+a[i-5]+a[i-4]+a[i-3]+a[i-2]+a[i-1])+1;
14     scanf("%d",&T);
15     while(T--)
16     {
17         scanf("%d",&n);
18         printf("%.4lf\n",a[n]);
19     }
20     return 0;
21 }
View Code

C  0\s

题目描述

计算整数n!(n的阶乘)末尾有多少个0。

输入

 第一行输入一个数T代表测试数据个数(T<=20)。接下来T行每行1个数代表n(0<=n< 2^31)。

输出

对于每个测试数据输n!末尾有多少个0,每行输出一个结果。

示例输入

3
1
5
10

示例输出

0
1
2








 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,t,x,m;
 5     scanf("%d",&m);
 6     while(m--)
 7     {
 8         scanf("%d",&n);
 9         t=0,x=5;
10         while(x<=n)
11         {
12             t+=n/x;
13             x*=5;
14         }
15         printf("%d\n",t);
16     }
17     return 0;
18 }
View Code

D the?1?2?...?n=k problem

题目描述

 

Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k

For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12
with n = 7

输入

 

 

The first line is the number of test cases, followed by a blank line.

 

Each test case of the input contains integer k (0<=|k|<=1000000000).

Each test case will be separated by a single line.

输出

 

For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.

Print a blank line between the outputs for two consecutive test cases.

示例输入

2

12

-3646397

示例输出

7

2701






#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    long long k,t;
    int T,i,y;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&k);
        k=abs(k);
        for(i=1;;i++)
        {
            t=i*(i+1)/2;
            if(t>=k) break;
        }
        y=i;
        while(1)
        {
           int x=y*(y+1)/2-k;
           if(x%2==0) break;
           y++;
        }
        printf("%d\n",y);
        if(T)
            printf("\n");
    }
    return 0;
}
View Code

E super prime

题目描述

We all know, prime is a kind of special number which has no other factors except of 1 and itself.
2,3,5,7,11,13,17,19,23,29 are the top 20 primes.
Now there is a new question about prime:
We call a prime as super prime when and only when which can be represented as the sum of multiple continuous primes. For example: 5=2+3, so 5 is a super prime.
Please program to judge whether a number is a super prime or not.

输入

The first line is an integer T (T<=1000), and then T lines follow.
There is only one positive integer 
N(1

输出

For each case you should output the case number first, and then the word "yes" if the integer N is a super prime, and you should output "no" otherwise.

示例输入

3
5
7
8

示例输出

Case 1: yes
Case 2: no
Case 3: no
#include<stdio.h>
#include<string.h>
#define N 100010
int a[N];
int p[N];
void f()
{
    int i,j;
    for(i=0;i<N;i++)
        a[i]=0;
    for(i=2;i<N;i++)
    {
        if(a[i]==0)
        {
            for(j=i+i;j<=N;j+=i)
                a[j]=1;
        }
    }
    for(i=2,j=0;i<N;i++)
    {
        if(a[i]==0)
            {p[j]=i;j++;}
    }
}
int main()
{
    f();
    int m,i,x,leag;
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        leag=1;
        int k=1,y=0,t=p[0];
        scanf("%d",&x);
        printf("Case %d: ",i);
        if(a[x]==1)
            {printf("no\n");continue;}
       while(1)
       {
           if(k==y)
           {
               leag=0;break;
           }
           else if(t>x)
           {
               t-=p[y];
               y++;
           }
           else if(t<x)
           {
               t+=p[k];
               k++;
           }
           else if(t==x)
           {
               break;
           }
       }
       if(leag&&k!=y+1)
        printf("yes\n");
       else
        printf("no\n");
    }
    return 0;
}
View Code

F  分类游戏

题目描述

分类游戏很适合于对儿童的教育,可以让他们通过游戏了解更多的事物,学习更多的知识。我们要实现的分类游戏很简单,是基于单词的分类游戏。我们给出两个或三个类别,比如说单词的首字母,有以B开头的字母和以C开头的字母两类,也可能有三类。然后给出若干个图片代表这两种分类里面的事物,这样孩子可以把下面的物品拖到对应首字母的篮子里,对了加分,错了减分。
作为写程序的人怎么可以忍受,于是你决定写一个外挂,瞬间秒杀,直接满分。假设你已经获得了数据,虽然有时候这是最难的部分,但今天我们只考虑外挂要实现的内容。数据包含了不同类别的首字母,和一些物品的英文单词(呵呵,这可比图片好多了)。
外挂的任务是根据类别的首字母,将物品的英文单词分类,并分别输出结果。

输入

输入数据有多组。
每组数据的第一行是两个正整数C(2<=C<=5),N(1<=N<=100)分别代表类别的个数和单词(物品名称)的个数。接下来一行有C个大写字母,代表类别的首字母。接下来N行,每行一个英文单词,代表具体的物品名称,单词长度不超过20。
注意有可能给出的单词不属于C个类别中的任何一个。

输出

对于每组输入有一组输出。每组输出按照物品类别给出的顺序有C行。每行给出对应的单词,如果该类别没有则不输出,有多个的话用空格隔开。每组输出后面输出一个空行。

示例输入

2 5
B C
Bag
Cat
boy
Boss
case
3 3
B C D
Bomb
dog
Donkey

示例输出

Bag boy Boss
Cat case

Bomb
dog Donkey
#include<stdio.h>
#include<string.h>
#define N 110
int main()
{
    int n,m,i,j,len,t,k;
    char st1[N][N],st2[N][N];
    while(~scanf("%d %d%*c",&m,&n))
    {
        t=0;k=0;
        for(i=0;i<m;i++)
            scanf("%s",st1[i]);
        for(i=0;i<n;i++)
            scanf("%s%*c",st2[i]);
        for(i=0;i<m;i++)
        {
            k=0;t=0;
            for(j=0;j<n;j++)
            {
                if(st1[i][0]==st2[j][0]||st1[i][0]==st2[j][0]-32)
                {
                    k=1;
                    if(t==0)
                        printf("%s",st2[j]);
                    else printf(" %s",st2[j]);
                    t++;
                }
            }
            if(k==1)
                printf("\n");
        }
        printf("\n");
    }
    return 0;
}
View Code

G图的深度遍历

题目描述

请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出。遍历时,先遍历节点编号小的。

输入

输入第一行为整数n(0 < n < 100),表示数据的组数。 对于每组数据,第一行是两个整数k,m(0 < k < 100,0 < m < k*k),表示有m条边,k个顶点。 下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

输出

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示DFS的遍历结果。

示例输入

1
4 4
0 1
0 2
0 3
2 3

示例输出

0 1 2 3



#include<stdio.h>
#include<string.h>
int g[110][110];
int w[110];
int x=1;
int m;
void DFS(int g[110][110],int u)
{
    if(x==1)
    {
        printf("%d",u);
        x=0;
    }
    else
    printf(" %d",u);
    w[u]=1;
    for(int i=0;i<=m;i++)
    {
        if(!w[i]&&g[u][i]==1)
        {
            DFS(g,i);
        }
    }
}
int main()
{
    int t,k,u,v;
    scanf("%d",&t);
    while(t--)
    {
        memset(w,0,sizeof(w));
        memset(g,0,sizeof(g));
        scanf("%d %d",&k,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&u,&v);
            g[u][v]=g[v][u]=1;
        }
        DFS(g,0);
        printf("\n");
        x=1;
    }
    return 0;
}
View Code

H  DOTA-人王之战

题目描述

 现在Dota盛行,相信很多同学有过通宵打Dota的经历,大宝也不例外。话说大宝是远近闻名的Dota大神,据传是人皇Sky的徒弟,自号人王,自出道以来未尝一败。当然,大宝的名声为他招来众多挑战者,但都被大宝虐的掩面而逃。这一天,大宝迎来了一个特殊的挑战者,进行了一场特殊的游戏,会不会有特殊的结局呢?。。。。
挑战者提出游戏规则:两人任选一个英雄去摧毁敌方的防护塔。假设有n个防护塔,二人轮流出击,每次可以摧毁至多m个防护塔,至少1个(在摧毁[1, m]个防护塔时英雄是无敌的,但是超过的话会被立刻干掉!)。当一个人没有防护塔可以摧毁是他就输了。
 
当然,以大宝的名声肯定会让挑战者先开始。你觉得今天大宝会不会保持不败的神话呢?
 

输入

 多组数据,处理到文件结尾。每组数据有两个值,n和m。(0 < m < n, n <= 2147483647)

输出

 如果大宝可以继续他不败的神话,输出"Orz Dota God",否则输出"A new star rise".

示例输入

5 3
2 1

示例输出

A new star rise
Orz Dota God


#include<stdio.h>
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        if(n%(m+1))
            printf("A new star rise\n");
        else
            printf("Orz Dota God\n");
    }
    return 0;
}

博弈题
View Code

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1917&cid=1166

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main()
 4 {
 5     int i,j,t,p,q;
 6     int ra,rb,ca,cb;
 7     int mat1[22][22],mat2[22][22];
 8     scanf("%d",&t);
 9     while(t--)
10     {
11         scanf("%d %d",&ra,&ca);
12         if(ra<1||ra>20||ca<1||ca>20) break;
13         
14         for(i=0;i<ra;i++)
15             for(j=0;j<ca;j++)
16                 scanf("%d",&mat1[i][j]);
17                 
18         scanf("%d %d",&rb,&cb);
19         if(rb<1||rb>20||cb<1||cb>20) break;
20         
21         for(i=0;i<rb;i++)
22             for(j=0;j<cb;j++)
23                 scanf("%d",&mat2[i][j]);
24                 
25         scanf("%d %d",&p,&q);
26         if(p<=0||p>ra||q<=0||q>ca) break;
27         
28         for(i=p-1;i<ra;i++)
29         {
30             for(j=q-1;j<ca;j++)
31             {
32                 if((i+1-p)<rb&&(j+1-q)<cb)
33                 {
34                     mat1[i][j]=mat2[i+1-p][j+1-q];
35                 }
36             }
37         }
38         for(i=0;i<ra;i++)
39         {
40             for(j=0;j<ca;j++)
41             {
42                 if(j==(ca-1)) 
43                     printf("%d",mat1[i][j]);
44                 else
45                  printf("%d ",mat1[i][j]);
46             }
47             printf("\n");
48         }
49         printf("\n");
50     }
51     return 0;
52 }
View Code

J又见回文

题目描述

    “回文串”是一个正读和反读都一样的字符串,比如“level或者“

noon等等就是回文串。现在呢,就是让你判断输入的字符串是否是回文串。

输入

    有多组输入,每行输入一串字符,保证字符串长度不会大于 100000,字符串由大小写英文字母和空格组成,以字符串“2013作为结束标志。

输出

    每行输出一个字符串,如果输入是回文串,输出“YES,否则输出“NO(注意:判断的时候空格是不作判断的,详见样例)。

示例输入

aaaa
ggg g
lozxvxoMJBCHsTXooXTsHCBJMoxvxzol
i am a good acmer
2013

示例输出

YES
YES
YES
NO
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 100010
 4 int main()
 5 {
 6     int i,j,len,n,t;
 7     char str[N],a[N];
 8     while(gets(str)!=NULL)
 9     {
10         if(strcmp(str,"2013")==0)break;
11         else{
12             n=0;t=0;
13             len=strlen(str);
14             j=0;
15             for(i=0;i<len;i++)
16             {
17                 if(str[i]!=' ')
18                 {
19                     a[j]=str[i];
20                     j++;n++;
21                 }
22             }
23         for(i=0;i<n/2;i++)
24         {
25             if(a[i]!=a[n-1-i])
26             {
27                 t=1;
28                 break;
29             }
30         }
31         if(t==0)
32             printf("YES\n");
33         else
34             printf("NO\n");
35     }
36     }
37     return 0;
38 }
View Code
原文地址:https://www.cnblogs.com/sdutmyj/p/3100226.html