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;
}
PWN! 第一次测试答案及讲解的更多相关文章
- 2018.10.19浪在ACM 集训队第一次测试赛
2018.10.19浪在ACM 集训队第一次测试赛 待参考资料: [1]:https://blog.csdn.net/XLno_name/article/details/78559973?utm_so ...
- Hibernate的第一次测试解析
解析:此题目考查的是对Hibernate中交叉连接的理解.HQL支持SQL风格的交叉连接查询,交叉连接适用于两个类之间没有定义任何关联时.在where字句中,通过属性作为筛选条件,如统计报表数据.使用 ...
- javascript专业八级测试答案整理
前几天社区的群里森破发了一个这样的链接: http://ourjs.com/detail/52fb82e13bd19c4814000001 做了一遍后突然对人生感到了迷茫,本着不能只有我一个人伤心的原 ...
- python网络爬虫。第一次测试-有道翻译
2018-03-0720:53:56 成功的效果如下 代码备份 # -*- coding: UTF-8 -*- from urllib import request from urllib impor ...
- C基础的练习集及测试答案(提高题)
提高题:1.编写程序,随机生成一个1~10内的数,让对方猜3次.如果3次内能猜中则输出“恭喜你”:若3次内猜不中则输出正确答案.C语言中提供生成随机数的函数rand()用法:①所需头文件:#inclu ...
- partOneJava学习卷土重来-----第一次测试题目介绍
石家庄铁道大学2021年秋季 2020 级课堂测试试卷(一)(15分) 课程名称: JAVA语言程序设计 任课教师: 王建民 考试时间: 150 分钟 一.考试要求: 1.按照测试 ...
- 第一次测试HTML和CSS
1.HTML(Hyper Text Markup Languange)超文本标记语言.HTML文件扩展名通常是:htm和html. <html> <head> <titl ...
- #C++初学记录 (第一次测试)(A - 复习时间 )
练习题目一 (A-复习时间) 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑.xhd复习有个习惯,在复习完一门课后,他总是挑一门更简单的课进行复习,而他复习这门课的效率为两门课的难度差的 ...
- C基础的练习集及测试答案(40-50)
40.(课堂)打印杨辉三角型前10行 #if 0 40.(课堂)打印杨辉三角型前10行 思路分析: 一.打印十行杨辉三角得第十行长度为十,所以建立一个长度为十的数组,作为每行的数据存储 二.按 0-9 ...
随机推荐
- jQuery(三)、属性、CSS
jQuery设置了很多为标签进行属性的操作,比如添加.删除. 一 .属性 1 attr(name | properties | [key, value | fn]) 设置或返回被选择的属性值. 参数: ...
- java爬虫系列第五讲-如何使用代理防止爬虫被屏蔽?
本文内容 1.分析一下爬虫存在的问题及解决方案 2.webmagic中代理的使用 3.目前市面上一些比较好用的代理服务器 存在的问题 我们在使用爬虫过程中,大多都会遇到这样的问题:突然某一天爬虫爬不到 ...
- Java高阶语法---transient
背景:听说transient Java高阶语法是挺进BAT必经之路. transient: Java中transient 关键字的作用,简单的说就是让某些被修饰的成员属性变量不被序列化. 这又扯到了序 ...
- SpringBoot 动态更新 resources 目录的文件
一.前言 SpringBoot 打成 Jar 包形式运行后 ,resources 目录下文件的读取修改和原来不太一样,网上比较多的是关于读取的方式,修改的几乎没有,终于在 stackoverflow ...
- 第十课html5 新增标签及属性 html5学习5
一.常用新增标签 1.header:定义页面的页眉头部 2.nav:定义导航栏 3.footer:定义页面底部,页脚 4.article:定义文章 5.section:定义区域 6.aside:定义侧 ...
- Easyui 关闭jquery-easui tab标签页前触发事件
关闭jquery-easui tab标签页前触发事件 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 需求场景 点击父页面tab 页关闭按钮时,需要做判断,判 ...
- DVWA 黑客攻防演练(十一) 存储型 XSS 攻击 Stored Cross Site Scripting
上一篇文章会介绍了反射型 XSS 攻击.本文主要是通过 dvwa 介绍存储型 XSS 攻击.存储型 XSS 攻击影响范围极大.比如是微博.贴吧之类的,若有注入漏洞,再假如攻击者能用上一篇文章类似的代码 ...
- sqlserver 2014使用时有Cannot find one or more components
好久没用sqlserver,今天打开却出现了一个错误,Cannot find one or more components,令人头疼.在启动Microsoft SQL Server Managemen ...
- Linux shell脚本中shift的用法说明
shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本). 示例1:依次读取输入的参数并打印参数个数:run ...
- Postgres 优雅存储树形数据
碰到一个树形数据需要存储再数据控制,碰到以下两个问题: 在PG数据库中如何表达树形数据 如何有效率的查询以任意节点为Root的子树 测试数据 为了更加简单一些,我们将使用一下数据 Section A ...