• 团体程序设计天梯赛-练习集(四)(L1-020 帅到没朋友 (20分))


    L1-020 帅到没朋友 (20分)

    https://pintia.cn/problem-sets/994805046380707840/problems/994805117167976448

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    setw()及setfill()使用方法

     1 cout<<setw(5)<<setfill('0')<<right<<temp;//输出5位,右对齐,不足补0 

    1、setw(int n)只是对直接跟在<<后的输出数据起作用,而在之后的<<需要在之前再一次使用setw;
    2、n是在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中左对齐输出;
    3、可以使用setfill('char x')使用x来填充空下的空格;示例如下:
     1 #include <iostream>
     2 #include <iomanip>
     3 using namespace std;
     4 int main()
     5 {
     6     char *str="hello";
     7     cout<<setw(10)<<setfill('#')<<left<<str<<endl;
     8     cout<<setw(10)<<setfill('#')<<right<<str<<endl;
     9         cout<<setw(10)<<setfill('#')<<str<<endl;
    10     system("pause");
    11     return 0; 
    12 }

    输出结果:

     

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    本题代码:

    (61ms)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <iomanip>
     5 using namespace std;
     6 int main()
     7 {
     8     int n,k,temp,a[100001]={0},m,flag=1;
     9     cin>>n;
    10     while(n--)
    11     {
    12         cin>>k;
    13         for(int i=0;i<k;i++) 
    14         {
    15             cin>>temp;
    16             if(k!=1) a[temp]=1;  //有朋友 
    17         }
    18     }
    19         cin>>m;
    20         for(int i=0;i<m;i++)
    21         {
    22             cin>>temp;
    23             if(a[temp]==0) 
    24             {
    25                 if(!flag) cout<<" ";
    26                 cout<<setw(5)<<setfill('0')<<right<<temp;
    27                 a[temp]=1;
    28                 flag=0;
    29             }
    30         }
    31         if(flag) cout<<"No one is handsome
    ";
    32         return 0;
    33 }

    加一段代码

     1 std::ios::sync_with_stdio(false);2 cin.tie(NULL); 

    (20ms)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <iomanip>
     5 using namespace std;
     6 int main()
     7 {
     8     std::ios::sync_with_stdio(false);
     9         cin.tie(NULL);
    10     int n,k,temp,a[100001]={0},m,flag=1;
    11     cin>>n;
    12     while(n--)
    13     {
    14         cin>>k;
    15         for(int i=0;i<k;i++) 
    16         {
    17             cin>>temp;
    18             if(k!=1) a[temp]=1;  //有朋友 
    19         }
    20     }
    21         cin>>m;
    22         for(int i=0;i<m;i++)
    23         {
    24             cin>>temp;
    25             if(a[temp]==0) 
    26             {
    27                 if(!flag) cout<<" ";
    28                 cout<<setw(5)<<setfill('0')<<right<<temp;//输出5位,右对齐,不足补0
    29                 a[temp]=1;
    30                 flag=0;
    31             }
    32         }
    33         if(flag) cout<<"No one is handsome
    ";
    34         return 0;
    35 } 
    天晴了,起飞吧
  • 相关阅读:
    在HTML中使用JavaScript
    七层网络模型
    JS执行机制
    继承
    变量作用域
    跨域
    ES6-Promise
    回调函数
    2019.3.9笔试
    CSS3新特性
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/12110748.html
Copyright © 2020-2023  润新知