Skip to content

Instantly share code, notes, and snippets.

@ncaq
Created December 1, 2016 03:57
Show Gist options
  • Save ncaq/5aa33b95e10e9f71d7e438262aa3b14d to your computer and use it in GitHub Desktop.
Save ncaq/5aa33b95e10e9f71d7e438262aa3b14d to your computer and use it in GitHub Desktop.
2014-11に記述,constexprのsqrtやhypotを実装したかったらしい,実際にどういう問題を解いてたのかはよくわからん
// -*- flycheck-clang-language-standard : "c++14"; -*-
// clang++ -std=c++14 -stdlib=libc++
// clang version 3.5.0 (tags/RELEASE_350/final)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <numeric>
template<typename T>
constexpr T sqrt_1(const T s, const T acc, const T prev)
{
return (prev == acc) ? acc :
sqrt_1(s, (acc + (s / acc)) / 2, acc);
}
template<typename T>
constexpr T sqrt(const T s)
{
return sqrt_1<T>(s, s / 2.0, 0);
}
constexpr double trianglearea(const double a, const double b, const double c)
{
const double perimeter = (a + b + c);
const double s = perimeter / 2.0;
const double max_side = std::max({a, b, c});
return (max_side < (perimeter - max_side)) ?
::sqrt(s * (s - a) * (s - b) * (s - c)) :
0;
}
template<typename T>
constexpr T hypot(const T a, const T b)
{
return ::sqrt(std::pow(a, 2) + std::pow(b, 2));
}
constexpr double dist(const double x1, const double y1, const double x2, const double y2)
{
return ::hypot<double>(x1 - x2, y1 - y2);
}
constexpr double maxdist(const double x[], const double y[], int n) {
double result = 0;
for(int i = 0; i < n + 1; ++i)
{
result += dist(x[i], y[i], x[i + 1], y[i + 1]);
}
return result;
}
int main() {
int i, n = 10;
double x[100], y[100];
double result;
for(i = 0; i < n; i++) {
scanf("%lf %lf", &x[i], &y[i]);
}
result = maxdist(x, y, n);
printf("%0.8f\n", result);
return 0;
}
@ncaq
Copy link
Author

ncaq commented Jan 13, 2017

これ見なおして思ったけど実装しているのmaxdistではなくてsumdistになってしまっているのではないか

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment