/*************************************
*                                    *
*    外枠システム  simlroud.c        *
*  （労働力人口〜雇用ﾒ推計）        *
*                                    *
*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "set.h"
#include "cntl.h"


void simlroud(void) {

/*  変数定義  */
double  TMP ;
double  TMQ ; 
double  TMR ;
int     ii , xx , flg_end , xx1 ;


  for ( nendo=ROUDYR+1; nendo<=KE+1; nendo++ ) {
    for ( sei=1; sei<=4; sei++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        roudr_c[nendo-STARTY][sei][nenrei] = roudr_c[nendo-1-STARTY][sei][nenrei] ;
        roudr_a_c[nendo-STARTY][sei][nenrei] = roudr_a_c[nendo-1-STARTY][sei][nenrei] ;
        syuwari_c[nendo-STARTY][sei][nenrei] = syuwari_c[nendo-1-STARTY][sei][nenrei] ;
        syuwari_a_c[nendo-STARTY][sei][nenrei] = syuwari_a_c[nendo-1-STARTY][sei][nenrei] ;
        syugyor_c[nendo-STARTY][sei][nenrei] = syugyor_c[nendo-1-STARTY][sei][nenrei] ;
        syugyor_a_c[nendo-STARTY][sei][nenrei] = syugyor_a_c[nendo-1-STARTY][sei][nenrei] ;
      }
    }
  }

  for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        roudj_c[nendo-STARTY][sei][nenrei]
          = jinko_c[nendo-STARTY][sei][nenrei] * roudr_c[nendo-STARTY][sei][nenrei] ;
        syugyoj_c[nendo-STARTY][sei][nenrei]
          = roudj_c[nendo-STARTY][sei][nenrei] * syuwari_c[nendo-STARTY][sei][nenrei] ;
      }
      roudj_c[nendo-STARTY][2][nenrei]
        = roudj_c[nendo-STARTY][3][nenrei] + roudj_c[nendo-STARTY][4][nenrei] ;
      syugyoj_c[nendo-STARTY][2][nenrei]
        = syugyoj_c[nendo-STARTY][3][nenrei] + syugyoj_c[nendo-STARTY][4][nenrei] ;

      roudj_c[nendo-STARTY][0][nenrei]
        = roudj_c[nendo-STARTY][1][nenrei] + roudj_c[nendo-STARTY][2][nenrei] ;
      syugyoj_c[nendo-STARTY][0][nenrei]
        = syugyoj_c[nendo-STARTY][1][nenrei] + syugyoj_c[nendo-STARTY][2][nenrei] ;
    }
  }

    for ( sei=1; sei<=4; sei++ ) {
      for ( nenrei=16; nenrei<=100; nenrei++ ) {
        koyour_cho_c[KIJUN+1-STARTY][sei][nenrei] 
         = 1.0 / 5.0 * ( koyour_c[KIJUN-STARTY][sei][nenrei] / koyour_c[KIJUN-1-STARTY][sei][nenrei-1] 
              + koyour_c[KIJUN-1-STARTY][sei][nenrei] / koyour_c[KIJUN-2-STARTY][sei][nenrei-1] 
              + koyour_c[KIJUN-2-STARTY][sei][nenrei] / koyour_c[KIJUN-3-STARTY][sei][nenrei-1] 
              + koyour_c[KIJUN-3-STARTY][sei][nenrei] / koyour_c[KIJUN-4-STARTY][sei][nenrei-1] 
              + koyour_c[KIJUN-4-STARTY][sei][nenrei] / koyour_c[KIJUN-5-STARTY][sei][nenrei-1] ) ;
      }
    }
    for ( nendo=KIJUN+2; nendo<=KE+1; nendo++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        for ( nenrei=16; nenrei<=100; nenrei++ ) {
          if ( koyour_cho_c[KIJUN+1-STARTY][sei][nenrei] > 1.0 ) {
            koyour_cho_c[nendo-STARTY][sei][nenrei]
              = 1.0 + max( 0, ( koyour_cho_c[KIJUN+1-STARTY][sei][nenrei] - 1.0 )
                               * ( 5.0 - nendo + KIJUN  + 1.0 ) / 5.0 ) ;
          }
          else {
            koyour_cho_c[nendo-STARTY][sei][nenrei] = koyour_cho_c[KIJUN+1-STARTY][sei][nenrei] ;
          }
        }
      }
    }

    for ( nendo=KIJUN+1; nendo<=KE+1; nendo++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        for ( nenrei=15; nenrei<=100; nenrei++ ) {

          if (( nenrei<=22 ) || ( nenrei-nendo+KIJUN <= 14 )) {
            koyour_c[nendo-STARTY][sei][nenrei] = koyour_c[nendo-1-STARTY][sei][nenrei] ;
          }
          else {
            koyour_c[nendo-STARTY][sei][nenrei]
              = koyour_c[nendo-1-STARTY][sei][nenrei-1] * koyour_cho_c[nendo-STARTY][sei][nenrei] ;
          }
          koyour_a_c[nendo-STARTY][sei][nenrei] = koyour_c[nendo-STARTY][sei][nenrei] ; 
        }
      }
    }
      for ( nendo=KIJUN+1; nendo<=KE+1; nendo++ ) {
        for ( nenrei=60; nenrei<=100; nenrei++ ) {
              if ( syugyor_c[nendo-STARTY][1][nenrei] > syugyor_a_c[nendo-STARTY][1][nenrei] ) {
                koyour_c[nendo-STARTY][1][nenrei]
                  = ( koyour_a_c[nendo-STARTY][1][nenrei] * syugyor_a_c[nendo-STARTY][1][nenrei]
                      + syugyor_c[nendo-STARTY][1][nenrei] - syugyor_a_c[nendo-STARTY][1][nenrei] )
                    / syugyor_c[nendo-STARTY][1][nenrei] ;
              }
        }
		for ( nenrei=76; nenrei<=100; nenrei++ ) {
			for ( sei=1; sei<=4; sei++ ) {
			 koyour_c[nendo-STARTY][sei][nenrei]=	koyour_c[nendo-STARTY][sei][75];
			}
		}
      }

  for ( nendo=STARTY+1; nendo<=KE+1; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        koyouj_c[0][nendo-STARTY][sei][nenrei]
          = syugyoj_c[nendo-STARTY][sei][nenrei] * koyour_c[nendo-STARTY][sei][nenrei] ;
      }

      koyouj_c[0][nendo-STARTY][2][nenrei]
        = koyouj_c[0][nendo-STARTY][3][nenrei] + koyouj_c[0][nendo-STARTY][4][nenrei] ;

      koyouj_c[0][nendo-STARTY][0][nenrei]
        = koyouj_c[0][nendo-STARTY][1][nenrei] + koyouj_c[0][nendo-STARTY][2][nenrei] ;
    }
  }


    for ( nendo=KIJUN+1; nendo<=KE+1; nendo++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          seikir_c[nendo-STARTY][sei][nenrei]   = seikir_c[nendo-1-STARTY][sei][nenrei] ;
          hiseikir_c[nendo-STARTY][sei][nenrei] = hiseikir_c[nendo-1-STARTY][sei][nenrei] ;
          hiseikihurur_c[0][nendo-STARTY][sei][nenrei] = hiseikihurur_c[0][nendo-1-STARTY][sei][nenrei] ;
          hiseikihurur_c[1][nendo-STARTY][sei][nenrei] = hiseikihurur_c[1][nendo-1-STARTY][sei][nenrei] ;
          hiseikitanr_c[0][nendo-STARTY][sei][nenrei] = hiseikitanr_c[0][nendo-1-STARTY][sei][nenrei] ;
          hiseikitanr_c[1][nendo-STARTY][sei][nenrei] = hiseikitanr_c[1][nendo-1-STARTY][sei][nenrei] ;
        }
      }
    }
    for ( nendo=ROUDYR+1; nendo<=KE+1; nendo++ ) {
      hiseikitanr_all_c[2][nendo-STARTY] = hiseikitanr_all_c[2][nendo-1-STARTY] ;
      hiseikitanr_all_c[3][nendo-STARTY] = hiseikitanr_all_c[3][nendo-1-STARTY] ;
      hiseikitanr_all_c[4][nendo-STARTY] = hiseikitanr_all_c[4][nendo-1-STARTY] ;
      hiseikitanr_all_c[5][nendo-STARTY] = hiseikitanr_all_c[5][nendo-1-STARTY] ;
      hiseikitanr_all_c[6][nendo-STARTY] = hiseikitanr_all_c[6][nendo-1-STARTY] ;
    }
    

    for ( nendo=KIJUN+1; nendo<=KE+1; nendo++ ) {
      TMP = 0.0; TMQ = 0.0; TMR = 0.0;
      if ( nendo<=ROUDYR ) {
        for ( nenrei=15; nenrei<=79; nenrei++ ) {
          	TMP += koyouj_c[0][nendo-STARTY][1][nenrei] * hiseikitanr_c[0][nendo-STARTY][1][nenrei] 
            	   + koyouj_c[0][nendo-STARTY][3][nenrei] * hiseikitanr_c[0][nendo-STARTY][3][nenrei]
            	   + koyouj_c[0][nendo-STARTY][4][nenrei] * hiseikitanr_c[0][nendo-STARTY][4][nenrei];
          	TMQ += koyouj_c[0][nendo-STARTY][1][nenrei] 
            	   + koyouj_c[0][nendo-STARTY][3][nenrei]
            	   + koyouj_c[0][nendo-STARTY][4][nenrei];
        }
          if ( TMQ > 0.0 ) {
          	TMR = TMP / TMQ ;
	         if ( TANCHO == 0 ){
	         	for ( nenrei=15; nenrei<=100; nenrei++ ){
	          	tankor_cho_c[nendo-STARTY][1][nenrei] = hiseikitanr_all_c[2][nendo-STARTY] / TMR ;
	          	tankor_cho_c[nendo-STARTY][2][nenrei] = hiseikitanr_all_c[2][nendo-STARTY] / TMR ;
	         	}
	         }
	         else if(TANCHO ==1){
          		if( hiseikitanr_all_c[2][nendo-STARTY] <= TMR){
          			for ( nenrei=15; nenrei<=100; nenrei++ ){
	          		tankor_cho_c[nendo-STARTY][1][nenrei] = hiseikitanr_all_c[2][nendo-STARTY] / TMR ;  
	          		tankor_cho_c[nendo-STARTY][2][nenrei] = hiseikitanr_all_c[2][nendo-STARTY] / TMR ; 
    	    		}
    	    	}
    	    	else {
    	    	     flg_end = 1;
 		       		 for (xx=1;xx<10000 && flg_end>0; xx++){
    	    	     	TMP = 0.0; TMQ = 0.0; TMR = 0.0;
 		       		 	for ( nenrei= XR ; nenrei<=79; nenrei++ ){
 		       		 		hiseikitanr_c[1][nendo-STARTY][1][nenrei] =(1.000 + xx * 0.001) * hiseikitanr_c[0][nendo-STARTY][1][nenrei] ;
 		       		 	}
 		       		 	for ( nenrei= XR1 ; nenrei<=79; nenrei++ ){
 		       		 		hiseikitanr_c[1][nendo-STARTY][2][nenrei] =(1.000 + xx * 0.001) * hiseikitanr_c[0][nendo-STARTY][2][nenrei];
 		       		 	}
 		       		 	for ( nenrei=15; nenrei<=79; nenrei++ ) {
 		       		 	TMP += koyouj_c[0][nendo-STARTY][1][nenrei] * hiseikitanr_c[1][nendo-STARTY][1][nenrei] 
            	   				+ koyouj_c[0][nendo-STARTY][2][nenrei] * hiseikitanr_c[1][nendo-STARTY][2][nenrei];
          				TMQ += koyouj_c[0][nendo-STARTY][1][nenrei] 
            	   				+ koyouj_c[0][nendo-STARTY][2][nenrei];
            	   		}
            	   		TMR = TMP / TMQ ;
            	   		if (TMR >= hiseikitanr_all_c[2][nendo-STARTY] ){
            	   			flg_end = 0;
            	   			xx1 = xx;
            	   		}
            	   	}
            	   	for ( nenrei=15; nenrei<=100; nenrei++ ){
            	   	tankor_cho_c[nendo-STARTY][1][nenrei] =1.000;
            	   	tankor_cho_c[nendo-STARTY][2][nenrei] =1.000;
            	   	}
            	   	for ( nenrei= XR ; nenrei<=79; nenrei++ ){
            	   	tankor_cho_c[nendo-STARTY][1][nenrei] =(1.000 + xx1 * 0.001);
            		}
            		for ( nenrei= XR1 ; nenrei<=79; nenrei++ ){
            	   	tankor_cho_c[nendo-STARTY][2][nenrei] =(1.000 + xx1 * 0.001);
            		}
            	}
            }
        }
        else {
        	for ( nenrei=15; nenrei<=100; nenrei++ ){
          	tankor_cho_c[nendo-STARTY][1][nenrei] = 0.0 ;
          	tankor_cho_c[nendo-STARTY][2][nenrei] = 0.0 ;
        	}
        }
      }
      else { 
      	for ( nenrei=15; nenrei<=100; nenrei++ ){
        tankor_cho_c[nendo-STARTY][1][nenrei] = tankor_cho_c[nendo-1-STARTY][1][nenrei] ;  
        tankor_cho_c[nendo-STARTY][2][nenrei] = tankor_cho_c[nendo-1-STARTY][2][nenrei] ;  
        }
      }
	  for ( nenrei=15; nenrei<=100; nenrei++ ){
	  	tankor_cho_c[nendo-STARTY][3][nenrei] =tankor_cho_c[nendo-STARTY][2][nenrei] ;
	  	tankor_cho_c[nendo-STARTY][4][nenrei] =tankor_cho_c[nendo-STARTY][2][nenrei] ;
	  }
      for ( sei=1; sei<=4; sei++ ) {
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          hiseikitanr_c[1][nendo-STARTY][sei][nenrei]
           = hiseikitanr_c[0][nendo-STARTY][sei][nenrei] * tankor_cho_c[nendo-STARTY][sei][nenrei] ;
          if ( hiseikitanr_c[1][nendo-STARTY][sei][nenrei]+seikir_c[nendo-STARTY][sei][nenrei] > 1.0 ) {
            hiseikitanr_c[1][nendo-STARTY][sei][nenrei] = 1.0 -seikir_c[nendo-STARTY][sei][nenrei];
          }
          hiseikihurur_c[1][nendo-STARTY][sei][nenrei] = 1.0 - hiseikitanr_c[1][nendo-STARTY][sei][nenrei] -seikir_c[nendo-STARTY][sei][nenrei];
        }
      }
    }

    for ( nendo=STARTY+1; nendo<=KE+1; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        for ( sei=1; sei<=4; sei++ ) {
          koyouj_c[1][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * seikir_c[nendo-STARTY][sei][nenrei] ;
          koyouj_c[2][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * hiseikihurur_c[1][nendo-STARTY][sei][nenrei] ;
          koyouj_c[3][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * hiseikitanr_c[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_c[3][nendo-STARTY] ;
          koyouj_c[4][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * hiseikitanr_c[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_c[4][nendo-STARTY] ;
          koyouj_c[5][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * hiseikitanr_c[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_c[5][nendo-STARTY] ;
          koyouj_c[6][nendo-STARTY][sei][nenrei]
           = koyouj_c[0][nendo-STARTY][sei][nenrei] * hiseikitanr_c[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_c[6][nendo-STARTY] ;
        }
        koyouj_c[1][nendo-STARTY][2][nenrei]
         = koyouj_c[1][nendo-STARTY][3][nenrei] + koyouj_c[1][nendo-STARTY][4][nenrei] ;
        koyouj_c[2][nendo-STARTY][2][nenrei]
         = koyouj_c[2][nendo-STARTY][3][nenrei] + koyouj_c[2][nendo-STARTY][4][nenrei] ;
        koyouj_c[3][nendo-STARTY][2][nenrei]
         = koyouj_c[3][nendo-STARTY][3][nenrei] + koyouj_c[3][nendo-STARTY][4][nenrei] ;
        koyouj_c[4][nendo-STARTY][2][nenrei]
         = koyouj_c[4][nendo-STARTY][3][nenrei] + koyouj_c[4][nendo-STARTY][4][nenrei] ;
        koyouj_c[5][nendo-STARTY][2][nenrei]
         = koyouj_c[5][nendo-STARTY][3][nenrei] + koyouj_c[5][nendo-STARTY][4][nenrei] ;
        koyouj_c[6][nendo-STARTY][2][nenrei]
         = koyouj_c[6][nendo-STARTY][3][nenrei] + koyouj_c[6][nendo-STARTY][4][nenrei] ;
        koyouj_c[1][nendo-STARTY][0][nenrei]
         = koyouj_c[1][nendo-STARTY][1][nenrei] + koyouj_c[1][nendo-STARTY][2][nenrei] ;
        koyouj_c[2][nendo-STARTY][0][nenrei]
         = koyouj_c[2][nendo-STARTY][1][nenrei] + koyouj_c[2][nendo-STARTY][2][nenrei] ;
        koyouj_c[3][nendo-STARTY][0][nenrei]
         = koyouj_c[3][nendo-STARTY][1][nenrei] + koyouj_c[3][nendo-STARTY][2][nenrei] ;
        koyouj_c[4][nendo-STARTY][0][nenrei]
         = koyouj_c[4][nendo-STARTY][1][nenrei] + koyouj_c[4][nendo-STARTY][2][nenrei] ;
        koyouj_c[5][nendo-STARTY][0][nenrei]
         = koyouj_c[5][nendo-STARTY][1][nenrei] + koyouj_c[5][nendo-STARTY][2][nenrei] ;
        koyouj_c[6][nendo-STARTY][0][nenrei]
         = koyouj_c[6][nendo-STARTY][1][nenrei] + koyouj_c[6][nendo-STARTY][2][nenrei] ;
      }
    }


  for ( nendo=STARTY+1; nendo<=KE+1; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=0; sei<=4; sei++ ) {
        jieigyoj_c[nendo-STARTY][sei][nenrei]
         = syugyoj_c[nendo-STARTY][sei][nenrei] - koyouj_c[0][nendo-STARTY][sei][nenrei] ;
      }
    }
  }

    for ( nendo=ROUDYR+1; nendo<=KE+1; nendo++ ) {
      for ( ii=1; ii<=9; ii++ ) {
        heikinh_c[ii][nendo-STARTY] = heikinh_c[ii][nendo-1-STARTY] ;
      }
    }
    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        for ( sei=1; sei<=4; sei++ ) {
          soroudh_c[1][nendo-STARTY][sei][nenrei]
           = koyouj_c[1][nendo-STARTY][sei][nenrei] * heikinh_c[1][nendo-STARTY] ;
          soroudh_c[2][nendo-STARTY][sei][nenrei]
           = koyouj_c[2][nendo-STARTY][sei][nenrei] * heikinh_c[2][nendo-STARTY] ;
          soroudh_c[3][nendo-STARTY][sei][nenrei]
           = koyouj_c[3][nendo-STARTY][sei][nenrei] * heikinh_c[3][nendo-STARTY] +koyouj_c[4][nendo-STARTY][sei][nenrei] * heikinh_c[4][nendo-STARTY]
             + koyouj_c[5][nendo-STARTY][sei][nenrei] * heikinh_c[5][nendo-STARTY] +koyouj_c[6][nendo-STARTY][sei][nenrei] * heikinh_c[6][nendo-STARTY];
          soroudh_c[4][nendo-STARTY][sei][nenrei]
           = jieigyoj_c[nendo-STARTY][sei][nenrei] * heikinh_c[7][nendo-STARTY] ;
          soroudh_c[0][nendo-STARTY][sei][nenrei]
           = soroudh_c[1][nendo-STARTY][sei][nenrei] + soroudh_c[2][nendo-STARTY][sei][nenrei]
              + soroudh_c[3][nendo-STARTY][sei][nenrei] + soroudh_c[4][nendo-STARTY][sei][nenrei];
        }
        for ( ii=0; ii<=4; ii++ ) {
          soroudh_c[ii][nendo-STARTY][2][nenrei]
            = soroudh_c[ii][nendo-STARTY][3][nenrei] + soroudh_c[ii][nendo-STARTY][4][nenrei] ;
          soroudh_c[ii][nendo-STARTY][0][nenrei]
            = soroudh_c[ii][nendo-STARTY][1][nenrei] + soroudh_c[ii][nendo-STARTY][2][nenrei] ;
        }
      }
    }


  for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
    if ( roudr_m[nendo-STARTY][1][30] <= 0.0 ) { 
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        for ( sei=1; sei<=4; sei++ ) {
          roudr_m[nendo-STARTY][sei][nenrei]
            = ( roudr_c[nendo-STARTY][sei][nenrei] + roudr_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( roudr_m[nendo-STARTY][sei][nenrei] > 1.0 ) { roudr_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( roudr_m[nendo-STARTY][sei][nenrei] < 0.0 ) { roudr_m[nendo-STARTY][sei][nenrei] = 0.0; }
          roudr_a_m[nendo-STARTY][sei][nenrei]
            = ( roudr_a_c[nendo-STARTY][sei][nenrei] + roudr_a_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( roudr_a_m[nendo-STARTY][sei][nenrei] > 1.0 ) { roudr_a_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( roudr_a_m[nendo-STARTY][sei][nenrei] < 0.0 ) { roudr_a_m[nendo-STARTY][sei][nenrei] = 0.0; }
          syuwari_m[nendo-STARTY][sei][nenrei] 
            = ( syuwari_c[nendo-STARTY][sei][nenrei] + syuwari_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( syuwari_m[nendo-STARTY][sei][nenrei] > 1.0 ) { syuwari_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( syuwari_m[nendo-STARTY][sei][nenrei] < 0.0 ) { syuwari_m[nendo-STARTY][sei][nenrei] = 0.0; }
          syuwari_a_m[nendo-STARTY][sei][nenrei] 
            = ( syuwari_a_c[nendo-STARTY][sei][nenrei] + syuwari_a_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( syuwari_a_m[nendo-STARTY][sei][nenrei] > 1.0 ) { syuwari_a_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( syuwari_a_m[nendo-STARTY][sei][nenrei] < 0.0 ) { syuwari_a_m[nendo-STARTY][sei][nenrei] = 0.0; }
          syugyor_m[nendo-STARTY][sei][nenrei]
            = ( syugyor_c[nendo-STARTY][sei][nenrei] + syugyor_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( syugyor_m[nendo-STARTY][sei][nenrei] > 1.0 ) { syugyor_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( syugyor_m[nendo-STARTY][sei][nenrei] < 0.0 ) { syugyor_m[nendo-STARTY][sei][nenrei] = 0.0; }
          syugyor_a_m[nendo-STARTY][sei][nenrei]
            = ( syugyor_a_c[nendo-STARTY][sei][nenrei] + syugyor_a_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
          if ( syugyor_a_m[nendo-STARTY][sei][nenrei] > 1.0 ) { syugyor_a_m[nendo-STARTY][sei][nenrei] = 1.0; }
          if ( syugyor_a_m[nendo-STARTY][sei][nenrei] < 0.0 ) { syugyor_a_m[nendo-STARTY][sei][nenrei] = 0.0; }
        }
      }
    }
  }

  for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=1; sei<=4; sei++ ) {

        koyour_m[nendo-STARTY][sei][nenrei]
          = ( koyour_c[nendo-STARTY][sei][nenrei] + koyour_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
        if ( koyour_m[nendo-STARTY][sei][nenrei] > 1.0 ) { koyour_m[nendo-STARTY][sei][nenrei] = 1.0; }
        if ( koyour_m[nendo-STARTY][sei][nenrei] < 0.0 ) { koyour_m[nendo-STARTY][sei][nenrei] = 0.0; }
        koyour_a_m[nendo-STARTY][sei][nenrei]
          = ( koyour_a_c[nendo-STARTY][sei][nenrei] + koyour_a_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
        if ( koyour_a_m[nendo-STARTY][sei][nenrei] > 1.0 ) { koyour_a_m[nendo-STARTY][sei][nenrei] = 1.0; }
        if ( koyour_a_m[nendo-STARTY][sei][nenrei] < 0.0 ) { koyour_a_m[nendo-STARTY][sei][nenrei] = 0.0; }
        seikir_m[nendo-STARTY][sei][nenrei]
          = ( seikir_c[nendo-STARTY][sei][nenrei] + seikir_c[nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
        if ( seikir_m[nendo-STARTY][sei][nenrei] > 1.0 ) { seikir_m[nendo-STARTY][sei][nenrei] = 1.0; }
        if ( seikir_m[nendo-STARTY][sei][nenrei] < 0.0 ) { seikir_m[nendo-STARTY][sei][nenrei] = 0.0; }
        hiseikir_m[nendo-STARTY][sei][nenrei]
          = 1- seikir_m[nendo-STARTY][sei][nenrei]  ;
        hiseikihurur_m[0][nendo-STARTY][sei][nenrei]
          = ( hiseikihurur_c[0][nendo-STARTY][sei][nenrei] + hiseikihurur_c[0][nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
        if ( hiseikihurur_m[0][nendo-STARTY][sei][nenrei] > 1.0 ) { hiseikihurur_m[0][nendo-STARTY][sei][nenrei] = 1.0; }
        if ( hiseikihurur_m[0][nendo-STARTY][sei][nenrei] < 0.0 ) { hiseikihurur_m[0][nendo-STARTY][sei][nenrei] = 0.0; }
        if ( hiseikihurur_m[0][nendo-STARTY][sei][nenrei]+seikir_m[nendo-STARTY][sei][nenrei] > 1.0 ) { hiseikihurur_m[0][nendo-STARTY][sei][nenrei] = 1.0 -seikir_m[nendo-STARTY][sei][nenrei]; }
		hiseikihurur_m[1][nendo-STARTY][sei][nenrei]
          = ( hiseikihurur_c[1][nendo-STARTY][sei][nenrei] + hiseikihurur_c[1][nendo+1-STARTY][sei][nenrei] ) / 2.0 ;
        if ( hiseikihurur_m[1][nendo-STARTY][sei][nenrei] > 1.0 ) { hiseikihurur_m[1][nendo-STARTY][sei][nenrei] = 1.0; }
        if ( hiseikihurur_m[1][nendo-STARTY][sei][nenrei] < 0.0 ) { hiseikihurur_m[1][nendo-STARTY][sei][nenrei] = 0.0; }
        if ( hiseikihurur_m[1][nendo-STARTY][sei][nenrei]+seikir_m[nendo-STARTY][sei][nenrei] > 1.0 ) { hiseikihurur_m[1][nendo-STARTY][sei][nenrei] = 1.0 -seikir_m[nendo-STARTY][sei][nenrei]; }
		hiseikitanr_m[0][nendo-STARTY][sei][nenrei]
          =  1.0 -hiseikihurur_m[0][nendo-STARTY][sei][nenrei] - seikir_m[nendo-STARTY][sei][nenrei] ;
		hiseikitanr_m[1][nendo-STARTY][sei][nenrei]
		  =  1.0 -hiseikihurur_m[1][nendo-STARTY][sei][nenrei] - seikir_m[nendo-STARTY][sei][nenrei] ;

        if ( nendo>= KIJUN+1 ) {
          if (( sei== 1) && ( nenrei >= 60 )) {
            if ( syugyor_m[nendo-STARTY][sei][nenrei] > syugyor_a_m[nendo-STARTY][sei][nenrei] ) {
              koyour_m[nendo-STARTY][sei][nenrei]
                = ( koyour_a_m[nendo-STARTY][sei][nenrei] * syugyor_a_m[nendo-STARTY][sei][nenrei]
                    + syugyor_m[nendo-STARTY][sei][nenrei] - syugyor_a_m[nendo-STARTY][sei][nenrei] )
                  / syugyor_m[nendo-STARTY][sei][nenrei] ;
            }
          }
          if  ( nenrei >= 76 ) {
			koyour_m[nendo-STARTY][sei][nenrei]=	koyour_m[nendo-STARTY][sei][75];
	  	  }
        }

      }
    }
  }




  for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        roudj_m[nendo-STARTY][sei][nenrei]
          = jinko_m[nendo-STARTY][sei][nenrei] * roudr_m[nendo-STARTY][sei][nenrei] ;
        syugyoj_m[nendo-STARTY][sei][nenrei]
          = roudj_m[nendo-STARTY][sei][nenrei] * syuwari_m[nendo-STARTY][sei][nenrei] ;
        koyouj_m[0][nendo-STARTY][sei][nenrei]
          = syugyoj_m[nendo-STARTY][sei][nenrei] * koyour_m[nendo-STARTY][sei][nenrei] ;
      }
      /*  女性計  */
      roudj_m[nendo-STARTY][2][nenrei]
        = roudj_m[nendo-STARTY][3][nenrei] + roudj_m[nendo-STARTY][4][nenrei] ;
      syugyoj_m[nendo-STARTY][2][nenrei]
        = syugyoj_m[nendo-STARTY][3][nenrei] + syugyoj_m[nendo-STARTY][4][nenrei] ;
      koyouj_m[0][nendo-STARTY][2][nenrei]
        = koyouj_m[0][nendo-STARTY][3][nenrei] + koyouj_m[0][nendo-STARTY][4][nenrei] ;
      /*  男女計  */
      roudj_m[nendo-STARTY][0][nenrei]
        = roudj_m[nendo-STARTY][1][nenrei] + roudj_m[nendo-STARTY][2][nenrei] ;
      syugyoj_m[nendo-STARTY][0][nenrei]
        = syugyoj_m[nendo-STARTY][1][nenrei] + syugyoj_m[nendo-STARTY][2][nenrei] ;
      koyouj_m[0][nendo-STARTY][0][nenrei]
        = koyouj_m[0][nendo-STARTY][1][nenrei] + koyouj_m[0][nendo-STARTY][2][nenrei] ;
    }
  }

    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      hiseikitanr_all_m[2][nendo-STARTY]
        = ( hiseikitanr_all_c[2][nendo-STARTY] + hiseikitanr_all_c[2][nendo+1-STARTY] ) / 2.0 ;
      hiseikitanr_all_m[3][nendo-STARTY]
        = ( hiseikitanr_all_c[3][nendo-STARTY] + hiseikitanr_all_c[3][nendo+1-STARTY] ) / 2.0 ;
      hiseikitanr_all_m[4][nendo-STARTY]
        = ( hiseikitanr_all_c[4][nendo-STARTY] + hiseikitanr_all_c[4][nendo+1-STARTY] ) / 2.0 ;
      hiseikitanr_all_m[5][nendo-STARTY]
        = ( hiseikitanr_all_c[5][nendo-STARTY] + hiseikitanr_all_c[5][nendo+1-STARTY] ) / 2.0 ;
      hiseikitanr_all_m[6][nendo-STARTY]
        = ( hiseikitanr_all_c[6][nendo-STARTY] + hiseikitanr_all_c[6][nendo+1-STARTY] ) / 2.0 ;
    }

    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      TMP = 0.0; TMQ = 0.0; TMR = 0.0;
      if ( nendo<=ROUDYR ) {
        for ( nenrei=15; nenrei<=79; nenrei++ ) {
          	TMP += koyouj_m[0][nendo-STARTY][1][nenrei] * hiseikitanr_m[0][nendo-STARTY][1][nenrei] 
            	   + koyouj_m[0][nendo-STARTY][3][nenrei] * hiseikitanr_m[0][nendo-STARTY][3][nenrei]
            	   + koyouj_m[0][nendo-STARTY][4][nenrei] * hiseikitanr_m[0][nendo-STARTY][4][nenrei];
          	TMQ += koyouj_m[0][nendo-STARTY][1][nenrei] 
            	   + koyouj_m[0][nendo-STARTY][3][nenrei]
            	   + koyouj_m[0][nendo-STARTY][4][nenrei];
        }
          if ( TMQ > 0.0 ) {
          	TMR = TMP / TMQ ;
	         if ( TANCHO == 0 ){
	         	for ( nenrei=15; nenrei<=100; nenrei++ ){
	          	tankor_cho_m[nendo-STARTY][1][nenrei] = hiseikitanr_all_m[2][nendo-STARTY] / TMR ;
	          	tankor_cho_m[nendo-STARTY][2][nenrei] = hiseikitanr_all_m[2][nendo-STARTY] / TMR ;
	         	}
	         }
	         else if(TANCHO ==1){
          		if( hiseikitanr_all_m[2][nendo-STARTY] <= TMR){
          			for ( nenrei=15; nenrei<=100; nenrei++ ){
	          		tankor_cho_m[nendo-STARTY][1][nenrei] = hiseikitanr_all_m[2][nendo-STARTY] / TMR ;  
	          		tankor_cho_m[nendo-STARTY][2][nenrei] = hiseikitanr_all_m[2][nendo-STARTY] / TMR ;
    	    		}
    	    	}
    	    	else {
    	    	     flg_end = 1;
 		       		 for (xx=1;xx<10000 && flg_end>0; xx++){
    	    	     	TMP = 0.0; TMQ = 0.0; TMR = 0.0;
 		       		 	for ( nenrei= XR ; nenrei<=79; nenrei++ ){
 		       		 		hiseikitanr_m[1][nendo-STARTY][1][nenrei] =(1.000 + xx * 0.001) * hiseikitanr_m[0][nendo-STARTY][1][nenrei] ;
 		       		 	}
 		       		 	for ( nenrei= XR1 ; nenrei<=79; nenrei++ ){
 		       		 		hiseikitanr_m[1][nendo-STARTY][2][nenrei] =(1.000 + xx * 0.001) * hiseikitanr_m[0][nendo-STARTY][2][nenrei];
 		       		 	}
 		       		 	for ( nenrei=15; nenrei<=79; nenrei++ ) {
 		       		 	TMP += koyouj_m[0][nendo-STARTY][1][nenrei] * hiseikitanr_m[1][nendo-STARTY][1][nenrei] 
            	   				+ koyouj_m[0][nendo-STARTY][2][nenrei] * hiseikitanr_m[1][nendo-STARTY][2][nenrei];
          				TMQ += koyouj_m[0][nendo-STARTY][1][nenrei] 
            	   				+ koyouj_m[0][nendo-STARTY][2][nenrei];
            	   		}
            	   		TMR = TMP / TMQ ;
            	   		if (TMR >= hiseikitanr_all_m[2][nendo-STARTY] ){
            	   			flg_end = 0;
            	   			xx1 = xx;
            	   		}
            	   	}
            	   	for ( nenrei=15; nenrei<=100; nenrei++ ){
            	   	tankor_cho_m[nendo-STARTY][1][nenrei] =1.000;
            	   	tankor_cho_m[nendo-STARTY][2][nenrei] =1.000;
            	   	}
            	   	for ( nenrei= XR ; nenrei<=79; nenrei++ ){
            	   	tankor_cho_m[nendo-STARTY][1][nenrei] =(1.000 + xx1 * 0.001);
            		}
            		for ( nenrei= XR1 ; nenrei<=79; nenrei++ ){
            	   	tankor_cho_m[nendo-STARTY][2][nenrei] =(1.000 + xx1 * 0.001);
            		}
            	}
            }
        }
        else {
        	for ( nenrei=15; nenrei<=100; nenrei++ ){
          	tankor_cho_m[nendo-STARTY][1][nenrei] = 0.0 ;
          	tankor_cho_m[nendo-STARTY][2][nenrei] = 0.0 ;
        	}
        }
      }
      else { 
      	for ( nenrei=15; nenrei<=100; nenrei++ ){
        tankor_cho_m[nendo-STARTY][1][nenrei] = tankor_cho_m[nendo-1-STARTY][1][nenrei] ;  
        tankor_cho_m[nendo-STARTY][2][nenrei] = tankor_cho_m[nendo-1-STARTY][2][nenrei] ;  
        }
      }
	  for ( nenrei=15; nenrei<=100; nenrei++ ){
	  	tankor_cho_m[nendo-STARTY][3][nenrei] =tankor_cho_m[nendo-STARTY][2][nenrei] ;
	  	tankor_cho_m[nendo-STARTY][4][nenrei] =tankor_cho_m[nendo-STARTY][2][nenrei] ;
	  }
      for ( sei=1; sei<=4; sei++ ) {
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          hiseikitanr_m[1][nendo-STARTY][sei][nenrei]
           = hiseikitanr_m[0][nendo-STARTY][sei][nenrei] * tankor_cho_m[nendo-STARTY][sei][nenrei] ;
          if ( hiseikitanr_m[1][nendo-STARTY][sei][nenrei]+seikir_m[nendo-STARTY][sei][nenrei] > 1.0 ) {
            hiseikitanr_m[1][nendo-STARTY][sei][nenrei] = 1.0 -seikir_m[nendo-STARTY][sei][nenrei];
          }
          hiseikihurur_m[1][nendo-STARTY][sei][nenrei] = 1.0 - hiseikitanr_m[1][nendo-STARTY][sei][nenrei] -seikir_m[nendo-STARTY][sei][nenrei];
        }
      }
    }

    for ( nendo=STARTY+1; nendo<=KE+1; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        for ( sei=1; sei<=4; sei++ ) {

          koyouj_m[1][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * seikir_m[nendo-STARTY][sei][nenrei] ;

          koyouj_m[2][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * hiseikihurur_m[1][nendo-STARTY][sei][nenrei] ;

          koyouj_m[3][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * hiseikitanr_m[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_m[3][nendo-STARTY] ;

          koyouj_m[4][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * hiseikitanr_m[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_m[4][nendo-STARTY] ;

          koyouj_m[5][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * hiseikitanr_m[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_m[5][nendo-STARTY] ;

          koyouj_m[6][nendo-STARTY][sei][nenrei]
           = koyouj_m[0][nendo-STARTY][sei][nenrei] * hiseikitanr_m[1][nendo-STARTY][sei][nenrei] * hiseikitanr_all_m[6][nendo-STARTY] ;
        }
        koyouj_m[1][nendo-STARTY][2][nenrei]
         = koyouj_m[1][nendo-STARTY][3][nenrei] + koyouj_m[1][nendo-STARTY][4][nenrei] ;
        koyouj_m[2][nendo-STARTY][2][nenrei]
         = koyouj_m[2][nendo-STARTY][3][nenrei] + koyouj_m[2][nendo-STARTY][4][nenrei] ;
        koyouj_m[3][nendo-STARTY][2][nenrei]
         = koyouj_m[3][nendo-STARTY][3][nenrei] + koyouj_m[3][nendo-STARTY][4][nenrei] ;
        koyouj_m[4][nendo-STARTY][2][nenrei]
         = koyouj_m[4][nendo-STARTY][3][nenrei] + koyouj_m[4][nendo-STARTY][4][nenrei] ;
        koyouj_m[5][nendo-STARTY][2][nenrei]
         = koyouj_m[5][nendo-STARTY][3][nenrei] + koyouj_m[5][nendo-STARTY][4][nenrei] ;
        koyouj_m[6][nendo-STARTY][2][nenrei]
         = koyouj_m[6][nendo-STARTY][3][nenrei] + koyouj_m[6][nendo-STARTY][4][nenrei] ;
        /* 男女計 */
        koyouj_m[1][nendo-STARTY][0][nenrei]
         = koyouj_m[1][nendo-STARTY][1][nenrei] + koyouj_m[1][nendo-STARTY][2][nenrei] ;
        koyouj_m[2][nendo-STARTY][0][nenrei]
         = koyouj_m[2][nendo-STARTY][1][nenrei] + koyouj_m[2][nendo-STARTY][2][nenrei] ;
        koyouj_m[3][nendo-STARTY][0][nenrei]
         = koyouj_m[3][nendo-STARTY][1][nenrei] + koyouj_m[3][nendo-STARTY][2][nenrei] ;
        koyouj_m[4][nendo-STARTY][0][nenrei]
         = koyouj_m[4][nendo-STARTY][1][nenrei] + koyouj_m[4][nendo-STARTY][2][nenrei] ;
        koyouj_m[5][nendo-STARTY][0][nenrei]
         = koyouj_m[5][nendo-STARTY][1][nenrei] + koyouj_m[5][nendo-STARTY][2][nenrei] ;
        koyouj_m[6][nendo-STARTY][0][nenrei]
         = koyouj_m[6][nendo-STARTY][1][nenrei] + koyouj_m[6][nendo-STARTY][2][nenrei] ;
      }
    }


  for ( nendo=STARTY+1; nendo<=KE+1; nendo++ ) {
    for ( nenrei=15; nenrei<=100; nenrei++ ) {
      for ( sei=0; sei<=4; sei++ ) {
        jieigyoj_m[nendo-STARTY][sei][nenrei]
         = syugyoj_m[nendo-STARTY][sei][nenrei] - koyouj_m[0][nendo-STARTY][sei][nenrei] ;
      }
    }
  }


    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      for ( ii=1; ii<=9; ii++ ) {
        heikinh_m[ii][nendo-STARTY]
          = ( heikinh_c[ii][nendo-STARTY] + heikinh_c[ii][nendo+1-STARTY] ) / 2.0 ;
      }
    }
    
    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        for ( sei=1; sei<=4; sei++ ) {
          soroudh_m[1][nendo-STARTY][sei][nenrei]
           = koyouj_m[1][nendo-STARTY][sei][nenrei] * heikinh_m[1][nendo-STARTY] ;
          soroudh_m[2][nendo-STARTY][sei][nenrei]
           = koyouj_m[2][nendo-STARTY][sei][nenrei] * heikinh_m[2][nendo-STARTY] ;
          soroudh_m[3][nendo-STARTY][sei][nenrei]
           = koyouj_m[3][nendo-STARTY][sei][nenrei] * heikinh_m[3][nendo-STARTY] +koyouj_m[4][nendo-STARTY][sei][nenrei] * heikinh_m[4][nendo-STARTY]
             + koyouj_m[5][nendo-STARTY][sei][nenrei] * heikinh_m[5][nendo-STARTY] +koyouj_m[6][nendo-STARTY][sei][nenrei] * heikinh_m[6][nendo-STARTY];
          soroudh_m[4][nendo-STARTY][sei][nenrei]
           = jieigyoj_m[nendo-STARTY][sei][nenrei] * heikinh_m[7][nendo-STARTY] ;
          soroudh_m[0][nendo-STARTY][sei][nenrei]
           = soroudh_m[1][nendo-STARTY][sei][nenrei] + soroudh_m[2][nendo-STARTY][sei][nenrei]
              + soroudh_m[3][nendo-STARTY][sei][nenrei] + soroudh_m[4][nendo-STARTY][sei][nenrei];
        }
        for ( ii=0; ii<=4; ii++ ) {
          soroudh_m[ii][nendo-STARTY][2][nenrei]
            = soroudh_m[ii][nendo-STARTY][3][nenrei] + soroudh_m[ii][nendo-STARTY][4][nenrei] ;
          soroudh_m[ii][nendo-STARTY][0][nenrei]
            = soroudh_m[ii][nendo-STARTY][1][nenrei] + soroudh_m[ii][nendo-STARTY][2][nenrei] ;
        }
      }
    }







}

