• 【PTA-训练day7】L2-019 悄悄关注 + L1-027 出租


    L2-019 悄悄关注 - 哈希表

    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. int n=sc.nextInt();
    8. String[] name=sc.nextLine().split(" ");
    9. Set st=new HashSet<>();
    10. for(int i=0;i
    11. int k=sc.nextInt();
    12. double sum=0;
    13. Set res=new TreeSet<>();
    14. Mapmp=new HashMap<>();
    15. int temp=k;
    16. while(k-->0)
    17. {
    18. String x=sc.next();
    19. int t=sc.nextInt();
    20. mp.put(x,t);
    21. sum+=t;
    22. }
    23. sum/=temp;
    24. for(Map.Entry x:mp.entrySet())
    25. {
    26. if(!st.contains(x.getKey())&&x.getValue()>sum) res.add(x.getKey());
    27. }
    28. if(res.size()==0) System.out.print("Bing Mei You");
    29. else for(String x:res) System.out.println(x);
    30. }
    31. }

    2、c++

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int n;
    6. cin>>n;
    7. setst;
    8. for(int i=0;i
    9. {
    10. string s;
    11. cin>>s;
    12. st.insert(s);
    13. }
    14. int k,temp;
    15. double sum=0;
    16. cin>>k;
    17. temp=k;
    18. mapint>mp;
    19. while(k--)
    20. {
    21. string x;
    22. int t;
    23. cin>>x>>t;
    24. mp[x]=t;
    25. sum+=t;
    26. }
    27. sum/=temp;
    28. setres;
    29. for(auto x:mp)
    30. if(!st.count(x.first)&&x.second>sum) res.insert(x.first);
    31. if(res.size()==0) cout<<"Bing Mei You";
    32. else for(auto x:res) cout<
    33. }

    L1-027 出租 - 20

    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. TreeSet st=new TreeSet<>(Comparator.reverseOrder());
    9. for(int i=0;i
    10. System.out.print("int[] arr = new int[]{");
    11. int cnt=0;
    12. Map mp=new HashMap<>();
    13. for(char x:st)
    14. {
    15. System.out.print(x);
    16. mp.put(x,cnt);
    17. cnt++;
    18. if(cnt!=st.size()) System.out.print(",");
    19. }
    20. System.out.println("};");
    21. System.out.print("int[] index = new int[]{");
    22. int res=0;
    23. for(int i=0;i
    24. {
    25. res++;
    26. System.out.print(mp.get(s.charAt(i)));
    27. if(res!=s.length()) System.out.print(",");
    28. }
    29. System.out.print("};");
    30. }
    31. }

     

    L1-025 正整数A+B - 15

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

    要注意 不能用split按空格分割 

    因为有多个空格存在时 比如123 123 kkk

    如果按split分割 则就是123+123=246

    但正确答案应该是123+?=?

    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.nextLine();
    8. String[] a=new String[2];
    9. a[0]=s.substring(0,s.indexOf(" "));
    10. a[1]=s.substring(s.indexOf(" ")+1);
    11. boolean f1=false,f2=false;
    12. if(isDigit(a[0])) f1=true;
    13. if(isDigit(a[1])) f2=true;
    14. if(f1&&f2)
    15. {
    16. Integer s1=Integer.parseInt(a[0]);
    17. Integer s2=Integer.parseInt(a[1]);
    18. System.out.print(a[0]+" + "+a[1]+" = "+(s1+s2));
    19. }
    20. else if(!f1&&f2) System.out.print("? + "+a[1]+" = ?");
    21. else if(f1&&!f2) System.out.print(a[0]+" + ? = ?");
    22. else System.out.print("? + ? = ?");
    23. }
    24. public static boolean isDigit(String s)
    25. {
    26. if(s.charAt(0)=='0'||(s.length()>3&&!"1000".equals(s))) return false;
    27. for(int i=0;i
    28. if((s.charAt(i)-'0')<0||(s.charAt(i)-'0')>9)
    29. return false;
    30. return true;
    31. }
    32. }

     

    L1-028 判断素数 - 10

    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. while(n-->0)
    9. {
    10. int x=sc.nextInt();
    11. if(isprime(x)) System.out.println("Yes");
    12. else System.out.println("No");
    13. }
    14. }
    15. public static boolean isprime(int n)
    16. {
    17. if(n==0||n==1) return false;
    18. for(int i=2;i<=n/i;i++) if(n%i==0) return false;
    19. return true;
    20. }
    21. }

  • 相关阅读:
    电脑版剪映怎么倒放?
    Java 8 新特性 Ⅰ
    html中登录按钮添加回车键登录
    《安富莱嵌入式周报》第283期:全开源逆向“爆破”硬件工具,Linux内核6.1将正式引入RUST语言,I3C培训教程,80款市场成熟的电感式位置传感器设计
    NoSQL之Redis配置与优化(一)
    PDF图片提取的方法有什么?这个方法1分钟提取完毕
    SQL注入漏洞(原理篇)
    QCSPCChart for Java R3x0 Crack
    Matlab|基础知识总结一
    【AI视野·今日Robot 机器人论文速览 第八十四期】Thu, 7 Mar 2024
  • 原文地址:https://blog.csdn.net/weixin_61639349/article/details/127887375