XYNU第二次比赛

XYNU第二周比赛题解

Sky数

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊。Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数。但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧。

Input

输入含有一些四位正整数,如果为0,则输入结束。
Output

若n为Sky数,则输出“#n is a Sky Number.”,否则输出“#n is not a Sky Number.”。每个结果占一行。注意:#n表示所读入的n值。

Sample Input

2992

1234

0

Sample Output

2992 is a Sky Number.

1234 is not a Sky Number.

#include<stdio.h>

int change(int n,int x){
int sum=0;

while(n){
    sum+=n%x;
    n/=x;
}
return sum;
}

int main(){
int n;
while(scanf("%d",&n)&&n){

int s1=    change(n,10);
int s2= change(n,16);    
int s3= change(n,12);

if(s1==s2&&s1==s3)
printf("%d is a Sky Number.\n",n);
else
printf("%d is not a Sky Number.\n",n);

}
}

Encoding

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:

  1. Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.

  2. If the length of the sub-string is 1, ‘1’ should be ignored.

Input

The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only ‘A’ - ‘Z’ and the length is less than 10000.

Output

For each test case, output the encoded string in a line.

Sample Input

2

ABC

ABBCCC

Sample Output

ABC

A2B3C

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char str[1000];
int main(){
int T;
cin>>T;

while(T--){
    cin>>str;
    int len=strlen(str);

    int t=1;
    for(int i=0;i<len;i++){
        while(str[i]==str[i+1]){
            i+=1;
            t++;
        }
        if(t!=1)
        {
            printf("%d%c",t,str[i]);
            t=1;
        }
        else{
            printf("%c",str[i]);
        }
    }
    printf("\n");
  }
}

分拆素数和

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

把一个偶数拆成两个不同素数的和,有几种拆法呢?

Input

输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。

Output

对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。

Sample Input

30

26

0

Sample Output

3

2

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

int IsPrime(int n)
{
int i;
for (i=2; i<=sqrt(n); i++)
 {
    if (n % i == 0)
    return 0;
 }
return 1;
}

int main()
{
int n, i, cnt;
while (scanf("%d", &n) && n)
{
    cnt = 0;
    for (i=3; i<n/2; i+=2)
    {//因为是不同的两个素数,那必定一个比n/2大,一个比n/2小
        if (IsPrime(i) && IsPrime(n-i))
            cnt++;
    }
    printf("%d\n", cnt);
 }
 return 0;
}

20岁生日

时间限制:1000 ms | 内存限制:65535 KB

描述

路过这的20岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达20岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达20岁生日所经过的总天数,让他好来比较一下。

输入

一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
输出T行,每行一个数,表示此人从出生到20岁生日所经过的天数。如果这个人没有20岁生日,就输出-1。

样例输入

1

1988-03-07

样例输出

7305

#include <stdio.h>  

int main()  
{  

int year,mon,day,sum1,sum2,sum3,t,i,flag1,flag2;  
scanf("%d",&t);  
while(t--)  
{  
    flag1=flag2=0;  
    int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//保存十二个月的天数,2月先用平年计算  
    scanf("%d-%d-%d",&year,&mon,&day);  
            sum1=sum2=sum3=0;  
    for(i=year+1;i<year+20;i++)  
    {  
        if(i%4==0&&i%100!=0||i%400==0)  
            sum1+=366;  
        else  
            sum1+=365;  
    }//计算出生下一年到20岁那年的天数  
    if(year%4==0&&year%100!=0||year%400==0)  
        a[2]=29,flag1=1;//如果出生那年为闰年2月要修改为29天,并且标记flag1=1;  
    for(i=mon+1;i<13;i++)  
        sum2=sum2+a[i];  
    sum2=sum2+a[mon]-day;//计算出生那年到下一年的天数  
    a[2]=28;  
    if((year+20)%4==0&&(year+20)%100!=0||(year+20)%400==0)  
        a[2]=29,flag2=1;//如果二十岁生日那年为闰年flag2=1;  
    for(i=1;i<mon;i++)  
        sum3=sum3+a[i];  
    if(flag1&&!flag2&&mon==2&&day==29)  
        printf("-1\n");//如果出生那年为闰年且为2月29日, 如果year+20不是闰年,那么这个人没有20岁生日。  
    else  
    printf("%d\n",sum1+sum2+sum3+day);  
 }  
 return 0;  
}          

Divisible number

时间限制: 1 Sec 内存限制: 128 MB

题目描述

1, 2, 3… … N this n (0 < n < = 1000000000) in the number of how many Numbers can be divided exactly by positive integer b

输入

Input contains multiple sets of data
Each group of data is a line, each row is given two positive integer n, b.

输出

Input contains multiple sets of data

Each group of data is a line, each row is given two positive integer n, b.

样例输入

2 1

5 3

10 4

样例输出

2

1

2

#include<stdio.h>
int main()
{
int  n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{

printf("%d\n",n/m);
}
return 0;
}

Quicksum

时间限制:3000 ms | 内存限制:65535 KB

描述

A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting transmission errors, validating document contents, and in many other situations where it is necessary to detect undesirable changes in data.

For this problem, you will implement a checksum algorithm called Quicksum. A Quicksum packet allows only uppercase letters and spaces. It always begins and ends with an uppercase letter. Otherwise, spaces and letters can occur in any combination, including consecutive spaces.

A Quicksum is the sum of the products of each character’s position in the packet times the character’s value. A space has a value of zero, while letters have a value equal to their position in the alphabet. So, A=1, B=2, etc., through Z=26. Here are example Quicksum calculations for the packets “ACM” and “MID CENTRAL”:

ACM: 11 + 23 + 3*13 = 46

MID CENTRAL: 113 + 29 + 34 + 40 + 53 + 65 + 714 + 820 +
918 + 101 + 1112 = 650

输入

The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 300 characters.

输出

For each packet, output its Quicksum on a separate line in the output.

样例输入

ACM

MID CENTRAL

REGIONAL PROGRAMMING CONTEST

ACN

A C M

ABC

BBC

#

*样例输出

46

650

4690

49

75

14

15

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char a[999];
while(gets(a))
{
    if(a[0]=='#')
        break;
    int sum=0,s;
   for(int i=0;i<strlen(a);i++)
   {
       if(a[i]==' ')
        s=0;
       else
       s=(a[i]-'A'+1);
       sum=(i+1)*s+sum;
   }
   printf("%d\n",sum);
 }
}    

Fibonacci Again!

时间限制:1000 ms | 内存限制:65535 KB

描述

求第n个斐波那契数是否是一个素数,n为整数
f[n]=f[n-1]+f[n-2] (2
输入

输入整数m,0
输出

如果f[m]是素数 则输出Yes,否则输出No,
每行输出占一行。

样例输入

2

3

样例输出

Yes

No

#include<stdio.h>
#include<ctype.h>
#include<math.h>`

int judge_prime(int n)
{
int i;
for(i=2;i<=sqrt(n);i++)
if(n%i==0) return 0;//不是素数 
return 1;//是素数 
}

int main()
{
int n;
int a[40];
a[1]=3;a[2]=7;
for(int i=3;i<30;i++)
{
a[i]=a[i-1]+a[i-2];
//printf("%d\n",a[i]);
}
while(scanf("%d",&n)!=EOF&&n!=-1)
{
if(judge_prime(a[n]))
printf("Yes\n");
else
printf("No\n");    
} 
return 0;
}

最少拦截系统

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input

输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)

Output

对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

特殊数据

输入:

8 17 16 15 16 13 12 14 12

输出:

2

#include<stdio.h>

int main(int argc,char* argv[]){

int n=0;
int a[10000];

while(scanf("%d",&n) != EOF){
  int h,k=0;
  for(int i =0;i<n;i++)
  scanf("%d",&a[i]);

  for(int i =0;i<n;i++){

  if(a[i] != -1){

    h =a[i];
  k++;    


    for(int j=i+1;j<n;j++)
        if(a[j] != -1&&h>=a[j])
        {

        h = a[j];
        a[j] =-1;
        }
   }

  }     
  printf("%d\n",k); 
  }    

 return 0;

} 
-------------Passage is endthanks for your reading.-------------