CodeVs刷题攻略之Bronze

CodeVs刷题攻略之Bronze

2017.12.18 By gwj1139177410

0x01整数处理

  1. 最小数和最大数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main(){
    int n; cin>>n;
    int _max=-1, _min=0xfffffff;
    for(int i = 1; i <= n; i++){
    int x; cin>>x;
    _max = max(_max, x);
    _min = min(_min, x);
    }
    cout<<_min<<" "<<_max<<"\n";
    return 0;
    }
  2. 求和

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main(){
    int n; cin>>n;
    int sum = 0;
    for(int i = 1; i <= n; i++){
    int x; cin>>x;
    sum += x;
    }
    cout<<sum<<"\n";
    return 0;
    }

0x02浮点数处理

  1. 判断浮点数是否相等

    1
    2
    3
    4
    5
    6
    7
    8
    #include<iostream>
    using namespace std;
    int main(){
    float a, b; cin>>a>>b;
    if(a == b)cout<<"yes\n";
    else cout<<"no\n";
    return 0;
    }
  2. 保留两位小数

    1
    2
    3
    4
    5
    6
    7
    #include<cstdio>
    int main(){
    double x;
    scanf("%lf", &x);
    printf("%.2lf\n", x);
    return 0;
    }
  3. 机票打折

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include<iostream>
    using namespace std;
    int main(){
    int n; float m;
    cin>>n>>m;
    int t = n*m/10;
    if(t%10<=4)cout<<t-t%10<<"\n";
    else cout<<t-t%10+10<<"\n";
    return 0;
    }

0x03字符串处理

  1. 寻找子串位置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include<iostream>
    #include<string>
    using namespace std;
    int main(){
    string a, b;
    cin>>a>>b;
    cout<<a.find(b)+1<<"\n";
    return 0;
    }
  2. 单词翻转

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include<iostream>
    #include<string>
    using namespace std;
    int main(){
    string a[110];
    int i = 1;
    while(cin>>a[i])i++;
    while(i--)cout<<a[i]<<" ";
    return 0;
    }