• 【PTA-训练day11】L2-023 图着色问题 + L1-039 古风排版


    目录

    L2-023 图着色问题 - dfs

    测试点3:图可能是不联通的 所以不能是dfs(1)

    L1-035 情人节 - 15

    L1-039 古风排版 - 20

    L1-040 最佳情侣身高差 - 10


    L2-023 图着色问题 - dfs

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

    思路:

    从点1出发 进行dfs

    dfs内 遍历和该点相连的其他点 如果颜色相同则返回false

    主函数里遍历1~n的点 一旦有重复的就跳出

    因为:图可能是不联通的,所以必须遍历1~n个点

    如果1~n个点都满足不重复 则输出Yes 否则输出No

    1. #include
    2. using namespace std;
    3. const int N=510;
    4. int n,m,k;
    5. bool g[N][N];
    6. bool st[N];
    7. int color[N];
    8. bool dfs(int u)
    9. {
    10. st[u]=true;
    11. for(int i=1;i<=n;i++) //遍历该点相关联的节点
    12. if(g[u][i])
    13. {
    14. if(color[u]==color[i]) return false;
    15. if(!st[i])
    16. if(!dfs(i)) return false;
    17. }
    18. return true;
    19. }
    20. int main()
    21. {
    22. cin>>n>>m>>k;
    23. for(int i=0;i
    24. {
    25. int a,b;
    26. cin>>a>>b;
    27. g[a][b]=g[b][a]=true;
    28. }
    29. int t;
    30. cin>>t;
    31. while(t--)
    32. {
    33. set<int>s;
    34. memset(color,0,sizeof color);
    35. memset(st,false,sizeof st);
    36. for(int i=1;i<=n;i++)
    37. {
    38. cin>>color[i];
    39. s.insert(color[i]);
    40. }
    41. if(s.size()!=k)
    42. {
    43. cout<<"No"<
    44. continue;
    45. }
    46. int i;
    47. for(i=1;i<=n;i++)
    48. if(!dfs(i)) break; //一旦有重复的点 则跳出
    49. if(i-1==n) cout<<"Yes"<//如果上面循环不跳出 说明没有重复的点
    50. else cout<<"No"<
    51. }
    52. return 0;
    53. }

    测试点3:图可能是不联通的 所以不能是dfs(1)

    1. #include
    2. using namespace std;
    3. const int N=510;
    4. int n,m,k;
    5. bool g[N][N];
    6. bool st[N];
    7. int color[N];
    8. bool dfs(int u)
    9. {
    10. st[u]=true;
    11. for(int i=1;i<=n;i++) //遍历该点相关联的节点
    12. if(g[u][i])
    13. {
    14. if(color[u]==color[i]) return false;
    15. if(!st[i])
    16. if(!dfs(i)) return false;
    17. }
    18. return true;
    19. }
    20. int main()
    21. {
    22. cin>>n>>m>>k;
    23. for(int i=0;i
    24. {
    25. int a,b;
    26. cin>>a>>b;
    27. g[a][b]=g[b][a]=true;
    28. }
    29. int t;
    30. cin>>t;
    31. while(t--)
    32. {
    33. set<int>s;
    34. memset(color,0,sizeof color);
    35. memset(st,false,sizeof st);
    36. for(int i=1;i<=n;i++)
    37. {
    38. cin>>color[i];
    39. s.insert(color[i]);
    40. }
    41. if(s.size()!=k)
    42. {
    43. cout<<"No"<
    44. continue;
    45. }
    46. if(dfs(1)) cout<<"Yes"<
    47. else cout<<"No"<
    48. }
    49. return 0;
    50. }

    L1-035 情人节 - 15

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int cnt=0;
    6. string s,x2,x14;
    7. while(true)
    8. {
    9. cin>>s;
    10. if(s==".") break;
    11. cnt++;
    12. if(cnt==2) x2=s;
    13. if(cnt==14) x14=s;
    14. }
    15. if(cnt<2) cout<<"Momo... No one is for you ...";
    16. else if(cnt<14) cout<" is the only one for you...";
    17. else cout<" and "<" are inviting you to dinner...";
    18. }

    L1-039 古风排版 - 20

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. char ch[101][101];
    6. int n,t,m,cnt=0;
    7. cin>>n;
    8. cin.get();//接收回车 大坑
    9. string s;
    10. getline(cin,s);
    11. int l=s.size();
    12. for(int i=0;;i++)
    13. if((l+i)%n==0)
    14. {
    15. t=i;
    16. break;
    17. }
    18. l+=t,m=l/n;
    19. for(int j=m-1;j>=0;j--)
    20. for(int i=0;i
    21. {
    22. if(s[cnt]=='\0') ch[i][j]=' '; //注意最后一行最后是'/0'
    23. else ch[i][j]=s[cnt++];
    24. }
    25. for(int i=0;i
    26. {
    27. for(int j=0;j
    28. {
    29. cout<
    30. }
    31. cout<
    32. }
    33. }

    L1-040 最佳情侣身高差 - 10

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int n;
    6. cin>>n;
    7. while(n--)
    8. {
    9. char c;
    10. double h;
    11. cin>>c>>h;
    12. if(c=='M') printf("%.2f\n",h/1.09);
    13. else printf("%.2f\n",h*1.09);
    14. }
    15. }

  • 相关阅读:
    【Flutter】shape 属性 ShapeBorder,形状
    自定义view实战(9):多点触控扇形图
    结构体和类的区别详细讲解
    【672. 灯泡开关 Ⅱ】
    Spring Cloud 整合 nacos 实现动态配置中心
    NGINX源码之:location
    Centos7 |Canal Admin搭建Canal Server集群|
    linux安装mysql8
    【IO多路转接】poll&epoll
    DataFrame(11):数据转换——map()函数的使用
  • 原文地址:https://blog.csdn.net/weixin_61639349/article/details/128084989