TopCoder - "TCHS SRM 1" 答案 及 代码

"TCHS SRM 1" 答案 及 代码

 

由于题目涉密以及英文, 不方便一一列出, 希望能坚持下去;

TopCoder具体网址:http://community.topcoder.com/


 


250-points: 雷达识别均值问题:

Code:

#include <string>
#include <vector>
#include <cstddef>

using namespace std;

class SpeedRadar {
public:
    double averageSpeed (int minLimit, int maxLimit, std::vector <int> readings);
};

double SpeedRadar::averageSpeed (int minLimit, int maxLimit, std::vector <int> readings){
	size_t cnt = readings.size();
	size_t lmtCnt = 0.1*cnt;
	double total(0.0);
	size_t infCnt(0);
	for (const auto& i : readings){
		if(i<minLimit || i>maxLimit){
			infCnt++;
			if(infCnt > lmtCnt){
				total = 0.0;
				break;
			}
		}else{
			total += i;
		}
	}
	return total/(cnt-infCnt);
}


500-points: 语言的字母规律:

Code:

#include <string>
#include <vector>
#include <array>
#include <numeric>
#include <cmath>
#include <limits>
#include <cstddef>

using namespace std;

class SymbolFrequency {
public:
	double language(vector <string> frequencies, vector <string> text);
};

double SymbolFrequency::language(vector <string> frequencies, vector <string> text){
	int asc(0); //ascii
	double cnt(0.0); //the count of alphabets
	double sum(0.0); //each language difference
	double result = std::numeric_limits<double>::max(); //result
	std::array<double, 26> alpText = {0};
	for(const auto& str : text){
		for(const auto& c : str){
			asc = static_cast<int>(c);
			++alpText[asc-97];
		}
	}
	cnt = std::accumulate(alpText.begin(), alpText.end(), 0);
	std::array<double, 26> alpFreq = {0};
	for(const auto& fre : frequencies){
		for(auto i=fre.cbegin(); i!=fre.cend(); i+=3){
			asc = static_cast<int>(*i);
			std::string fig = std::string(i+1, i+3);
			alpFreq[asc-97] = std::stod(fig)*cnt/100.0;
		}
		for(size_t i=0; i<26; ++i){
			sum += std::pow(std::abs(alpFreq[i] - alpText[i]), 2.0);
		}
		result = (sum < result) ? sum : result;
		sum = 0; //reset
	}
	return result;
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ManonLegrand

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值