寒假刷题第十天

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
using namespace std;
int getC(string x){  //返回.的index
    int c = 0;
    while (c < x.size()&&x[c]!='.')
        c++;
    return c;
}
string floatToSpecial(string x,int k){
    int c = getC(x);
    //使用substr(index)去除最前面多余的0
    int significant = 0;
    if(c>1){
        while(x[significant]=='0'){
            significant++;
        }
        if(significant==c){
            significant--;
        }
        x = x.substr(significant);
    }
    c = getC(x);
    //使用substr(0,l)去除小数点后面多余的0
    int index = x.size() - 1, l = 0;
    while(c0&&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位的话末尾补0
    int 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
#include

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;
}

你可能感兴趣的:(寒假刷题,算法,数据结构)