【PTA-训练day8】L2-020 功夫传人 + L1-032 Left-pad

L2-020 功夫传人 - dfs

PTA | 程序设计类实验辅助教学平台

思路

由题意可知,该题为一棵树,要计算根节点的power值 dfs即可

10 18.0 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
#include <bits/stdc++.h>
using namespace std;

const int N=1e5+10;
vector<vector<int>> g;
int n;
double z,r,sum=0;
bool st[N];

void dfs(int u,double pow)
{
    if(st[u])
    {
        sum+=pow*g[u][0];
        return;
    }
    for(int i=0;i<g[u].size();i++)
        dfs(g[u][i],pow*(1-r/100));
}

int main()
{
    cin>>n>>z>>r;
    g.resize(n);
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        if(k==0)
        {
            int x;
            cin>>x;
            st[i]=true;
            g[i].push_back(x);
        }
        else 
        {
            for(int j=0;j<k;j++)
            {
                int x;
                cin>>x;
                g[i].push_back(x);
            }
        }
    }
    dfs(0,z);
    cout<<(int)sum;
}

 

L1-030 一帮一 - 15

PTA | 程序设计类实验辅助教学平台

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String[][] mp=new String[n][2];
        for(int i=0;i<n;i++)
        {
            mp[i][0]=sc.next();
            mp[i][1]=sc.next();
        }
        int[] f=new int[n];
        for(int i=0;i<n/2;i++)
            for(int j=n-1;j>=n/2;j--)
                if(!(mp[i][0].equals(mp[j][0]))&&f[j]==0)
                {
                    System.out.println(mp[i][1]+" "+mp[j][1]);
                    f[j]=1;
                    break;
                }
    }
}

L1-031 到底是不是太胖了 - 10

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0)
        {
            double h=sc.nextDouble(),w=sc.nextDouble();
            double p=(h-100)*0.9*2;
            if(Math.abs(p-w)<p*0.1) System.out.println("You are wan mei!");
            else if(w>p) System.out.println("You are tai pang le!");
            else System.out.println("You are tai shou le!");
        }
    }
}

L1-032 Left-pad - 20

PTA | 程序设计类实验辅助教学平台

sc.nextLine()         //接收回车

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String ch=sc.next();
        sc.nextLine();//接收回车
        String s=sc.nextLine();
        int len=s.length();
        if(n>=len)
        {
            for(int i=0;i<n-len;i++) System.out.print(ch);
            System.out.print(s);
        }
        else System.out.print(s.substring(len-n));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值