• 【PTA-训练day2】L2-013 红色警报 + L1-006 连续因子


    L2-013 红色警报 - 并查集 

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

    思路:

    并查集

    • 先将图联通 用父节点==自己 统计连通块数cnt
    • 输入k个询问 每次要删除的点x标记为true
    • 然后将非该点x的点连通 不访问已经打上标记的点
    • 再次统计连通块数res
    • 如果res>cnt+1 (因为被删除的x点也算一个连通块 如果删掉x的res比cnt+1大 说明图的连通性改变
    • 此时更新图 也就是cnt=res
    1. import java.util.*;
    2. public class Main
    3. {
    4. static int N=510,M=5001;
    5. static int[] f=new int[N];
    6. static int[][] e=new int[M][2];
    7. static boolean[] st=new boolean[N];
    8. static void init(int n)
    9. {
    10. for(int i=0;i
    11. }
    12. static int find(int x)
    13. {
    14. if(x!=f[x]) f[x]=find(f[x]);
    15. return f[x];
    16. }
    17. static void unite(int a,int b)
    18. {
    19. int x=find(a);
    20. int y=find(b);
    21. if(x!=y) f[x]=y;
    22. }
    23. public static void main(String[] args)
    24. {
    25. Scanner sc=new Scanner(System.in);
    26. int n=sc.nextInt(),m=sc.nextInt();
    27. init(n);
    28. //把每两个点连接起来
    29. for(int i=0;i
    30. {
    31. e[i][0]=sc.nextInt();
    32. e[i][1]=sc.nextInt();
    33. unite(e[i][0],e[i][1]);
    34. }
    35. int cnt=0;//连通块的数量
    36. for(int i=0;iif(f[i]==i) cnt++;
    37. int k,t;
    38. k=sc.nextInt();
    39. t=k;
    40. while(k-->0)
    41. {
    42. init(n);
    43. int x=sc.nextInt();
    44. st[x]=true; //把要删的点标记出来 然后把没有被标记的点联通起来 也就相当于删除已标记的点
    45. for(int i=0;i
    46. {
    47. int a=e[i][0],b=e[i][1];
    48. if(st[a]||st[b]) continue; //如果一条边里有一个点被标记 则不连接
    49. if(x!=a&&x!=b) unite(a,b); //如果两点都不是要删除的点 则连接
    50. }
    51. int res=0;
    52. for(int i=0;iif(f[i]==i) res++;
    53. //因为删掉该城市 这个城市也是一个单独的连通块 所以当前连通块>之前连通块+这个城市 说明删掉该城市 会改变图的连通性
    54. if(res>cnt+1) System.out.printf("Red Alert: City %d is lost!\n",x);
    55. else System.out.printf("City %d is lost.\n",x);
    56. cnt=res;//更新连通块
    57. }
    58. if(t==n) System.out.printf("Game Over.");
    59. }
    60. }

     

    L1-005 考试座位号 - 15

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

    1. #include
    2. using namespace std;
    3. struct stu
    4. {
    5. string id;
    6. int a,b;
    7. }p[1010];
    8. int main()
    9. {
    10. int n,k;
    11. cin>>n;
    12. for(int i=0;i>p[i].id>>p[i].a>>p[i].b;
    13. cin>>k;
    14. while(k--)
    15. {
    16. int x,idx=0;
    17. cin>>x;
    18. for(int i=0;i
    19. if(x==p[i].a)
    20. {
    21. idx=i;
    22. break;
    23. }
    24. cout<" "<
    25. }
    26. }

    L1-006 连续因子 - 20

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

    思路:

    • 两个循环 第一个控制首因子
    • 第二个嵌套在第一个循环里 连乘 一旦不能被n整除则跳出 
    • 每次跳出内部循环 更新最大连续因子长度
    • 最后特判素数情况——只有1个因子 即它自己
    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. int cnt,s,max=0,idx=0;
    9. for(int i=2;i//i为首个因子
    10. {
    11. s=1;
    12. cnt=0;
    13. for(int j=i;j
    14. {
    15. s*=j;
    16. if(n%s!=0) //一旦不连续跳出
    17. {
    18. break;
    19. }
    20. cnt++;
    21. }
    22. if(max//更新最长的长度
    23. {
    24. max=cnt;
    25. idx=i;
    26. }
    27. }
    28. if(max==0) System.out.printf("1\n%d",n); //素数情况
    29. else
    30. {
    31. System.out.println(max);
    32. for(int i=0;i
    33. {
    34. if(i!=0) System.out.print("*");
    35. System.out.printf("%d",idx++);
    36. }
    37. }
    38. }
    39. }

     

    L1-008 求整数段和 - 10

    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. int a=sc.nextInt(),b=sc.nextInt();
    8. int cnt=0,sum=0;
    9. for(int i=a;i<=b;i++)
    10. {
    11. cnt++;
    12. System.out.printf("%5d",i);
    13. sum+=i;
    14. if(cnt%5==0) System.out.println();
    15. }
    16. if(cnt!=5) System.out.println();
    17. System.out.print("Sum = "+sum);
    18. }
    19. }

  • 相关阅读:
    Qml使用cpp文件的信号槽
    最全前端面试总结(2)map/reduce
    系统性考量【复盘】这件事儿
    iconfont使用
    2022年最新宁夏道路运输安全员模拟真题题库及答案
    触摸TP,gt9xx调试分享
    Vertica 向 GBase8a 迁移指南之LONG VARBINARY 数据类型
    【人工智能】机器学习入门之监督学习(一)有监督学习
    jieba分词
    Web 自动化神器 TestCafe—页面基本操作篇
  • 原文地址:https://blog.csdn.net/weixin_61639349/article/details/127652816