PAT 2012 冬
A Shortest Distance
题意相当于一个环,找出两点间从不同方向得到的距离中的最小值。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std;
vector<int> disVec;
int main()
{
int N, tmpDis;
cin >> N;
disVec.resize(N+,);
for(int i = ; i <= N; ++ i)
{
cin >> tmpDis;
disVec[i] = disVec[i-] + tmpDis;
}
cin >> disVec[];
int M, tmpSt, tmpEnd;
cin >> M;
for(int i = ; i < M; ++ i)
{
cin >> tmpSt >> tmpEnd;
if(tmpSt > tmpEnd)
swap(tmpSt, tmpEnd);
tmpDis = min(disVec[tmpEnd]-disVec[tmpSt], disVec[tmpSt]+disVec[]-disVec[tmpEnd]+disVec[N]);
cout << tmpDis << endl;
}
}
B Student List for Course
课程清单,这个题目有点意思,我是通过把cin,cout 改成printf和scanf让它不超时的。
试过用set会超时,暂时没有想到更好的方法。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector> using namespace std;
vector<int> disVec;
vector<string> classInfo[];
int main()
{
int N, K, tmpNum, tmpClass;
string tmpStr;
cin >> N >> K;
for(int i = ; i <= N; ++ i)
{
cin >> tmpStr;scanf("%d", &tmpNum);
while(tmpNum--)
{
scanf("%d", &tmpClass);
classInfo[tmpClass].push_back(tmpStr);
}
}
for(int i = ; i <= K; ++ i)
{
vector<string> tmpVec = classInfo[i];
sort(tmpVec.begin(), tmpVec.end());
printf("%d %d\n", i, tmpVec.size());
for(auto it = tmpVec.begin(); it != tmpVec.end(); ++ it)
{
printf("%s\n", (*it).c_str());
}
}
return ;
}
C Find Coins
因为需要两个硬币,所以可以排序后,用双指针法,从两端向中间找,因为在找到合适两个硬币前,一旦左侧硬币加右侧硬币超过了所需面值,右边这个硬币肯定是不可能满足条件的。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector> using namespace std;
vector<int> coinVal;
int main()
{
int N, M, tmpVal;
cin >> N >> M;
coinVal.resize(N);
for(int i = ; i < N; ++ i)
cin >> coinVal[i];
int tmpLast = N-;
sort(coinVal.begin(), coinVal.end());
for(int i = ; i < tmpLast; ++ i)
{
while(coinVal[i] + coinVal[tmpLast] > M)
tmpLast --;
if(tmpLast != i && coinVal[i] + coinVal[tmpLast] == M)
{
cout << coinVal[i] << " " << coinVal[tmpLast];
return ;
}
}
cout << "No Solution";
return ;
}
D Counting Ones
这个题目需要先利用数学知识找一下规律,会发现每一位产生的1的个数是有规律的,见代码while循环部分。
(其中numArray[i]是第i位数值,multBase是0-9、0-99、0-999...中包含1的个数,digitBase是0,10,100,1000...)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector> using namespace std;
int charToNum(char c)
{
return c-'';
}
int n;
char numStr[];
int numArray[];
int len;
int numDeal(int tmpN)
{
int tmpNum = ;
int digitBase = ;
int multBase = ;
while(tmpN >= )
{
if(numArray[tmpN] == )
{
tmpNum += numArray[tmpN] * multBase + n%digitBase+;
}
else if(numArray[tmpN] > )
{
tmpNum += numArray[tmpN] * multBase + digitBase;
}
multBase = * multBase + digitBase;
digitBase *= ;
tmpN--;
}
return tmpNum;
}
int main()
{ cin >> n;
sprintf(numStr, "%d", n);
len = strlen(numStr);
for(int i = ; i < len; ++ i)
numArray[i] = charToNum(numStr[i]);
int num = numDeal(len-);
cout << num;
return ;
}
PAT 2012 冬的更多相关文章
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- 1.初始Windows Server 2012 R2 Hyper-V + 系统安装详细
干啥的?现在企业服务器都是分开的,比如图片服务器,数据库服务器,redis服务器等等,或多或少一个网站都会用到多个服务器,而服务器的成本很高,要是动不动采购几十台,公司绝对吃不消的,于是虚拟化技术出来 ...
- 0.Win8.1,Win10,Windows Server 2012 安装 Net Framework 3.5
后期会在博客首发更新:http://dnt.dkill.net 网站部署之~Windows Server | 本地部署:http://www.cnblogs.com/dunitian/p/482280 ...
- windows 2012 r2 can't find kb2919355
问题 解决: 1.手动安装了 Windows8.1-KB2919442-x64 2.手动下载 KB2919355 更新成功 Turns out to have been a result ...
- Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
今天给客户配置故障转移群集,在Windows Server 2012 R2的系统上,通过iSCSI连接上DELL的SAN存储后,在磁盘管理里面发现可以新建 简单卷.跨区卷.带区卷.镜像卷.RAID-5 ...
- VMware下Windows Server 2012添加新磁盘
系统管理员在VM下新装了一台Windows Server 2012服务器,我在上面安装了SQL Server 2014 Standard版数据库,安装之初,只分配了一个C盘,我想在这台服务器上添加了三 ...
- 在Windows Server 2012中如何快速开关桌面上经典的“计算机、我的文档”等通用图标
我们都知道,在Windows Server 2012系列的服务器版本中都已经引入了Modern的现代界面作为默认的用户交互界面,同时满足视觉一致化,新版的服务器管理程序也做成了扁平化.因此传统的计算机 ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
随机推荐
- C++面试常见问题——17类模板的使用
类模板的使用 注意在每次类模板函数时都需要声明一个类模板 #include<iostream> using namespace std; template <class T,int ...
- PHP数组——定义,类型,遍历数组,数组函数
1.定义 $attr=array(); //标准定义方式 $attr=[1,2]; $attr[0]="hello"; ...
- gem5-gpu 运行 PARSEC2.1
PARSEC是针对共享内存多核处理器(CPU)的一套基准测试程序,详细介绍见wiki:http://wiki.cs.princeton.edu/index.php/PARSEC,主要参考:http:/ ...
- maven详解 之 pom.xml
Maven 一个项目管理工具 其作用就是用来管理jar 包的 maven的核心 pom.xml配置文件 <project xmlns="http://maven.apache ...
- leetcode1305 All Elements in Two Binary Search Trees
""" Given two binary search trees root1 and root2. Return a list containing all the i ...
- redisTemplate注入为空
springboot2.*集成redis时,redis工具类中的redisTemplate注入后总是为空. 问题代码还原: 1.工具类定义成静态工具类,@Resource注入redisTemplate ...
- LeetCode1217 玩筹码(贪心)
题目: 数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中. 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以): 将第 i 个筹码向左或者右移动 2 个单位,代价为 ...
- 配置tomcat、nginx实现反向代理(需操作)
配置tomcat.nginx实现反向代理现在我想通过nginx访问tomcat 这就要我们去修改nginx的核心配置文件,在其目录下的conf文件夹下的nginx.conf文件,那么首先我们就要了解该 ...
- cf221 D. Little Elephant and Array
题解真的是太神奇了2333 用离线和树状数组(为什么感觉和HH的项链是的,什么鬼),比较巧妙的是他把整个数列分成几段.用一个vector来记录每个数出现的位置.一共就是data[a[i]][sz]-- ...
- Spring-IOC(基于XML配置)
什么是IOC? IOC: Inversion of Control(控制反转). 控制反转:将对象的创建权反转给(交给)Spring. 在使用Spring框架之后,对象的实例不再由调用者来创建,而 是 ...