1136 A Delayed Palindrome(20 分)

题意:给定字符串A,判断A是否是回文串。若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palindrome;否则继续迭代。

分析:根据题意模拟。

1、C++写法。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
using namespace std;
const int MAXN = 1000 + 10;
string A;
bool judge(string x){
int len = x.size();
for(int i = 0; i < len / 2; ++i){
if(x[i] != x[len - 1 - i]) return false;
}
return true;
}
string add(string x, string y){
string ans;
int lenx = x.size();
int leny = y.size();
int tmp = 0;
int i, j;
for(i = lenx - 1, j = leny - 1; i >= 0 && j >= 0; --i, --j){
tmp += x[i] - '0' + y[j] - '0';
ans += (tmp % 10) + '0';
tmp /= 10;
}
while(i >= 0){
tmp += x[i] - '0';
ans += (tmp % 10) + '0';
tmp /= 10;
--i;
}
while(j >= 0){
tmp += y[j] - '0';
ans += (tmp % 10) + '0';
tmp /= 10;
--j;
}
if(tmp > 0) ans += tmp + '0';
reverse(ans.begin(), ans.end());
return ans;
}
int main(){
while(cin >> A){
if(judge(A)){
printf("%s is a palindromic number.\n", A.c_str());
continue;
}
int cnt = 0;
while(1){
string B = A;
reverse(B.begin(), B.end());
string C = add(A, B);
printf("%s + %s = %s\n", A.c_str(), B.c_str(), C.c_str());
if(judge(C)){
printf("%s is a palindromic number.\n", C.c_str());
break;
}
++cnt;
if(cnt == 10){
printf("Not found in 10 iterations.\n");
break;
}
A = C;
}
}
return 0;
}

2、python写法。 

def judge(x):
y = x[::-1]
if x == y: return True
return False
A = input()
if judge(A):
print(A, "is a palindromic number.")
else:
cnt = 0
while(True):
B = A[::-1]
C = int(A) + int(B)
C = str(C)
print(A, "+", B, "=", C)
if judge(C):
print(C, "is a palindromic number.")
break
cnt += 1
if cnt == 10:
print("Not found in 10 iterations.")
break
A = C
1137 Final Grading(25 分)

题意:为得到证书,首先online programming assignments要不少于200分,其次final grade要不少于60分。其中,final grade的计算方法为:若G​mid−term​​>G​final,则G=(G​mid−term​​×40%+G​final​​×60%);否则,G=G​final​​。

分析:根据题意模拟。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 100000 + 10;
struct Node{
string name;
double Gp, Gmid, Gfinal, G;
Node(){
Gp = Gmid = Gfinal = -1;
}
bool operator < (const Node&rhs)const{
return G > rhs.G || G == rhs.G && name < rhs.name;
}
}num[MAXN];
int cnt;
map<string, int> mp;
int getId(string x){
if(mp.count(x)) return mp[x];
return mp[x] = ++cnt;
}
int main(){
int P, M, N;
scanf("%d%d%d", &P, &M, &N);
string name;
double score;
for(int i = 0; i < P; ++i){
cin >> name >> score;
int id = getId(name);
num[id].Gp = score;
num[id].name = name;
}
for(int i = 0; i < M; ++i){
cin >> name >> score;
int id = getId(name);
num[id].Gmid = score;
num[id].name = name;
}
for(int i = 0; i < N; ++i){
cin >> name >> score;
int id = getId(name);
num[id].Gfinal = score;
num[id].name = name;
}
for(int i = 1; i <= cnt; ++i){
if(num[i].Gmid > num[i].Gfinal){
num[i].G = num[i].Gmid * 0.4 + num[i].Gfinal * 0.6;
}
else{
num[i].G = num[i].Gfinal;
}
num[i].G = round(num[i].G);
}
sort(num + 1, num + 1 + cnt);
for(int i = 1; i <= cnt; ++i){
if((int)num[i].Gp >= 200 && (int)num[i].G >= 60){
printf("%s %.0lf %.0lf %.0lf %.0lf\n", num[i].name.c_str(), num[i].Gp, num[i].Gmid, num[i].Gfinal, num[i].G);
}
}
return 0;
}
1138 Postorder Traversal(25 分)

题意:给定前序遍历和中序遍历,求后序遍历的第一个点。

分析:根据前序遍历和中序遍历的性质,递归建树,并记录后序遍历的值。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 50000 + 10;
int preorder[MAXN], inorder[MAXN];
vector<int> ans;
void build(int pL, int pR, int iL, int iR, int root){
if(pL > pR) return;
int id = iL;
while(inorder[id] != root) ++id;
int cnt = id - iL;
build(pL + 1, pL + cnt, iL, id - 1, preorder[pL + 1]);
build(pL + cnt + 1, pR, id + 1, iR, preorder[pL + cnt + 1]);
ans.push_back(root);
}
int main(){
int N;
while(scanf("%d", &N) == 1){
for(int i = 0; i < N; ++i) scanf("%d", preorder + i);
for(int i = 0; i < N; ++i) scanf("%d", inorder + i);
int root = preorder[0];
build(0, N - 1, 0, N - 1, root);
printf("%d\n", ans[0]);
}
return 0;
}
1139 First Contact(30 分)

题意:给定N个人和M条关系,若A想和B联系,首先A在自己的朋友中联系与其同性别的C,C在自己的朋友中联系与B同性别的D,而且D和B也是朋友,求能使A和B互相联系的C和D。

分析:

1、由于用4位数字的ID表示一个人,负数为女,正数为男,且同一个人只可能有一个性别,所以,在用atoi函数将读取的ID转化为数字并取绝对值后,可保证每个人的ID互不相同。

2、枚举A的同性朋友i和B的同性朋友j,若i和j是朋友,则满足输出条件。

3、用map<int, map<int, bool> > isfriend判断两人是否为朋友。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 10000 + 10;
vector<int> G[MAXN];
map<int, map<int, bool> > isfriend;
int cnt;
char x[10], y[10];
struct Node{
int x, y;
bool operator < (const Node&rhs)const{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
}num[MAXN];
int main(){
int N, M;
scanf("%d%d", &N, &M);
while(M--){
scanf("%s%s", x, y);
int id1 = abs(atoi(x));
int id2 = abs(atoi(y));
isfriend[id1][id2] = isfriend[id2][id1] = true;
if(strlen(x) == strlen(y)){
G[id1].push_back(id2);
G[id2].push_back(id1);
}
}
int K;
scanf("%d", &K);
while(K--){
cnt = 0;
scanf("%s%s", x, y);
int id1 = abs(atoi(x));
int id2 = abs(atoi(y));
int len1 = G[id1].size();
int len2 = G[id2].size();
for(int i = 0; i < len1; ++i){
for(int j = 0; j < len2; ++j){
int tmpx = G[id1][i];
int tmpy = G[id2][j];
if(tmpx != id1 && tmpx != id2 && tmpy != id1 && tmpy != id2 && isfriend[tmpx][tmpy]){
num[++cnt].x = tmpx;
num[cnt].y = tmpy;
}
}
}
sort(num + 1, num + 1 + cnt);
printf("%d\n", cnt);
for(int i = 1; i <= cnt; ++i){
printf("%04d %04d\n", num[i].x, num[i].y);
}
}
return 0;
}

PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟的更多相关文章

  1. PAT L2-004. 这是二叉搜索树吗?【前序遍历转化为后序遍历】

    一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树. 所谓二叉搜索 ...

  2. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  3. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  4. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  5. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  6. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  9. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

随机推荐

  1. POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

  2. Linux vim中方向键变成字母的问题

    使用Ubuntu Desktop 18.04 时 发现 vim 在编辑模式的时候,方向键变成了字母ABCD. 原因: Ubuntu预装的是vim tiny版本,安装vim full版本即可解决. 1. ...

  3. 设计模式课程 设计模式精讲 3-4 依赖倒置原则讲解+coding

    1 课程讲解 1.1 定义 1.2 优点 1.3 细节描述 2 代码演练 2.0 代码展示优点 2.1 非面向接口编程 2.2 面向接口编程1 传参 2.3 面向接口编程2 构造函数 2.4 面向接口 ...

  4. DC: 8-Write-up

    下载地址:点我 哔哩哔哩:点我 信息收集 确定网段,找到虚拟机的IP,扫端口和服务. ➜ ~ nmap -sn 192.168.116.1/24 Starting Nmap 7.80 ( https: ...

  5. 【转载】Cmd Markdown 公式指导手册

    目录 Cmd Markdown 公式指导手册 一.公式使用参考 1.如何插入公式 2.如何输入上下标 3.如何输入括号和分隔符 4.如何输入分数 5.如何输入开方 6.如何输入省略号 7.如何输入矢量 ...

  6. vim锁定,不能动

    在vim中 ctrl+s是锁屏命令, ctrl+q是解锁

  7. C++获取驱动盘句柄

    转载:https://www.cnblogs.com/sherlock-merlin/p/10792116.html     https://univasity.iteye.com/blog/8052 ...

  8. Linux:vi & vim(待学)

    VI编辑器_终端编辑器 目标 vi简介 打开和新建文件 三种工作模式 常用命令查询 1 简介 1.1 学习vi的目的 在工作中, 要对 服务器上的 文件进行 简单 的修改, 可以使用 ssh 登录到远 ...

  9. 【PAT甲级】1007 Maximum Subsequence Sum (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

  10. windows下代码规范检测工具sonarqube安装与使用,含与maven的结合

    一.首先下载sonarqube   地址 : https://www.sonarqube.org/downloads/   (最新版本支持java11+,博主下载支持java8的版本7.7), 下载S ...