Contest1063 - 2017广东工业大学第一次月赛-部分题解

Problem A: Chiruno

Description

五年前,Aerix 无意间飞到了幻想乡,然后遇到了传说中的⑨酱,心情非常激动,想和她合影留念,但是⑨酱比较傲娇,她只欣赏算数能力强的,也不随随便便和人合影的,于是她手握冰锥,问 Aerix 这样一个问题:“我现在写一个很长很长的数字,设它为 A, 你告诉我 ⑨ 的 A 次方是多少,当然我不想听你说一长串数字,你告诉我从右往左数第二位的数字是多少就行。” Aerix 一看这么简单的问题,怎么用他出手,然后就把问题丢给你了。

Input

第一行:T,表示这里有 T 组数据
接下来 T 行:每行一个 <=100000 位的数 A

Output

共 T 行:每行一个数,表示对这组数据的解。

Sample Input

4 0 1 2 999999999999999999999999

Sample Output

0 0 8 8
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<string.h>
#include<math.h>
using namespace std;
#define N 110108
#define ll long long
char str[N];
int ans[10]={0,0,8,2,6,4,4,6,2,8};
int main()
{
    int T,j,len;
    scanf("%d",&T);
     while(T--) //从右往左!!!
    {
        scanf("%s",str);
        len=strlen(str);
       // i-str[len-2]-'0';
        j=str[len-1]-'0';
       // s=i*10+j;
      printf("%d\n",ans[j]);
    }

    return 0;
}
View Code

 

这题难就难在找规律,一是可以手推;二是可以用写程序进行找规律,如下:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long
#define mod 100
int main()
{
    int i,n;
       ll k=1;
        for(i=1;i<=180 ;i++){
            k=k*9%mod;
            printf("%d %02lld\n",i,k);
            if(i%10==9)printf("\n");
        }


    return 0;
}
View Code

运行结果截图为:

Problem G: 狗哥的选择困难症

Description

狗哥是一个举棋不定又雷厉风行的人,有一天他遇到了一个巨大的困难,他走到了一个水果摊的前面,水果摊有n种芭娜娜,每种的芭娜娜都只卖剩下1个,老板看出了他的小心思,便说:“你可以随意挑选不超过n个的水果,但是不允许不拿。”狗哥眉头一皱,发现事情并不简单,于是掏出随身携带的天河二号计算机,计算起了有多少种的挑选方案。机智的你一定能比狗哥提前算出来的是吧。

Input

多组样例,第一行一个数字T,代表样例的个数(t<100)

每个样例有一个数字n(0=<n<=62),代表摊位上芭娜娜的种类

Output

对于每个样例输出一个m,代表方案数

Sample Input

1 2

Sample Output

 

主要应用一个组合数的公式即可:

 题意要求是所有的选择方法,及偶数项加上奇数项之和构成2的m次方。由于Cm0项取不到(不允许取到0个),故减1.

 【这篇博客详细讲解了一些常用的组合数公式,相关知识点可以记到本子上。 http://m.blog.csdn.net/litble/article/details/75913032】

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long

int main()
{
    int T;
    double i,n;
    scanf("%d",&T);
    while(T--){
        scanf("%lf",&n);
        printf("%.0lf\n",pow(2.0,n)-1.0+1e-8);
    }


    return 0;
}
View Code

而我一开始没想到这个公式,用DP推了出来:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<string.h>
#include<math.h>
using namespace std;
#define N 110108
#define ll long long

ll dp[100][100],ans[100]; // dp[n][m]表示n个物体中选择m个的组合数
int main()
{
    int T;ll i,j;
    scanf("%d",&T);
    memset(dp,0,sizeof(dp));
    memset(ans,0,sizeof(ans));
    for(i=1;i<=62;i++)
    {
        for(j=1;j<=i;j++)
        {
            if(j==1)
                dp[i][j]=i;
            else
                dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
            ans[i]+=dp[i][j];
        }
    }
    while(T--)
    {
        scanf("%lld",&j);
        printf("%lld\n",ans[j]);
    }

    return 0;
}
View Code

了解DP的同学,可以画个表格自己手推一下!不会就跳过。

Problem H: 找hTh

Description

国庆期间,hTh同学在家好无聊,就玩起自己最喜欢的游戏“找你妹”,对于眼疾手快的他这个好简单了。but,突然hTh收到队长cYz发给他一串长长的字符串,问hTh,请问里面有多少个“你”?聪明的hTh一下子就知道了,不就是要找“hTh”的个数嘛,而且hTh知道,“hThTh”答案是肯定是2不是1,不过,这个字符串好长啊!!所以hTh懒得去算,他向你求助,你能帮帮他吗?

Input

第一行一个整数T,(0<T<=10)
接下来每一行是一个字符串(0<长度<=10000)

 

Output

输出答案个数。

 

Sample Input

2
hThTh
abcdhThTefghTh

Sample Output

2 2
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<string.h>
#include<math.h>
using namespace std;
#define N 100008
char str[10008];
int main()
{
    int i,T,j,cnt,len;
    scanf("%d\n",&T);
    while(T--)
    {
        cnt=0;
        gets(str);
        len=strlen(str);
        if(len<3)
            cnt=0;
        else
        {
            for(i=0;i<=len-3;i++)
            {
                if(str[i]=='h'&&str[i+1]=='T'&&str[i+2]=='h')
                    cnt++;
            }
        }
        printf("%d\n",cnt);
    }



    return 0;
}
View Code

Problem J: 牛蛙的叫声

Description

你训练完踩单车回宿舍路上听到牛蛙的蛙鸣声,有些好听有些不好听,蛙鸣声可以用一个正整数表示,你发现当该整数的质因子种类仅有两种时,蛙鸣声是好听的。
现在给你一个整数表示牛蛙的叫声,问该牛蛙的叫声是否好听。

 

Input

输入包含多组样例,每个样例输入一个正整数n (1<=n<=1e6),表示牛蛙的叫声

 

Output

对于每一组样例,输出“YES”表示牛蛙的声音是好听的,否则输出“NO”

 

Sample Input

6 10 13 16 40

Sample Output

YES YES NO NO YES
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<string.h>  //质因子(或质因数)在数论里是指能整除给定正整数的 质数
#include<math.h>
using namespace std;
#define N 1000100
#define M 100000
#define ll long long
int prime[N],cnt,p[M];  //p[]表示质因子表,cnt表示质因子的总个数
void getprime(){     //生成1--N的素数判定表,prime[i]=0?1 ;
    int i,j;
    cnt=0;
    for(i=0;i<=N-10;i++)
        prime[i]=1;
    prime[0]=prime[1]=0;
    prime[2]=1;
    for(i=2;i<=N;i++){
        if(prime[i]==1)
        {
           p[++cnt]=i;
            for(j=i+i;j<N;j+=i)
                prime[j]=0;
        }
    }
}
int cntprime(int n){  //pcnt表示当前课找到的质因子数
    int i,j,pcnt=0;
    for(i=1;i<=cnt;i++)
    {
        if(p[i]>n)
            break;
        if(n%p[i]==0)
            pcnt++;
        if(pcnt>2) //存在两个以上的质因子
            return 0;
    }
    if(pcnt==2) //存在两个质因子才是正解
        return 1;
    return 0;
}
void fact(int a[],int n){
    for(int i=1;i<=n;i++)
        printf(" %d:%6d ",i,p[i]);
}
int main()
{
    int n;
    getprime();
  //  fact(p,10);
    while(scanf("%d",&n)!=EOF)
    {
        if(prime[n]==1||n==1)  // 质数直接判定
            printf("NO\n");
        else
        {
            if(cntprime(n)==1)
                printf("YES\n");
            else
            printf("NO\n");
        }
    }

    return 0;
}
View Code

有人说其实这个题,可以加个奇数的判定条件——即直接忽略奇数!

应该不对,比如15的质因子就有两个:3和5,输入的是YES!

代码运行时间:52MS。比较省时!

Problem I: 一个撒狗粮的游戏

Description

今天讲的是一个撒狗粮的的游戏。
首先,给定四个数字分别是abcd, 其中0≤a, b, c, d≤9,现在我们拥有一个神技,可以消耗一个魔法值来修改这四个数字之中的任意一个(改完还是在0-9以内)。现在要使得a+b=c+d,那么最少要花费多少魔法值?(这个游戏怎么就是撒狗粮了?)

 

Input

第一行是样例个数T,代表T个样例
接下来T行,每行是4个整数abcd(没有空格隔开),每个整数都是0-9以内

Output

对于每一个样例都输出最小的花费魔法值

 

Sample Input

2 1188 1234

Sample Output

2 1

HINT

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long

int main()
{
    int i,j,k,m,n,T;
    int a,b,c,d;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%1d%1d%1d%1d",&a,&b,&c,&d);
        if(a+b==c+d)
        {
            printf("0\n");
        }
        else if(a+b>c+d)
        {
            if(a>=c+d&&b<=c+d || b>=c+d&&a<=c+d)
                printf("1\n");
            else
                printf("2\n");
        }
        else
        {
            if(c>=a+b&&d<=a+b || d>=a+b&&c<=a+b)
                printf("1\n");
            else
                printf("2\n");
        }
    }

    return 0;
}
View Code

没提交,目测没问题。。

———————————————————————————————我是分割线————————————————————————————————————————

题目怪难的,我花了一半比赛的时间也就勉强写了这几题。惭愧,惭愧。

没有多少耐心了,题解写的烂,程度好的同学可以看看,差一点的也不要灰心——多问问多交流,能补多少题补多少题。

虽然比赛难度大,但是也不应该气馁,应该迎难而上、多多努力,认识到自己的不足和与他人的差距,积累做题审题经验(很重要),打好心态,找到自己知识的漏洞,一点一点补起来。

 以后的比赛的机会不会很多了,各位多多努力,积极参与。

posted @ 2017-11-26 19:13  山枫叶纷飞  阅读(451)  评论(0编辑  收藏  举报