/***************************************
*                                      *
*         経済要素、年金改定率、       *
*       及び基礎年金単価等の設定       *
*                                      *
*                                      *
***************************************/

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "snaps.h"
#include "seid.h"
#include "econ.h"
#include "cntl.h"
#include "file_open.h"

double round( double a , int n );
double round_2( double a );
int entyou_nensu( int a , int b );

void econ()
{
	// 変数宣言
	int data_number;
	int nendo;
	int nendo_kaishou_ikou;
	int seinendo;
	int seinendo_kaishou_ikou;
	int nenrei;
	int kubun;
	int counter;
	int tokurei_kaishou_nendo[SAISHUNENDO - 0 - ( SHONENDO - MAX_ROREI_JUKYU ) + 1];
	double buffer[16];
	double previous_buffer0;
	double x[SAISHUNENDO - ECON_SHONENDO + 1];
	double y[SAISHUNENDO - ECON_SHONENDO + 1];
	double base_up_real[SAISHUNENDO - ECON_SHONENDO + 1];
	double cpi_tokurei_ruiseki[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double kaiteiritu_ruiseki[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double kaiteiritu_s[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double kaiteiritu_k[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double Full_Pension_Tokurei[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double Full_Pension_Honrai[SAISHUNENDO - ECON_SHONENDO + 1][MAX_ROREI_JUKYU - UNDER_67 + 1];
	double A;
	double c_up;
	double ucr;
	double cpi_up_ruiseki[SAISHUNENDO - ECON_SHONENDO + 1];
	
	A = 0.910;
	c_up = 0.00354;
	ucr = 0.1830;
	
		fprintf( fp_settei , "可処分所得割合 %f\n" , A );
		fprintf( fp_settei , "厚年保険料引上げ幅 %f\n" , c_up );
		fprintf( fp_settei , "厚年最終保険料率 %f\n" , ucr );
	
	// 初期化
	previous_buffer0 = -1000.;
	
	// ファイルから読み込み
	// ＥＯＦ検出だけではねられないので、年度が減少したときも脱出することとする
	while ( ( read_data( buffer , fp_econ , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)( buffer[0] ) + 2000;
		cpi_up[nendo - ECON_SHONENDO] = 1. + buffer[6] / 100.;
		base_up[nendo - ECON_SHONENDO] = 1. + buffer[4] / 100. /* + buffer[6] / 100. （実質ベース表記）*/;
		previous_buffer0 = buffer[0];
	}
	
	for( counter = nendo + 1 ; counter <= SAISHUNENDO ; counter++ )
	{
		cpi_up[counter - ECON_SHONENDO] = cpi_up[nendo - ECON_SHONENDO];
		base_up[counter - ECON_SHONENDO] = base_up[nendo - ECON_SHONENDO];
	}
	
	for( nendo = ECON_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		base_up_real[nendo - ECON_SHONENDO] = base_up[nendo - ECON_SHONENDO] / cpi_up[nendo - ECON_SHONENDO];
	}
	
	// 改定率の作成
	
	// 状態の初期化
	for( nendo = ECON_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
			kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
			
			//コーホート別に満額を設定
			Full_Pension_Tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67] = Full_Pension_Tokurei_Shonendo[ entyou_nensu( nendo , nenrei ) ];
			Full_Pension_Honrai[nendo - ECON_SHONENDO][nenrei - UNDER_67] = Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ];
			
			cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
			cpi_up_tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
		}
		x[nendo - ECON_SHONENDO] = 1.;
		y[nendo - ECON_SHONENDO] = 1.;
		cpi_up_ruiseki[nendo - ECON_SHONENDO] = 1.;
	}
	
	
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			Full_Pension[nendo - SHONENDO][nenrei - UNDER_67] = Full_Pension_Tokurei_Shonendo[ entyou_nensu( nendo , nenrei ) ];
		}
		for( nenrei = 0 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			Kakyu_Tanka_12shi[nendo - SHONENDO][nenrei] = Kakyu_Tanka_12shi_Tokurei_Shonendo;
			Kakyu_Tanka_3shiiko[nendo - SHONENDO][nenrei] = Kakyu_Tanka_3shiiko_Tokurei_Shonendo;
		}
	}
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] = SHONENDO;
	}
	
	for( nendo = 2005 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		cpi_up_ruiseki[nendo - ECON_SHONENDO] = cpi_up_ruiseki[nendo - 1 -ECON_SHONENDO] * cpi_up[nendo - ECON_SHONENDO];
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			if( nenrei == UNDER_67 )
			{
				cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] = min( cpi_tokurei_ruiseki[nendo - 1 -ECON_SHONENDO][nenrei - UNDER_67], cpi_up_ruiseki[nendo - ECON_SHONENDO]);
			}
			else
			{
				cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] = min( cpi_tokurei_ruiseki[nendo - 1 -ECON_SHONENDO][nenrei - 1 - UNDER_67], cpi_up_ruiseki[nendo - ECON_SHONENDO]);
			}
		}
	}
	
	for( nendo = 2005 ; nendo <= SAISHUNENDO ; nendo++)
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			if( nenrei == UNDER_67 )
			{
				cpi_up_tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67] = round(cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] / cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67], 3);
			}
			else
			{
				cpi_up_tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67] = round(cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] / cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67], 3);
			}
		}
	}
	
	// 物価スライド特例用の物価スライドの累積
	for( nendo = 2004 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			cpi_tokurei_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
			 = cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67] * cpi_up_tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67];
		}
	}
	
	// 実質賃金上昇率及び可処分所得変化率の算定
	for( nendo = 2007 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		y[nendo - ECON_SHONENDO]
		 = ( base_up_real[nendo - 4 - ECON_SHONENDO] * base_up_real[nendo - 3 - ECON_SHONENDO] * base_up_real[nendo - 2 - ECON_SHONENDO] );
		
		y[nendo - ECON_SHONENDO] = round( pow( y[nendo - ECON_SHONENDO] , 1. / 3. ) , 3 );
	}
	
	for( nendo = 2007 ; nendo <= 2019 ; nendo++ )
	{
		x[nendo - ECON_SHONENDO] = ( ( A - ( 0.1358 + c_up * ( nendo - 2006 ) ) / 2. ) / ( A - ( 0.1358 + c_up * ( nendo - 2007 ) ) / 2. ) );
		
		x[nendo - ECON_SHONENDO] = round( x[nendo - ECON_SHONENDO] , 3 );
	}
	x[2020 - ECON_SHONENDO]= ( ( A - ucr / 2 ) / ( A - ( 0.1358 + c_up * ( 2020 - 2007 ) ) / 2. ) );
	x[2020 - ECON_SHONENDO] = round( x[2020 - ECON_SHONENDO] , 3 );
	
	// 年度別・年齢別の改定率の作成
	for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
	{
		for( nendo = 2005 ; nendo <= SAISHUNENDO ; nendo++ )
		{
			if( nenrei == UNDER_67 )
			{
				// 逆転防止
				if( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < 1.
				 && cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < cpi_up[nendo - 1 - ECON_SHONENDO] )
				{
					if( cpi_up[nendo - 1 - ECON_SHONENDO] < 1. )
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up[nendo - 1 - ECON_SHONENDO];
					}
					else
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
					}
				}
				else
				{
					if( nendo <= 2006 )
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up[nendo - 1 - ECON_SHONENDO];
					}
					else
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
						 = round( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] , 3 );
					}
				}
				kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = round( kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
				        * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] , 3 );
			}
			else
			{
				if( cpi_up[nendo - 1 - ECON_SHONENDO] > cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] &&
				     cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] > 1. )
				{
					kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					 = round( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] , 3 );
				}
				else if( cpi_up[nendo - 1 - ECON_SHONENDO] > 1.
				      && cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < 1. )
				{
					kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
				}
				else
				{
					kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up[nendo - 1 - ECON_SHONENDO];
				}
				
				kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = round( kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67]
				        * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] , 3 );
			}
		}
	}
	
	// 物価スライド特例解消
	nendo = 2013;
	for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
	{
		if( nenrei == UNDER_67 )
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 1.5 / 100 - 1.7 / 100 );
		}
		else
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - 1 - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 1.5 / 100 - 1.7 / 100 );
		}
	}
	nendo = 2014;
	for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
	{
		if( nenrei == UNDER_67 )
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 0.5 / 100 - 1.7 / 100 );
		}
		else
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - 1 - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 0.5 / 100 - 1.7 / 100 );
		}
	}
	nendo = 2015;
	for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
	{
		if( nenrei == UNDER_67 )
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 0.0 / 100 - 1.7 / 100 );
		}
		else
		{
			cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
			 = min( cpi_tokurei_ruiseki[nendo - 2 - ECON_SHONENDO][nenrei - 1 - UNDER_67]
			      , kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] + 0.0 / 100 - 1.7 / 100 );
		}
	}
	for( nendo = 2013 ; nendo <=2015 ; nendo++ )
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			if( nenrei == UNDER_67 )
			{
				cpi_up_tokurei[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
				 = round(cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67] / cpi_tokurei_ruiseki[nendo - 2 -  ECON_SHONENDO][nenrei - UNDER_67] , 3 );
			}
			else
			{
				cpi_up_tokurei[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
				 = round(cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67] / cpi_tokurei_ruiseki[nendo - 2 -  ECON_SHONENDO][nenrei - 1 - UNDER_67] , 3 );
			}
		}
	}
	
	// 基礎年金単価等の改定
	
	// 基礎年金単価の改定
	// コーホート別物価スライド特例解消年度を算出
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			nenrei = nendo - seinendo;
			
			if( ( nenrei >= UNDER_67 ) && ( nenrei <= MAX_ROREI_JUKYU ) )
			{
				// 推計開始
				Full_Pension_Tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = round_2( Full_Pension_Tokurei_Shonendo[ entyou_nensu( nendo , nenrei ) ]
				          * round( 0.988 * round( cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67] , 3 ) , 3 ) );
				
				Full_Pension_Honrai[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = round_2( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] );
				
				Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
				 = max( Full_Pension_Tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67] , Full_Pension_Honrai[nendo - ECON_SHONENDO][nenrei - UNDER_67] );
				
				if( Full_Pension_Tokurei[nendo - ECON_SHONENDO][nenrei - UNDER_67]
		    	   > Full_Pension_Honrai[nendo - ECON_SHONENDO][nenrei - UNDER_67] )
				{
					if( nenrei == MAX_ROREI_JUKYU )
					{
						tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] = nendo + 1;
						break;
					}
					if( nendo == 2015 )
					{
						tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] = nendo;
						break;
					}
				}
				else
				{
					tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] = nendo;
					break;
				}
			}
		}
		if( nenrei == UNDER_67 )
		{
			break;
		}
	}
	
	// 67歳以下の時点ですでに特例が解消された場合
	for( seinendo_kaishou_ikou = seinendo + 1 ; seinendo_kaishou_ikou <= SAISHUNENDO - 0 ; seinendo_kaishou_ikou++ )
	{
		tokurei_kaishou_nendo[seinendo_kaishou_ikou - ( SHONENDO - MAX_ROREI_JUKYU )] = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )];
		
		for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			nenrei = nendo - seinendo_kaishou_ikou;
			if( nenrei == UNDER_67 )
			{
				Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
		 	   = round_2( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] );
			}
		}
	}
	
	// 年度別・年齢別の改定率の作成
	for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
	{
		for( nendo = 2007 ; nendo <= SAISHUNENDO ; nendo++ )
		{
			y[nendo - ECON_SHONENDO]
			 = base_up_real[nendo - 4 - ECON_SHONENDO] * base_up_real[nendo - 3 - ECON_SHONENDO] * base_up_real[nendo - 2 - ECON_SHONENDO];
			if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
			{
				y[nendo - ECON_SHONENDO] = round( pow( y[nendo - ECON_SHONENDO] , 1. / 3. ) , 3 );
			}
			else
			{
				y[nendo - ECON_SHONENDO] = pow( y[nendo - ECON_SHONENDO] , 1. / 3. );
			}
			
			if( nendo <= 2019 )
			{
				x[nendo - ECON_SHONENDO] = ( ( A - ( 0.1358 + c_up * ( nendo - 2006 ) ) / 2. ) / ( A - ( 0.1358 + c_up * ( nendo - 2007 ) ) / 2. ) );
				if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
				{
					x[nendo - ECON_SHONENDO] = round( x[nendo - ECON_SHONENDO] , 3 );
				}
			}
			else if( nendo == 2020 )
			{
				x[2020 - ECON_SHONENDO]= ( ( A - ucr / 2. ) / ( A - ( 0.1358 + c_up * ( 2020 - 2007 ) ) / 2. ) );
				if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
				{
					x[2020 - ECON_SHONENDO] = round( x[2020 - ECON_SHONENDO] , 3 );
				}
			}
			
			if( nenrei == UNDER_67 )
			{
				// 逆転防止
				if( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < 1.
				 && cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < cpi_up[nendo - 1 - ECON_SHONENDO] )
				{
					if( cpi_up[nendo - 1 - ECON_SHONENDO] < 1. )
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up[nendo - 1 - ECON_SHONENDO];
					}
					else
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
					}
				}
				else
				{
					if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
						 = round( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] , 3 );
					}
					else
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
						 = cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO];
					}
				}
				
				if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
				{
					kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					 = round( kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67]
					        * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] , 3 );
				}
				else
				{
					kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					 = kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67];
				}
			}
			else
			{
				// 逆転防止
				if( cpi_up[nendo - 1 - ECON_SHONENDO] > cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO]
				 && cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] > 1. )
				{
					if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
						 = round( cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] , 3 );
					}
					else
					{
						kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
						 = cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO];
					}
				}
				else if( cpi_up[nendo - 1 - ECON_SHONENDO] > 1.
				      && cpi_up[nendo - 1 - ECON_SHONENDO] * x[nendo - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] < 1. )
				{
					kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
				}
				else
				{
					kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up[nendo - 1 - ECON_SHONENDO];
				}
				
				if( nendo <= tokurei_kaishou_nendo[nendo - nenrei - ( SHONENDO - MAX_ROREI_JUKYU )] )
				{
					kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					 = round( kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67]
					        * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] , 3 );
				}
				else
				{
					kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					 = kaiteiritu_ruiseki[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67];
				}
			}
		}
	}
	
	// 特例解消年度以降の年金額を算出
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		nendo = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] + 1;
		nenrei = nendo - seinendo;
		
		// 解消年度直後、改定率が変わらない場合
		while( ( nendo < SAISHUNENDO ) && ( nenrei < MAX_ROREI_JUKYU ) )
		{
			if( nenrei >= UNDER_67 )
			{
				if( kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				     == kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67] )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = max( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					      , round_2( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] ) );
					break;
				}
				else
				{
					break;
				}
			}
			else
			{
				if( kaiteiritu_ruiseki[nendo + 1 - ECON_SHONENDO][UNDER_67 - UNDER_67]
				     != kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][UNDER_67 - UNDER_67] )
				{
					break;
				}
			}
			nendo++;
			nenrei++;
		}
		
		// それ以外の場合
		while( ( nendo <= SAISHUNENDO ) && ( nenrei <= MAX_ROREI_JUKYU ) )
		{
			if( nenrei == UNDER_67 )
			{
				if( Option3 == 0 )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = Full_Pension[nendo - 1 - SHONENDO][UNDER_67 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67];
				}
				if( Option3 == 1 )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = Full_Pension[nendo - 1 - SHONENDO][UNDER_67 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67]
					 * Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , UNDER_67 ) ]
				   / Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo - 1 , UNDER_67 ) ];
				}
			}
			else if( nenrei >= UNDER_67 + 1 )
			{
				Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
				 = Full_Pension[nendo - 1 - SHONENDO][nenrei - 1 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67];
			}
			nendo++;
			nenrei++;
		}
	}
	
	// 加給単価の改定
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		for( nendo = SHONENDO ; nendo < tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] ; nendo++ )
		{
			nenrei = nendo - seinendo;
			
			if( nenrei >= 0 && nenrei <= MAX_ROREI_JUKYU )
			{
				Kakyu_Tanka_12shi[nendo - SHONENDO][nenrei]
				 = round_2( Kakyu_Tanka_12shi_Tokurei_Shonendo * round( 0.988 * round( cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][ UNDER_67 - UNDER_67] , 3 ) , 3 ) );
				Kakyu_Tanka_3shiiko[nendo - SHONENDO][nenrei]
				 = round_2( Kakyu_Tanka_3shiiko_Tokurei_Shonendo * round( 0.988 * round( cpi_tokurei_ruiseki[nendo - 1 - ECON_SHONENDO][UNDER_67 - UNDER_67] , 3 ) , 3 ) );
			}
		}
		
		nenrei = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - seinendo;
		
		if( nenrei >= 0 && nenrei <= MAX_ROREI_JUKYU )
		{
			Kakyu_Tanka_12shi[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - SHONENDO][nenrei]
			 = round_2( Kakyu_Tanka_12shi_Honrai_Shonendo
			          * kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][UNDER_67 - UNDER_67] );
			Kakyu_Tanka_3shiiko[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - SHONENDO][nenrei]
			 = round_2( Kakyu_Tanka_3shiiko_Honrai_Shonendo
			          * kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][UNDER_67 - UNDER_67] );
		}
		
		nendo_kaishou_ikou = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] + 1;
		while( kaiteiritu_ruiseki[nendo_kaishou_ikou - ECON_SHONENDO][UNDER_67 - UNDER_67]
		        == kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][UNDER_67 - UNDER_67] )
		{
			nenrei = nendo_kaishou_ikou - seinendo;
			
			if( nenrei >= 0 && nenrei <= MAX_ROREI_JUKYU )
			{
				Kakyu_Tanka_12shi[nendo_kaishou_ikou - SHONENDO][nenrei]
				 = max( Kakyu_Tanka_12shi_Honrai_Shonendo * kaiteiritu_ruiseki[nendo_kaishou_ikou - ECON_SHONENDO][UNDER_67 - UNDER_67]
						  , round_2( Kakyu_Tanka_12shi_Honrai_Shonendo * kaiteiritu_ruiseki[nendo_kaishou_ikou - ECON_SHONENDO][UNDER_67 - UNDER_67] ) );
				Kakyu_Tanka_3shiiko[nendo_kaishou_ikou - SHONENDO][nenrei]
				 = max( Kakyu_Tanka_3shiiko_Honrai_Shonendo * kaiteiritu_ruiseki[nendo_kaishou_ikou - ECON_SHONENDO][UNDER_67 - UNDER_67]
						  , round_2( Kakyu_Tanka_3shiiko_Honrai_Shonendo * kaiteiritu_ruiseki[nendo_kaishou_ikou - ECON_SHONENDO][UNDER_67 - UNDER_67] ) );
			}
			nendo_kaishou_ikou++;
		}
		
		for( nendo = nendo_kaishou_ikou ; nendo <= SAISHUNENDO ; nendo++ )
		{
			nenrei = nendo - seinendo;
			
			if( nenrei >= 0 && nenrei <= MAX_ROREI_JUKYU )
			{
				Kakyu_Tanka_12shi[nendo - SHONENDO][nenrei]
				 = Kakyu_Tanka_12shi_Honrai_Shonendo * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67];
				Kakyu_Tanka_3shiiko[nendo - SHONENDO][nenrei]
				 = Kakyu_Tanka_3shiiko_Honrai_Shonendo * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67];
			}
		}
	}
	
	// 死亡一時金単価
	for( kubun = 0 ; kubun <= 5 ; kubun++ )
	{
		Tanka_Shibou[SHONENDO + 1 - SHONENDO][kubun] = Tanka_Shibou_Shonendo[kubun];
		for( nendo = SHONENDO + 2 ; nendo <= SAISHUNENDO ; nendo++ )
		{
			Tanka_Shibou[nendo - SHONENDO][kubun] = Tanka_Shibou_Shonendo[kubun];
		}
	}
	
	// 各コーホートの特例解消年度までの単年改定率を変更
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		for( nendo = 2005 ; nendo < tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] ; nendo++ )
		{
			nenrei = nendo - seinendo;
			
			if( nenrei >= UNDER_67 && nenrei <= MAX_ROREI_JUKYU )
			{
				kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] = cpi_up_tokurei[nendo - 1 - ECON_SHONENDO][nenrei - UNDER_67];
			}
		}
		
		nenrei = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - seinendo;
		if( nenrei > UNDER_67 && nenrei <= MAX_ROREI_JUKYU )
		{
			kaiteiritu_tannen[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67]
			 = Full_Pension_Honrai[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67]
			 / Full_Pension_Tokurei[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67];
		}
		else if( nenrei == UNDER_67 )
		{
			kaiteiritu_tannen[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67]
			 = Full_Pension_Honrai[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67]
			 / Full_Pension_Tokurei[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - 1 - ECON_SHONENDO][nenrei - UNDER_67];
		}
	}
	
	// 既裁物スラ８割保障を加味した改定率の作成
	for( nendo = 2005 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			kaiteiritu_s[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
			kaiteiritu_k[nendo - ECON_SHONENDO][nenrei - UNDER_67] = 1.;
		}
	}
	for( nendo = 2005 + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		for( nenrei = UNDER_67 + 1 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			kaiteiritu_s[nendo - ECON_SHONENDO][nenrei - UNDER_67]
			 = kaiteiritu_s[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67];
			kaiteiritu_k[nendo - ECON_SHONENDO][nenrei - UNDER_67]
			 = kaiteiritu_k[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67];
			
			if( kaiteiritu_k[nendo - ECON_SHONENDO][nenrei - UNDER_67] < Kisai_Shitasasae * kaiteiritu_s[nendo - ECON_SHONENDO][nenrei - UNDER_67] )
			{
				kaiteiritu_k[nendo - ECON_SHONENDO][nenrei - UNDER_67] = Kisai_Shitasasae * kaiteiritu_s[nendo - ECON_SHONENDO][nenrei - UNDER_67];
				
				kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = kaiteiritu_k[nendo - ECON_SHONENDO][nenrei - UNDER_67] / kaiteiritu_k[nendo - 1 - ECON_SHONENDO][nenrei - 1 - UNDER_67];
			}
		}
	}
	
	// 特例解消年度以降の年金額を算出（既裁物スラ８割保障加味後）
	for( seinendo = SHONENDO - MAX_ROREI_JUKYU ; seinendo <= SAISHUNENDO - 0 ; seinendo++ )
	{
		nendo = tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] + 1;
		nenrei = nendo - seinendo;
		
		// 解消年度直後、改定率が変わらない場合
		while( ( nendo < SAISHUNENDO ) && ( nenrei < MAX_ROREI_JUKYU ) )
		{
			if( nenrei >= UNDER_67 )
			{
				if( kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				     == kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][nenrei - UNDER_67] )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = max( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67]
					      , round_2( Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , nenrei ) ] * kaiteiritu_ruiseki[nendo - ECON_SHONENDO][nenrei - UNDER_67] ) );
					break;
				}
				else
				{
					break;
				}
			}
			else
			{
				if( kaiteiritu_ruiseki[nendo + 1 - ECON_SHONENDO][UNDER_67 - UNDER_67]
				     != kaiteiritu_ruiseki[tokurei_kaishou_nendo[seinendo - ( SHONENDO - MAX_ROREI_JUKYU )] - ECON_SHONENDO][UNDER_67 - UNDER_67] )
				{
					break;
				}
			}
			nendo++;
			nenrei++;
		}
		
		// それ以外の場合
		while( ( nendo <= SAISHUNENDO ) && ( nenrei <= MAX_ROREI_JUKYU ) )
		{
			if( nenrei == UNDER_67 )
			{
				if( Option3 == 0 )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = Full_Pension[nendo - 1 - SHONENDO][UNDER_67 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67];
				}
				if( Option3 == 1 )
				{
					Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
					 = Full_Pension[nendo - 1 - SHONENDO][UNDER_67 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67]
					 * Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo , UNDER_67 ) ]
					 / Full_Pension_Honrai_Shonendo[ entyou_nensu( nendo - 1 , UNDER_67 ) ];
				}
			}
			else if( nenrei >= UNDER_67 + 1 )
			{
				Full_Pension[nendo - SHONENDO][nenrei - UNDER_67]
				 = Full_Pension[nendo - 1 - SHONENDO][nenrei - 1 - UNDER_67] * kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67];
			}
			nendo++;
			nenrei++;
		}
	}
	
	// 改定率ファイルの作成
	for( nendo = 2005 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		fprintf( fp_kaitei , "%d," , nendo - 2000 );
		for( nenrei = UNDER_67 ; nenrei <= MAX_ROREI_JUKYU ; nenrei++ )
		{
			fprintf( fp_kaitei , "%20.14e," , kaiteiritu_tannen[nendo - ECON_SHONENDO][nenrei - UNDER_67] );
		}
		fprintf( fp_kaitei , "\n" );
	}
}


// 小数第 n + 1 位で四捨五入
double round( double a , int n )
{
 double  b;
 char    buf[256] = {'\0'};
 char    *p;
	
	sprintf( buf , "%.*f" , n , a );
	b = strtod( buf , &p );
	
	return b;
}


// 基礎年金単価等の作成用
double round_2( double a )
{
	double b;
	
	b = floor( ( a + 50. ) / 100. ) * 100;
	
	return b;
}


// オプションⅢ用
int entyou_nensu( int a , int b )
{
	int n;
	
	if( a - b <= 1957 )
	{
		n = 0;
	}
	else if( ( a - b >= 1958 )&&( a - b <= 1959 ) )
	{
		n = 1;
	}
	else if( ( a - b >= 1960 )&&( a - b <= 1961 ) )
	{
		n = 2;
	}
	else if( ( a - b >= 1962 )&&( a - b <= 1963 ) )
	{
		n = 3;
	}
	else if( ( a - b >= 1964 )&&( a - b <= 1965 ) )
	{
		n = 4;
	}
	else if( a - b >= 1966 )
	{
		n = 5;
	}
	
	return n;
}
