PWN! 第一次测试答案及讲解

题目链接:https://vjudge.net/contest/279567#overview
题目密码:190118

1、A+B:(考察点:EOF输入、加法运算)

Topic:

Calculate A + B.

Input:

Input contains multiple test cases。
Each test case contains two integers: A(1 ≤ A ≤ 100) and B(1 ≤ B ≤ 100).

Output:

For each test case output the sum A + B.

Sample Input:

1 2
3 4

Sample Output:

3
7

解析及答案:

简单的EOF输入,多行重复输入后再进行求和。

代码如下:

#include <stdio.h>

int main() 
{
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF)
    {
        printf("%d", a + b);
    }

    return 0;
}

2、最小公倍数:(考察点:EOF输入、最大公约数函数和最小公倍数函数)

Topic:

给定两个正整数,计算这两个数的最小公倍数。

Input:

输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.

Output:

对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。

Sample Input:

10 14

Sample Output:

70

解析及答案:

运用Wiki百科上已有关于最小公倍数最大公约数的函数,进行编写程序。(个人函数库——最大公约数和最小公倍数)

代码如下:

#include <stdio.h>

int GCD(int a, int b);
int LCM(int a, int b);

int main() 
{
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF)
    {
        printf("%d\n", LCM(a, b));
    }

    return 0;
}

int GCD(int a, int b)
{
    if (b)
        while ((a %= b) && (b %= a));
    return a + b;
}

int LCM(int a, int b)
{
    return a * b / GCD(a, b);
}

3、2月29日:(考察点:闰年函数的判断、字符串的使用、数组的使用、自定义函数的使用)

Topic:

Given two dates, please calculate the number of February 29 between the two dates inclusively.

Only a leap year contains February 29. The year satisfying one of the following conditions is a leap year.

\1. The year number can be divided by 4 but cannot be divided by 100.

\2. The year number can be divided by 400.

Input:

The first line contains an integer T, indicating the number of test cases.

Each test case contains two lines which are both in the format "Month Day, Year", indicating the dates. Month is a string contained in {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}. Day and Year are two integers.

We guarantee that all the dates are valid. The first date comes before the second date or the two dates are the same.

Output:

For each test case, output a line containing "Case #X: Y". X is the test case number starting from 1, and Y is the answer.

Sample Input:

4
January 12, 2012
March 19, 2012
August 12, 2899
August 12, 2901
August 12, 2000
August 12, 2005
February 29, 2004
February 29, 2012

Sample Output:

Case #1: 1
Case #2: 0
Case #3: 1
Case #4: 3

解析及答案:

这里的核心思想是:分别计算从公元1年到起始年和到结束年的闰年个数,然后用差值的方式计算出两年之间的闰年个数。

但是值得注意的是,在首尾两端的年份,我们需要判断它的日期是否包含2月29日,否则即使它是闰年也没用。

然后我在这里使用了一个C++中的用法:string 。这也是一种数据类型,用于字符和字符串。下面是三种用于声明字符和字符串数组的方法:

char Array1[5] = { 'a','b','c','d','e' };
char Array2[5][10] = { "one","two","three","four","five" };
string Array3[5] = { "one","two","three","four","five" };

我们都知道数组的下标严格控制数据的个数,在第一种数组中就是严格的5个数组。

第二种数组中,我们使用的是二维数组,最开始数组里面都是空,然后在录入的过程中会在字符结束的时候会将 '\0' 录到结尾,但是不会显示。也就是说原本5个的储存空间的会变成6个,这是字符串数组,具体的请查询专业书籍,这里只是点出。

但是这会又一个很大的问题,那就是会浪费内存空间,而且这样会声明很多子数组,导致地址的复杂,但是如果是 string 数组的话,就不会浪费内存,而且他会在字符串录入结束后自动赋予 '\0' 这无疑是一种很好的方法,并且 string 数组只有一个地址,那就是数组名的初地址,这与一维数组的应用是一致的,更方便使用和理解。

代码如下:

#include <cstdio>
#include <string>

string months[12]= { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December" };

bool isLeapYear(int year);
int Calculation(int year, int mon, int day);

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

    for (int t = 1; t <= T; t++)
    {
        char CharStartMonth[10], CharEndMonth[10];
        int StartDay, StartYear, StartMonth, EndDay, EndYear, EndMonth;

        scanf("%s %d, %d", &CharStartMonth, &StartDay, &StartYear);
        scanf("%s %d, %d", &CharEndMonth, &EndDay, &EndYear);
        
        for (int i = 0; i < 12; i++)                                        // 将字符串对应的年份换算成数字年份
        {
            if (CharStartMonth == months[i])
                StartMonth = i + 1;
            if (CharEndMonth == months[i])
                EndMonth = i + 1;
        }

        int num1 = Calculation(StartYear, StartMonth, StartDay);                    
        int num2 = Calculation(EndYear, EndMonth, EndDay);
        int res = num2 - num1;                                              // 闰年差,得出两个年份之间的闰年个数

        if (isLeapYear(StartYear) && StartMonth == 2 && StartDay == 29)     // 如果正好是 2月29日 的时候,则该年算是闰年
            res++;

        printf("Case #%d: %d\n", t, res);
    }

    return 0;
}

bool isLeapYear(int year)                                                   // 闰年判断函数
{
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return true;
    else
        return false;
}

int Calculation(int year, int mon, int day)
{
    int sum = year / 4 + year / 400 - year / 100;                           // 计算出从 公元1年 至目前所有的闰年个数
    if (isLeapYear(year) && (mon < 2 || mon == 2 && day < 29))              // 判断起始年或终止年是否包含 2月29日
        sum--;
    return sum;
}

4、最右边的数:(考察点:快速幂的使用)

Topic:

Given a positive integer N, you should output the most right digit of N^N.

Input:

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output:

For each test case, you should output the rightmost digit of N^N.

Sample Input:

2
3
4

Sample Output:

7
6

Hint:

In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

解析及答案:

题目的要求是 (1 <= N <= 1,000,000,000) 这么一个范围是绝对的会超出目前我们所有的数据类型,甚至硬盘放不放不下都是一个问题。但是根据题意,我们只需要求出最后一位的数就行。

那么我们通过初步的草稿运算可以得出以下的规律:

7*7=49                  7*7=49……9
49*7=343                7*9=63……3
343*7=2401              7*3=21……1
……

那就是如果每次乘幂之前对10取余,也就是只取最后一位数字,然后继续进行乘幂运算。这样的话对最后一位的数字并没有什么改变。我们的代码也是源于这样的一个思路而展开的。

值得注意的是我们采用的是 long 而不是 int ,因为可能还是在运算过程中会有超出 int 范围的数据出现,比如任何大于或等于 2 的数乘以 1,000,000,000 ,则会超出 int 的范围。所以为了避免这种事情的发生,我们采用 long

代码如下:

#include <stdio.h>

long long Pow(long long base, long long index);
int main()
{
    int len = 0, num = 0;
    scanf("%d", &len);
    
    while (len--)
    {
        cin >> num;
        printf("%d\n", Pow(num, num));
    }
    return 0;
}

long long Pow(long long base, long long index)
{
    int temp = 1;
    base = base % 10;
    while (index > 0) 
    {
        if (index % 2 == 1)
            temp = (temp * base) % 10;
        index = index / 2;
        base = (base * base) %10;
    }
    return temp;
}

5、都8102年了:(考察点:逻辑思维)

Topic:

"都8102年了"是2018年网络流行语,8102是将今年年份2018的各位数字倒排的结果,常用来表达“都这个年头了,怎么还有某某事情发生"之意。

这个梗的关键在于将年份各位数字倒排之后,得到一个非常遥远的未来年份。小Hi想知道,哪些年份适合使用这个梗。具体来说,一个年份Y,倒排之后得到年份X,如果X-Y>=1000,我们就认为Y适合这个梗。

给定起止年份,请你判断这段时间内有几个年份适合这个梗。

Input:

两个正整数代表年份,A和B。

1000 <= A <= B <= 9999

Output:

一个整数代表答案

Sample Input:

2018 2020

Sample Output:

2

解析及答案:

这里的思路十分简单,只需将一个四位数导致然后减去原数大于等于1000即可。如果出现 1000 -> 0001 情况,则看为 1 即可。

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;

int main() 
{
    int StartYear, EndYear, AllYear = 0;
    scanf("%d %d", &StartYear, &EndYear);

    for (int i = StartYear; i <= EndYear; i++)
    {
        int year = i, sum = 0;
        for (int j = 0; j < 4; j++)             // 数据转置
        {
            sum = sum * 10 + year % 10;
            year /= 10;
        }
        if (sum - i >= 1000)
            AllYear++;
    }
    printf("%d", AllYear);

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/JingWenxing/p/10291299.html
pwn
今日推荐