• 【PTA-训练day6】L2-016 愿天下有情人都是失散多年的兄妹+ L1-011 帅到没朋友


    L2-016 愿天下有情人都是失散多年的兄妹 - dfs

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

    思路:

    先记录每个人的父母 然后k组查询

    分别用dfs搜索两个人的族谱 五服之内出现的人都标记

    如果搜索第二个人时 出现已标记的人 说明这两个有共同的祖先

    1. #include
    2. using namespace std;
    3. const int N=1e5+10;
    4. bool st[N];
    5. bool f=false;
    6. char sex[N];
    7. vector<int> v[N];
    8. void dfs(int u,int d)
    9. {
    10. if(d>=5) return; //五服之外就return
    11. st[u]=true; //把该点标记
    12. for(int i=0;isize();i++) //遍历该点的所有长辈 如果有的长辈被标记过 说明两个人有共同的祖先
    13. {
    14. if(st[v[u][i]]) f=true;
    15. dfs(v[u][i],d+1);
    16. }
    17. }
    18. int main()
    19. {
    20. int n;
    21. cin>>n;
    22. for(int i=0;i
    23. {
    24. int id;
    25. cin>>id;
    26. char c;
    27. cin>>c;
    28. sex[id]=c;
    29. int fa,ma;
    30. cin>>fa>>ma;
    31. if(~fa)
    32. {
    33. sex[fa]='M';
    34. v[id].push_back(fa);
    35. }
    36. if(~ma)
    37. {
    38. sex[ma]='F';
    39. v[id].push_back(ma);
    40. }
    41. }
    42. int k;
    43. cin>>k;
    44. while(k--)
    45. {
    46. int x,y;
    47. cin>>x>>y;
    48. f=false;
    49. memset(st,false,sizeof st); //每次查询 st数组都要清空
    50. dfs(x,0);
    51. dfs(y,0);
    52. if(sex[x]==sex[y]) cout<<"Never Mind"<
    53. else{
    54. if(!f) cout<<"Yes"<
    55. else cout<<"No"<
    56. }
    57. }
    58. }

    L1-017 到底有多二 - 15

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

    1. import java.util.*;
    2. public class Main
    3. {
    4. public static void main(String[] args)
    5. {
    6. Scanner sc=new Scanner(System.in);
    7. String s=sc.next();
    8. int cnt=0,n=s.length();
    9. for(int i=0;iif(s.charAt(i)=='2') cnt++;
    10. double bei=1;
    11. if((s.charAt(n-1)-'0')%2==0) bei*=2; //因为判断负数时n的值会改变 所以这个要放在前面
    12. if(s.charAt(0)=='-')
    13. {
    14. bei*=1.5;
    15. n-=1;
    16. }
    17. System.out.printf("%.2f%%",cnt*1.0/n*bei*100);
    18. }
    19. }

    L1-018 大笨钟 - 10

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

    1、java版

    接收字符的过程比较麻烦

    1. import java.util.*;
    2. public class Main
    3. {
    4. public static void main(String[] args)
    5. {
    6. Scanner sc=new Scanner(System.in);
    7. String[] num=sc.next().split(":");
    8. int hh=Integer.parseInt(num[0]),mm=Integer.parseInt(num[1]);
    9. if(hh>=0&&hh<=11||hh==12&&mm==0)
    10. {
    11. System.out.printf("Only %02d:%02d. Too early to Dang.",hh,mm);
    12. }else
    13. if(mm==0)
    14. {
    15. int n=hh-12;
    16. while(n-->0) System.out.print("Dang");
    17. }else{
    18. int n=hh-11;
    19. while(n-->0) System.out.print("Dang");
    20. }
    21. }
    22. }

    2、c++版 

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int hh,mm;
    6. char ch;
    7. cin>>hh>>ch>>mm;
    8. if(hh>=0&&hh<=11||hh==12&&mm==0) printf("Only %02d:%02d. Too early to Dang.",hh,mm);
    9. else if(mm==0)
    10. {
    11. int n=hh-12;
    12. while(n--) cout<<"Dang";
    13. }
    14. else{
    15. int n=hh-11;
    16. while(n--) cout<<"Dang";
    17. }
    18. }

    L1-020 帅到没朋友 - 20

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

    1、java版 - 最后一个点超时 喜闻乐见

    思路:

    • 把朋友圈里出现过的id都存入set里,其中要注意如果朋友圈只有一个人,这种情况是不存入朋友圈的
    • 因为输出时,不能重复输出id,且要保证输出顺序,所以用LinkedHashSet
    • 之前set中出现过的id就不输出,否则存入LinkedHashSet的res容器里
    • 最后输出我用了2种方法:一个是把set转化为string数组;一个是直接输出set

     set转化为string数组版:

    1. import java.util.*;
    2. public class Main
    3. {
    4. public static void main(String[] args)
    5. {
    6. Scanner sc=new Scanner(System.in);
    7. int n=sc.nextInt();
    8. Set st=new HashSet<>();
    9. Set res=new LinkedHashSet<>();
    10. while(n-->0)
    11. {
    12. int k=sc.nextInt();
    13. if(k==1) //如果朋友圈就他一个人 说明答案也要输出他 也就是不存入set
    14. {
    15. String s=sc.next();
    16. continue;
    17. }
    18. while(k-->0)
    19. {
    20. String s=sc.next();
    21. st.add(s);
    22. }
    23. }
    24. int m=sc.nextInt();
    25. int cnt=0;
    26. for(int i=0;i
    27. {
    28. String t=sc.next();
    29. if(st.contains(t)) continue;
    30. cnt++;
    31. res.add(t);
    32. }
    33. if(cnt==0) System.out.print("No one is handsome");
    34. else
    35. {
    36. String[] ans=res.toArray(new String[]{}); //这里是把set转化为string数组
    37. for(int i=0;i
    38. {
    39. if(i!=0) System.out.print(" ");
    40. System.out.print(ans[i]);
    41. }
    42. }
    43. }
    44. }

     直接输出set版:

    1. import java.util.*;
    2. public class Main
    3. {
    4. public static void main(String[] args)
    5. {
    6. Scanner sc=new Scanner(System.in);
    7. int n=sc.nextInt();
    8. Set st=new HashSet<>();
    9. Set res=new LinkedHashSet<>();
    10. while(n-->0)
    11. {
    12. int k=sc.nextInt();
    13. if(k==1) //如果朋友圈就他一个人 说明答案也要输出他 也就是不存入set
    14. {
    15. String s=sc.next();
    16. continue;
    17. }
    18. while(k-->0)
    19. {
    20. String s=sc.next();
    21. st.add(s);
    22. }
    23. }
    24. int m=sc.nextInt();
    25. int cnt=0;
    26. for(int i=0;i
    27. {
    28. String t=sc.next();
    29. if(st.contains(t)) continue;
    30. cnt++;
    31. res.add(t);
    32. }
    33. if(cnt==0) System.out.print("No one is handsome");
    34. else
    35. {
    36. boolean f=true;
    37. for(Object x:res)
    38. {
    39. if(f)
    40. {
    41. System.out.print(x);
    42. f=false;
    43. }
    44. else System.out.print(" "+x);
    45. }
    46. }
    47. }
    48. }

    2、c++版

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int n;
    6. cin>>n;
    7. set st;
    8. while(n--)
    9. {
    10. int k;
    11. cin>>k;
    12. string s;
    13. if(k==1)
    14. {
    15. cin>>s;
    16. continue;
    17. }
    18. while(k--)
    19. {
    20. cin>>s;
    21. st.insert(s);
    22. }
    23. }
    24. int m,cnt=0;
    25. cin>>m;
    26. set res;
    27. while(m--)
    28. {
    29. string s;
    30. cin>>s; //要根据输入顺序输出
    31. if(!st.count(s)&&!res.count(s)) //如果朋友圈中无记录且答案不重时
    32. {
    33. if(cnt==0)cout<
    34. else cout<<" "<
    35. res.insert(s);
    36. cnt++;
    37. }
    38. }
    39. if(cnt==0) cout<<"No one is handsome";
    40. }

  • 相关阅读:
    SpringCloudAliBaba篇(二)之nacos集群部署
    【精句】k8s资源管理概述
    sklearn机器学习——day11
    【Python自然语言处理】使用SVM、随机森林法、梯度法等多种方法对病人罹患癌症预测实战(超详细 附源码)
    Feign源码解析6:如何集成discoveryClient获取服务列表
    00Hadoop数据仓库平台
    MODB:软体动物线粒体基因组数据库
    外贸客户来源的渠道有哪些?
    Vue 2.0——初识组件
    基于netmap的用户态协议栈(一)
  • 原文地址:https://blog.csdn.net/weixin_61639349/article/details/127711144