现在的位置: 首页 > 综合 > 正文

周赛 Newstar 解题

2018年10月29日 ⁄ 综合 ⁄ 共 8349字 ⁄ 字号 评论关闭

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 33   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。

Input

输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。

Output

对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。

Sample Input

1 3
2 5

Sample Output

4 28
20 152
题目中有一个小陷阱,输入的数m不一定小于n,所以要先判断。
#include<stdio.h>
#define swap(a,b) {a=a+b; b=a-b; a=a-b;}

int main()
{
    int x,y,i,ans1,ans2;
    while(scanf("%d %d",&x,&y)!=EOF)
    {
        ans1=0; ans2=0;
        if(x>y) swap(x,y);
        for(i=x;i<=y;i++)
        {
            if(i%2) ans2+=i*i*i;
            else ans1+=i*i;
        }
        printf("%d %d\n",ans1, ans2);
    }
    return 0;
}

Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。

Output

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

Sample Input

123
-234.00

Sample Output

123.00
234.00
有点坑,必须用double,float不过
#include<stdio.h>

int main()
{
    double n;
    while(scanf("%lf",&n)!=EOF)
    {
        if(n<0) printf("%.2lf\n",-n);
        else printf("%.2lf\n",n);
    }
    return 0;
}

Problem C

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

Input

输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。

Output

对于每组输入数据,输出一行,结果保留两位小数。

Sample Input

0 0 0 1
0 1 1 0

Sample Output

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

int main()
{
    double x1,x2,y1,y2,ans;
    while(scanf("%lf %lf %lf %lf",&x1, &y1, &x2, &y2)!=EOF)
    {
        ans=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        printf("%.2lf\n",ans);
    }
    return 0;
}

Problem D

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵^-^
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input

输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output

对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input

2
4

Sample Output

4
22
简单模拟,当第n天时只剩一个桃子,那么第n-1天剩(1+1)*2,第n-2天剩((1+1)*1+1)*2,以此类推...用循环即可求出原先桃子数,用递归也行但循环更快更省内存。
#include<stdio.h>

int main()
{
    int n,ans,i;
    while(scanf("%d",&n)!=EOF)
    {
        ans=1;
        for(i=1;i<n;i++)
        {
            ans=(ans+1)*2;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Problem E

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。

Input

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

Sample Input

100 120
300 380

Sample Output

no
370 371

循环判断

#include<stdio.h>

bool is_shui(int x)
{
    int sum=0,ans=x;
    while(x!=0)
    {
        sum+=(x%10)*(x%10)*(x%10);
        x/=10;
    }
    if(ans==sum) return true;
    else return false;
}

int main()
{
    int m,n,i,j;
    bool flag;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        flag=false;
        for(i=m;i<=n;i++)
        {
            if(is_shui(i) )
            {
                if(flag)
                {
                    printf(" ");
                    printf("%d",i);
                }
                else
                {
                    printf("%d",i);
                    flag=true;
                }
            }
        }
        if(flag) printf("\n");
        else printf("no\n");
    }
}

Problem F

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm: 

    1.      input n

    2.      print n

    3.      if n = 1 then STOP

    4.           if n is odd then n <- 3n + 1

    5.           else n <- n / 2

    6.      GOTO 2

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that
0 < n < 1,000,000 (and, in fact, for many more numbers than this.) 

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16. 

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j. 

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0. 

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j. 

You can assume that no opperation overflows a 32-bit integer.

Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output
for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line). 

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

简单模拟,按要求计算程序运行的次数,但是此题也有一个小陷阱,输入的i,j不一定升序,而且在输出时必选按如输入时的顺序输出

#include<stdio.h>
#define swap(a,b) {a=a+b; b=a-b; a=a-b;}

int whatstep(int num)
{
    int ans=1;
    while(num!=1)
    {
        if(num%2) num=3*num+1;
        else num/=2;
        ans++;
    }
    return ans;
}

int main()
{
    int n, m, i, j, max, step;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        max=0;
        if(n>m)
        {
            i=m; j=n;
        }
        else
        {
            i=n; j=m;
        }
        for(;i<=j;i++)
        {
            step=whatstep(i);
            max=max>step?max:step;
        }
        printf("%d %d %d\n",n,m,max);
    }
    return 0;
}

Problem G

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Descriptio

Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi
River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion. 

After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The
semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.) 

Input

The first line of input will be a positive integer indicating how many data sets will be included (N). 

Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.

Output

For each data set, a single line of output should appear. This line should take the form of: 

“Property N: This property will begin eroding in year Z.” 

Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer. 

After the last data set, this should print out “END OF OUTPUT.” 

Notes: 

1. No property will appear exactly on the semicircle boundary: it will either be inside or outside. 

2. This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines. 

3. All locations are given in miles.

Sample Input

2 
1.0 1.0 
25.0 0.0

Sample Output

Property 1: This property will begin eroding in year 1. 
Property 2: This property will begin eroding in year 20. 
END OF OUTPUT. 
可以使用向上取整函数 ceil()
#include<stdio.h>
#include<math.h>
#define Pi 3.1415926
int main()
{
    int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        double x,y,r,s;
        int year;
        scanf("%lf %lf",&x,&y);
        r=sqrt(x*x+y*y);
        s=Pi*r*r;
        year=ceil(s/2/50);
        printf("Property %d: This property will begin eroding in year %d.\n",
               i,year);
    }
    printf("END OF OUTPUT.\n");
    return 0;
}

Preblem H 是个大数,改天专门开一帖讲解。

Problem I

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. ^_^

Input

Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.

Output

Output A+B in decimal number in one line.

Sample Input

1 9
A B
a b

Sample Output

10
21
21
#include<stdio.h>

int main()
{
    int x,y;
    while(scanf("%x %x",&x, &y)!=EOF)
    {
        printf("%d\n",x+y);
    }
    return 0;
}

抱歉!评论已关闭.