PAT 2018 春
A 1140 Look-and-say Sequence
简单模拟。可能要注意字符串第一个字符和最后一个字符的处理。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector> using namespace std;
string numToString(int n)
{
string tmpStr;
int tmpNum;
while(n > )
{
tmpNum = n%;
n /= ;
tmpStr = (char)(tmpNum + '')+tmpStr;
}
return tmpStr;
}
int main()
{
int m, tmpNum;;
string tmpStr, rstStr;
char tmpC;
cin >> tmpStr >> m;
rstStr = tmpStr;
m--;
while(m--)
{
rstStr.clear();
tmpNum = ;
tmpC = 'A';
for(int i = ; i < tmpStr.size(); ++i)
{
if(tmpStr[i] != tmpC)
{
if(i > )
rstStr = rstStr + tmpC + numToString(tmpNum);
tmpNum = ;
tmpC = tmpStr[i];
}
else
tmpNum++;
if(i == tmpStr.size()-)
rstStr = rstStr + tmpC + numToString(tmpNum);
}
tmpStr = rstStr;
}
cout << rstStr;
return ;
}
A 1141 PAT Ranking of Institutions
排序题目。成绩为了尽量无误差,先按A,B,T存储,最后计算并排序。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
typedef struct SCHOOLINFO
{
int score;
int peopleCnt;
int AScore, BScore, TScore;
string id;
SCHOOLINFO():AScore(),BScore(),TScore(),peopleCnt(){}
SCHOOLINFO(string str):AScore(),BScore(),TScore(),peopleCnt(),id(str){}
}schoolinfo;
unordered_map<string, int> schoolFlagMap;
vector<schoolinfo> schoolDataVec;
bool cmp(schoolinfo a, schoolinfo b)
{
if(a.score != b.score)
return a.score > b.score;
else if(a.peopleCnt != b.peopleCnt)
return a.peopleCnt < b.peopleCnt;
else
return a.id < b.id;
}
void strToLower(string &tmpStr)
{
for(int i = ; i < tmpStr.size(); ++ i)
{
if(tmpStr[i] <= 'Z' && tmpStr[i] >= 'A')
tmpStr[i] = tmpStr[i]-'A'+'a';
}
}
int main()
{
int n, index = , tmpNum;
string tmpId, tmpSchool;
int tmpScore;
cin >> n;
while(n--)
{
cin >> tmpId >> tmpScore >> tmpSchool;
strToLower(tmpSchool);
if(schoolFlagMap[tmpSchool] == )
{
schoolFlagMap[tmpSchool] = index++;
schoolDataVec.push_back(SCHOOLINFO(tmpSchool));
}
tmpNum = schoolFlagMap[tmpSchool]-;
schoolDataVec[tmpNum].peopleCnt++;
if(tmpId[] == 'T')
schoolDataVec[tmpNum].TScore += tmpScore;
else if(tmpId[] == 'A')
schoolDataVec[tmpNum].AScore += tmpScore;
else
schoolDataVec[tmpNum].BScore += tmpScore;
}
for(auto it = schoolDataVec.begin(); it != schoolDataVec.end(); ++it)
it->score = it->AScore + it->BScore/1.5 + it->TScore*1.5;
sort(schoolDataVec.begin(), schoolDataVec.end(), cmp);
int tmpTWS = -, rankCnt = ;
cout << schoolDataVec.size() << endl;
for(int i = ; i < schoolDataVec.size(); ++i)
{
schoolinfo tmpSchool = schoolDataVec[i];
if(tmpSchool.score != tmpTWS)
rankCnt = i+;
cout << rankCnt << " " << tmpSchool.id << " " <<tmpSchool.score << " " <<tmpSchool.peopleCnt << endl;
tmpTWS = schoolDataVec[i].score;
}
return ;
}
A 1142 Maximal Clique
读懂题目就行,写一个判断是否与所给的其它各点连通的函数。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
#define MAX_SIZE 210
int route[MAX_SIZE][MAX_SIZE]={};
int Nv, Ne, M;
vector<int> cliqueVec;
bool judge(int u)
{
for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
{
if(*it == u)
continue;
if(route[*it][u] == )
return false;
}
return true;
}
int main()
{
cin >> Nv >> Ne;
int tmpSt, tmpEnd, tmpCnt, tmpNum;
for(int i = ; i <= Ne; ++i)
{
cin >> tmpSt >> tmpEnd;
route[tmpSt][tmpEnd] = ;
route[tmpEnd][tmpSt] = ;
}
cin >> M;
while(M--)
{
cin >> tmpCnt;
bool dealFlag = false;
vector<int> visitFlag(Nv+, );
cliqueVec.resize(tmpCnt,);
for(int i = ; i < tmpCnt; ++i)
{
cin >> cliqueVec[i];
visitFlag[cliqueVec[i]] = ;
}
for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
if(!judge(*it))
dealFlag = true;
if(dealFlag)
{
cout << "Not a Clique" << endl;
continue;
}
for(int i = ; i <= Nv; ++i)
if(visitFlag[i] == && judge(i))
dealFlag = true;
if(dealFlag)
{
cout << "Not Maximal" <<endl;
continue;
}
cout << "Yes" << endl;
}
return ;
}
A 1143 Lowest Common Ancestor
这道题目,深刻理解树的前序遍历和BST的话,是道比较水的题目。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
vector<int> treeKeyVec();
unordered_map<int, int> keyValMap;
int M, N;
void findLCA(int a, int b)
{
for(int i = ; i <N; ++i)
{
int tmpNum = treeKeyVec[i];
bool flag = false;
if(tmpNum == a)
printf("%d is an ancestor of %d.\n", a, b);
else if(tmpNum == b)
printf("%d is an ancestor of %d.\n", b, a);
else if(tmpNum < max(a,b) && tmpNum > min(a,b))
printf("LCA of %d and %d is %d.\n", a, b, tmpNum);
else
flag = true;
if(!flag)
return;
}
}
int main()
{
cin >> M >> N;
int tmpNum;
for(int i = ; i < N; ++i)
{
cin >> tmpNum;
keyValMap[tmpNum] = ;
treeKeyVec[i] = tmpNum;
}
int tmpNum1, tmpNum2;
for(int i = ; i < M; ++i)
{
cin >> tmpNum1 >> tmpNum2;
bool flag1 = true, flag2 = true;
if(keyValMap[tmpNum1] == )
flag1 = false;
if(keyValMap[tmpNum2] == )
flag2 = false;
if(!flag1 && !flag2)
printf("ERROR: %d and %d are not found.\n", tmpNum1, tmpNum2);
else if(!flag1)
printf("ERROR: %d is not found.\n", tmpNum1);
else if(!flag2)
printf("ERROR: %d is not found.\n", tmpNum2);
else
findLCA(tmpNum1, tmpNum2);
}
return ;
}
PAT 2018 春的更多相关文章
- 2018春招-今日头条笔试题-第四题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) #-*- coding:utf-8 -*- class Magic: ''' a:用于存储数组a b:用于存储数组b num:用于 ...
- 2018春招-今日头条笔试题-第三题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...
- 2018春招-今日头条笔试题-第二题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...
- 2018春招-今日头条笔试题-第一题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 要想得到输入的数字列中存在相隔为k的数,可以将输入的数字加上k,然后判断其在不在输入的数字列中即可. #-*- cod ...
- 2018春招实习笔试面试总结(PHP)
博主双非渣本计算机软件大三狗一枚,眼看着春招就要结束了,现将自己所经历的的整个春招做一个个人总结. 首先就是关于投递计划,博主自己整理了一份各大公司的春招信息,包括网申地址,开始时间,结束时间,以及自 ...
- 链家2018春招Java工程师编程题题解
Light 题目描述 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮.现在问你,使用这n组开关,最多能够使得多少个灯泡点亮呢? 输入 第一行一个n,表示有n组开关.接下来n行,每行第一个 ...
- 爱奇艺2018春招Java工程师编程题题解
字典序最大子序列 题目描述 对于字符串a和b,如果移除字符串a中的一些字母(可以全部移除,也可以一个都不移除)就能够得到字符串b我们就称b是a的子序列. 例如."heo"是&quo ...
- 2018春招-今日头条笔试题5题(后附大佬答案-c++版)
1题目描述 在n个元素的数组中,找到差值为k的除重后的数字对 输入描述 第一行:n和k,n表示数字的个数,k表示差值 第二行:n个整数 输入样例 输入: 5 2 1 5 3 4 2 输出: 3 说明: ...
- 2018春招-美团后台开发方向编程题 (python实现)
第一题:字符串距离 题目: 给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量.如串”aab”与串”aba”的距离为 2:串”ba”与串”aa”的距离为 1: ...
随机推荐
- loadrunner 手动添加关联
以loadrunner自带的订票系统为例 1.录制两次订票流程 2.比较两次不同点(Tools ->Compare with script) 3.找到服务器返回的动态值 4.回到Script模式 ...
- Ternsorflow 学习:006-MNIST进阶 深入MNIST
前言 这篇文章适合实践过MNIST入门的人学习观看.没有看过MNIST基础的人请移步这里 深入MNIST TensorFlow是一个非常强大的用来做大规模数值计算的库.其所擅长的任务之一就是实现以及训 ...
- R 《回归分析与线性统计模型》page141,5.2
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data141.xlsx") ...
- SQL中的Where,Group By,Order By和Having
说到SQL语句,大家最开始想到的就是他的查询语句: select * from tableName: 这是最简单的一种查询方式,不带有任何的条件. 当然在我们的实际应用中,这条语句也是很常用到的,当然 ...
- 01 vue入门
vue简介 官网上有介绍,这里粘出来 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心 ...
- 配置 git公钥报错:unknown key type -rsa
配置 git公钥的时候出现:ssh-keygen unknown key type -rsa 一个解决办法是去本地寻找.ssh文件,参考路径(C:\Users\Administrator.ssh),把 ...
- 谈谈HashSet的存储原理及为什么重写equals必须重写hashcode方法
HashSet的存储原理: 1.将要传入的数据根据系统的hash算法得到一个hash值: 2.根据hash值可以得出该数据在hash表中的位置: 3.判断该位置上是否有值,没有值则把数据插入进来:如果 ...
- Spark 资料整理
http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ http://blog.csdn.net/aliv ...
- java.io.IOException: Error: JSP Buffer overflow
错误 jsp页面报错如下: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java ...
- Oracle delete 之后恢复数据
当我们粗心大意直接delete from不加条件而又没有回滚的时候有一个很简单的方法能够将数据恢复到delete之前的状态 第一种方案已经帮助我解决了实际问题.第二种方案暂未实践 在此记录下以便日后查 ...