| Задача о городах – решение |
|
|
Sunday, 23 April 2006 | Зэев Фрайман для раздела Ученье – свет Это – решение задачи, условие которой мы опубликовали в предыдущем материале. Сохранен тот вариант, который предложен авторами-составителями; мне кажется, что в рамках тех дополнений, которые, скорее всего, будут введены в рамках нашей школьной программы, решение (как, впрочем, и условие), можно и даже следует немного изменить – в первую очередь в отношении ввода и вывода данных и результатов. Ну а пока, как сказано, перед вами вариант составителей.// Class Living - research on standard of living in big cities
public class Living {
private int [] [] standards =
{{ 65, 70, 75,80 },
{ 80, 85, 78, 76 },
{ 89, 93, 95, 92 },
{ 60, 65, 59, 61 },
{ 76, 82, 84, 80 },
{ 62, 65, 72, 74 },
{ 83, 80, 85, 82 }};
private String [] contin =
{ "Europe", "N. America", "Australia", "Africa", "N. America", "Africa", "Australia"};
private double [] living;
private int [] numbers;
public Living()
{
living = new double [standards.length ];
numbers = new int [standards.length ];
}
// compute the standard of living
private void compute_living ()
{
for ( int i=0;i < standards.length; i++)
living [i] = 0;
for ( int i=0; i < standards.length; i++)
{
for ( int j=0; j < 4;j++)
living [i] = living [i] + standards [i][j] ;
living [i] = living [i] /4.0;
}
}
// compute the percent of cities in a given continent
// with a very high standard of living
public double very_high (String s)
{
int n_cont = 0;
int n_high = 0;
compute_living();
for ( int i=0; i< living.length; i++)
{
if (contin[i].equals (s) )
{
n_cont ++;
if ( living[i] > 85)
n_high ++;
}
}
if (n_cont == 0)
return -1;
else
return 100.0/ n_cont * n_high;
}
// compute the percent of cities in a given continent
// with a very low standard of living
public double very_low (String s)
{
int n_cont = 0;
int n_low = 0;
compute_living();
for ( int i=0; i< living.length; i++)
{
if (contin[i].equals (s) )
{
n_cont ++;
if ( living[i] <= 65)
n_low ++;
}
}
if (n_cont == 0)
return -1;
else
return 100.0/ n_cont * n_low;
}
// compute the percent of cities in a given continent
// with a standard of living higher than the value in the parameter
public double higher (String s, int value)
{
int n_cont = 0;
int n_high = 0;
compute_living();
for ( int i=0; i< living.length; i++)
{
if (contin[i].equals (s) )
{
n_cont ++;
if ( living[i] > value )
n_high ++;
}
}
if (n_cont == 0)
return -1;
else
return 100.0/ n_cont * n_high;
}
// list the id numbers of cities in which the level of safety is lower than
// the other levels
public int safety ()
{
int n = 0;
for ( int i=0; i < standards.length; i++)
{
if ( standards [i][0] < standards [i] [1] && standards [i][0] < standards [i] [2]
&& standards [i] [0] < standards [i] [3] )
{
numbers [n] = i;
n ++;
}
}
for ( int i=0; i System.out.println (numbers[i]);
return n;
}
// list the id numbers of cities in which the level of education is higher than
// the other levels
public int educ ()
{
int n = 0;
for ( int i=0; i < standards.length; i++)
{
if ( standards [i][2] > standards [i] [0] && standards [i][2] > standards [i] [1]
&& standards [i] [2] > standards [i] [3] )
{
numbers [n] = i;
n ++;
}
}
for ( int i=0; i System.out.println (numbers[i]);
return n;
}
// list the id numbers of cities in which the level of education is very close
// to the level of safety
public int educ_safety ()
{
int n = 0;
for ( int i=0; i < standards.length; i++)
{
if ( Math.abs (standards [i][0] - standards [i] [2] ) < 5 )
{
numbers [n] = i;
n ++;
}
}
for ( int i=0; i
System.out.println (numbers[i]);
return n;
}
} |