algorithm is this integration - andstudy/forge GitHub Wiki

[http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=111307&format=html ๋ฌธ์ œ๋ณด๊ธฐ] [http://www.programming-challenges.com/pg.php?page=studenthome&threshold=0&perpage=8&pagenum=12&orderby=12 13์žฅ Summit Pages]

Mastojun

๋ฌธ์ œ ํ’€์ด

  • ๋ฐฑํˆฌ๋” ๋Œ€ํ•™๊ต 1ํ•™๋…„!
  • ์˜ค๋žซ๋งŒ์— ์ ๋ถ„ํ‘œ๊บผ๋‚ด๋“ค๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค
  • Programming-Challenges์—์„œ๋Š” WA, UVa์—์„œ๋Š” AC๋œจ๋„ค์š”.
  • ์†Œ์Šค๋Š” ๋ฏผ๋งํ•ฉ๋‹ˆ๋‹ค.
  • ๋Œ€๋ถ€๋ถ„์˜ ํ’€์ด๋Š” ์ข…์ด์™€ ๊ณ„์‚ฐ๊ธฐ๋กœ.

์†Œ์Šค ์ฝ”๋“œ

#include <stdio.h>

    int main()
    {
    	double in;
    
    	double s, d, r;
    
    	while( scanf("%lf", &in) >= 1 )
    	{
    		in *= in;
    		s = in*0.31514674363;
    		d = in*0.51129916633;
    		r = in*0.17355409004;
    
    		printf("%.3lf %.3lf %.3lf\n", s, d, r);
    	}
    
    	return 0;
    }

CynicJJ

  • wrong answer. ์™œ?

    • UVa์—์„œ๋Š” Accepted๋œจ๋„ค์š”! :D PC์ชฝ์ด ์ด์ƒํ•œ๋“ฏ...
      • ์šฐ์™•ใ…‹๊ตณใ…‹ ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค ^^
  • ์—ฌ๊ธฐ์ €๊ธฐ ์„  ๊ทธ์–ด์„œ ํ’€์ด

  • ์ ๋ถ„์ธ๊ฐ€? ๋ผ๋Š” ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋Œ€๋‹ต์€ ์•„๋‹ˆ์˜ค

      #include <iostream>
      #include <math.h>
      
      using namespace std;
      
      const double PI = 3.14159265359;
      
      double getStripedHeight(double radius)
      {
      	double height = radius * (sqrt(3.0)-1) / 2;
      	return height;
      }
      
      double getStriped(double radius)
      {
      	double circlePart = (PI*radius*radius) / 12;
      	double triangle = radius/2 * getStripedHeight(radius) / 2;
      	double striped = 4 * (circlePart - 2*triangle);
      	return striped;
      }
      
      double getDotted(double radius)
      {
      	double circlePart = (PI*radius*radius)/4 - (radius*radius)/2;
      	double dotted = 4 * (circlePart - getStriped(radius)/2);
      	return dotted;
      }
      
      int main()
      {
      	double length;
      	while (cin >> length)
      	{
      		double striped = getStriped(length);
      		double dotted = getDotted(length);
      		double rest = length*length - striped - dotted;
      		printf("%.3f %.3f %.3f\n", striped, dotted, rest);
      	}
      	return 0;
      }
    

Outbreak

๋ฌธ์ œ ์š”์•ฝ

  • ์›๊ณผ ์ •์‚ฌ๊ฐํ˜•์ด ๊ต์ฐจ ํ•  ๋•Œ, ๊ฐ ๋ถ€๋ถ„ ์˜์—ญ์˜ ๋„“์ด ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œ ํ•ด๊ฒฐ

  • ์‚ผ๊ฐํ˜•๊ณผ ๋ถ€์ฑ„๊ผด์˜ ๋ณผ๋ก๋ถ€๋ถ„ ๋„“์ด๋ฅผ ๊ตฌํ•ด์„œ ํ•ด๊ฒฐ

์†Œ๊ฐ

  • ๋‹ต์— ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์Œ;
    • ์•„ ์ž˜๋ชป ํ’€์—ˆ๋„ค์š”.. ์˜ฌ๋ฆฌ๋‹ค ๋ฌธ์ œ์  ๋ฐœ๊ฒฌ ํ..
    • ์Œ, ์ œ๋Œ€๋กœ ๊ณ ์นœ๊ฑฐ ๊ฐ™์€๋ฐ.. Wrong Answer.. ํ..;
      • UVa๊ฒฐ๊ณผ Accepted! Programming-challenges ํ™ˆํŽ˜์ด์ง€์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋“ฏ^^;

ํ’€์ด

// "Is This Integration" Solution By Outbreak 

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//#define _TDD

//---------------------------------------------------------------------------

namespace Solver
{
	static const double PI = 3.14159265358979;
	
	class ABCD
	{
	public:
		ABCD(double r) : r(r) {}

	public:
		// ๊ฐ ์˜์—ญ ๋„“์ด
		double GetStripedArea()
		{
			return GetRectangleArea() + GetChordArea() * 4;
		}

		double GetDottedArea()
		{
			return  GetTriangleArea() * 4 + GetChordArea() * 4;
		}

		double GetRestArea()
		{
			return r * r - GetStripedArea() - GetDottedArea();
		}

	private:
		// ๋ถ€์ฑ„๊ผด ABD, CDB
		double GetBigFanArea()
		{
			return (r * r * PI) / 4;
		}

		// ์‚ผ๊ฐํ˜• ABD, CBD
		double GetBigTriangleArea()
		{
			return r * r / 2;
		}

		// striped ์˜์—ญ ๋‚ด๋ถ€ ์ •์‚ฌ๊ฐํ˜• ๋„“์ด
		double GetRectangleArea()
		{			
			return GetChordLength() * GetChordLength();
		}

		// dotted ์˜์—ญ ๋‚ด๋ถ€ ์ •์‚ผ๊ฐํ˜• ๋„“์ด
		double GetTriangleArea()
		{
			return sqrt((double)3)*GetChordLength()*GetChordLength()/4;
		}
	
		// striped ์‚ฌ๊ฐํ˜• ๋˜๋Š” dotted ์‚ผ๊ฐํ˜•์˜ ๋ณผ๋ก ๋ถ€๋ถ„ ๋„“์ด
		double GetChordArea()
		{		
			return (GetBigFanArea() - GetBigTriangleArea() - GetRectangleArea()/2 - GetTriangleArea() ) / 3;
		}

		double GetChordLength()
		{
			return  (r/2) / cos(PI/12);
		}

	private:
		double r;
	};

	void Calculate(const double r, double& striped, double& dotted, double& rest)
	{
		ABCD abcd(r);

		// striped		
		striped = abcd.GetStripedArea();

		// dotted
		dotted = abcd.GetDottedArea();

		// rest
		rest = abcd.GetRestArea();
	}
}

//---------------------------------------------------------------------------

namespace Runner
{
	void Execute(istream& in, ostream& out)
	{
		double a;

		while( in >> a )
		{
			if( in.fail() )
				break;

			double striped = 0, dotted = 0, rest = 0;

			// ๋‹ต ๊ตฌํ•˜๊ธฐ
			Solver::Calculate(a, striped, dotted, rest);

			out << setprecision(3) << fixed << striped << " " << dotted << " " << rest << endl;	
		}
	}
}

//---------------------------------------------------------------------------

#ifdef _TDD

#include "unittest++.h"

TEST(Output1)
{
	stringstream input;
    stringstream output;

    input << "0.1";

	Runner::Execute(input, output);

	CHECK_EQUAL("0.003 0.005 0.002\n", output.str());           
}

TEST(Output)
{
	stringstream input;
    stringstream output;

    input << "0.1\n0.2\n0.3\n";

	Runner::Execute(input, output);

	CHECK_EQUAL("0.003 0.005 0.002\n0.013 0.020 0.007\n0.028 0.046 0.016\n", output.str());           
}

#endif

//---------------------------------------------------------------------------

int main()
{

#ifdef _TDD	
	UnitTest::RunAllTests();
#else
	Runner::Execute(cin, cout);
#endif // _TDD
	return 0;

}

itmentor

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

์†Œ์Šค(C++)

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    float centerTriangleArea(float area)
    {
    	//sqrt( 1-x^2 ) --> (x*sqrt(1 - x^2) + arcsin(x))/2;
    	//x=[0.5.. sqrt(3) / 2]
    	float center = 0.26179938090693;
    	float baseArea = 0.5 * ( sqrt(3.0) / 2.0  - 0.5 );
    
    	return 4*area*(center - baseArea);
    }
    
    float topTriangleArea(float area)
    {
    	//sqrt( 1-x^2 ) --> (x*sqrt(1 - x^2) + arcsin(x))/2;
    	//x=[0..0.5]
    	float top = 0.47830573874525906;
    	return area * 8*(0.5 - top);	//8์กฐ๊ฐ
    }
    
    float midTriangleArea(float area)
    {
    	return area  - centerTriangleArea(area) - topTriangleArea(area);
    }
    
    int main()
    {
    	float scale;
    	while( std::cin >> scale )
        {
                if( std::cin.fail() )
                        break;
    
    			scale *= scale;	//area
    
     			float center = centerTriangleArea(scale);
    			float top = topTriangleArea(scale);
    			float mid = midTriangleArea(scale);
    
    			printf("%.3f %.3f %.3f\n", center, mid, top );
        }
    
    
    	return 0;
    }
โš ๏ธ **GitHub.com Fallback** โš ๏ธ