本文主要是介绍寒假刷题第十天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PTA甲级
1060 Are They Equal
# 测试点5错误
l = input().split()
n = int(l.pop(0))
a = '{:e}'.format(float(l[0]))
b = '{:e}'.format(float(l[1]))
idx , idx1 = a.index('e') , b.index('e')
x , y = int(a[idx + 1:]) + 1 if a[0] != '0' else int(a[idx + 1:]) , int(b[idx + 1:]) + 1 if b[0] != '0' else int(b[idx + 1:])
a = '0.' + a.replace('.' , '')
b = '0.' + b.replace('.' , '')
xa , xb = '*10^' + str(x) , '*10^' + str(y)
a , b = a[:min(idx - 1 , n + 2)] , b[:min(idx1 - 1 , n + 2)]
a += '0' * (n - len(a) + 2)
b += '0' * (n - len(b) + 2)
a += xa
b += xb
if a == b:print("YES" , a)
else:print("NO" , a , b)
#include<iostream>
using namespace std;
int getC(string x){ //返回.的indexint c = 0;while (c < x.size()&&x[c]!='.')c++;return c;
}
string floatToSpecial(string x,int k){int c = getC(x);//使用substr(index)去除最前面多余的0int significant = 0;if(c>1){while(x[significant]=='0'){significant++;}if(significant==c){significant--;}x = x.substr(significant);}c = getC(x);//使用substr(0,l)去除小数点后面多余的0int index = x.size() - 1, l = 0;while(c<index&&x[index]=='0'){index--;l++;}x = x.substr(0, x.size() - l);c = getC(x);int zhishu = c;string pre = "";if(x[0]=='0'){c++;zhishu = 0;while(c<x.size()&&x[c]=='0'){c++;zhishu--;}for (int i = 0; i < k && c < x.size();i++,c++){pre += x[c];}}else if(c<k&&c>0&&x[0]!='0'){for (int i = 0; i < k+1 && i < x.size();i++)if(x[i]!='.'){pre += x[i];}}else if(c>=k){for (int i = 0; i < k && i < x.size(); i++){pre += x[i];}} //不足k位的话末尾补0int num = k - pre.size();for (int i = 0; i < num;i++){pre += '0';}return "0." + pre + "*10^" + to_string(zhishu);
}
int main(){string a, b;int k;cin >> k >> a >> b;string an1 = floatToSpecial(a, k), ans2 = floatToSpecial(b, k);int flag = an1 == ans2;if(flag){cout << "YES" << " " << an1;}else{cout << "NO" << " " << an1 << " " << ans2;}return 0;
}
1063 Set Similarity
可以使用python的交集和并集
d = {}
n = int(input())
for i in range(1 , n + 1):l = list(map(int , input().split()))l.pop(0)d[i] = l
for _ in range(int(input())):a , b = map(int , input().split())nt = len(set(d[a] + d[b]))nc = len(set(d[a]) & set(d[b]))res = f'{(nc / nt) * 100:.1f}%'print(res)
1064 Complete Binary Search Tree
二叉搜索树中序遍历是有序的
完全二叉树从1开始编号,之后左孩子为2 * x
#include<iostream>
#include<algorithm>using namespace std;const int N = 1010;
// cbt完全二叉树 左儿子2*x
// 二叉搜索树 中序遍历 有序的
// cbt对应二叉搜索树
int n;
int in[N] , level[N] , k = 0;void dfs(int u)
{if(2 * u <= n) dfs(2 * u); // 左孩子level[u] = in[k ++];if(2 * u + 1 <= n) dfs(2 * u + 1);// 右孩子
}int main()
{cin >> n;for(int i = 0;i < n;i ++)cin >> in[i];sort(in , in + n);dfs(1);for(int i = 1;i <= n;i ++){if(i != 1) cout << " ";cout << level[i];}return 0;
}
这篇关于寒假刷题第十天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!