2019-09-07keep笔试

image.png

image.png
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1111111;
 
int n;
int x[maxn];
long long m;
 
bool C(int mid) {
    long long cnt = 0;
    for(int i = 0; i < n; i++)
        cnt += x+n-lower_bound(x+i+1, x+n, x[i]+mid);
    return cnt <= m/2;    
}
int main() {
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; i++)
            scanf("%d", &x[i]);
        sort(x, x+n);
        m = n*(n-1)/2;      
        int L = 0, R = *max_element(x, x+n);
        while(R-L > 1) {
            int mid = (L+R)/2;
            if(C(mid)) R = mid;
            else L = mid;
        }
        printf("%d\n", L);
 
    }
    return 0;
}

参考https://blog.csdn.net/hhhhhhj123/article/details/47866399

image.png

image.png

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

bool fun(const std::string &str){
    if(str.size() < 8){
        return false;
    }
    if(str[0] >= '0' && str[0] <= '9'){
        return false;
    }
    int num = 0;
    int daxie = 0;
    int xiaoxie = 0;
    for(int i = 0 ; i < str.size() ; ++i){
        if(str[i] >= '0' && str[i] <= '9'){
            num = 1;
        }else if(str[i] >= 'a' && str[i] <= 'z'){
            xiaoxie = 1;
        }else{
            daxie = 1;
        }
        if(num + daxie + xiaoxie >=2){
            return true;
        }
    }
    return false;
}

int main(){
    int n;
    std::cin>>n;
    std::vector<std::string> mima;
    for(int i = 0 ; i < n ; ++i){
        std::string item;
        std::cin>>item;
        mima.push_back(item);
    }
    for(auto &item : mima){
        if(fun(item)){
            std::cout<<"YES"<<std::endl;
        } else{
            std::cout<<"NO"<<std::endl;
        }
    }
}
image.png

image.png
#include <vector>
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

void LineFitLeastSquares(double *data_x, double *data_y, int data_n, vector<double> &vResult)
{
    double A = 0.0;
    double B = 0.0;
    double C = 0.0;
    double D = 0.0;
    double E = 0.0;
    double F = 0.0;

    for (int i=0; i<data_n; i++)
    {
        A += data_x[i] * data_x[i];
        B += data_x[i];
        C += data_x[i] * data_y[i];
        D += data_y[i];
    }

    // 计算斜率a和截距b
    double a, b, temp = 0;
    if( temp = (data_n*A - B*B) )// 判断分母不为0
    {
        a = (data_n*C - B*D) / temp;
        b = (A*D - B*C) / temp;
    }
    else
    {
        a = 1;
        b = 0;
    }

    // 计算相关系数r
    double Xmean, Ymean;
    Xmean = B / data_n;
    Ymean = D / data_n;

    double tempSumXX = 0.0, tempSumYY = 0.0;
    for (int i=0; i<data_n; i++)
    {
        tempSumXX += (data_x[i] - Xmean) * (data_x[i] - Xmean);
        tempSumYY += (data_y[i] - Ymean) * (data_y[i] - Ymean);
        E += (data_x[i] - Xmean) * (data_y[i] - Ymean);
    }
    F = sqrt(tempSumXX) * sqrt(tempSumYY);

    double r;
    r = E / F;

    vResult.push_back(a);
    vResult.push_back(b);
    vResult.push_back(r*r);
}

int main(){
    vector<double> veca,vecb;
    int a=0,b=0;
    while (cin>>a>>b){
        veca.push_back(a);
        vecb.push_back(b);
    }
    vector<double> res;
    LineFitLeastSquares(&veca[0],&vecb[0],veca.size(),res);
    //printf("%f\t%f",res[0],res[1]);
    std::cout<<res[0]<<"\t"<<res[1];
}

需要反思部分,关于函数的输入输出一定要重视。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容