CREATE TABLE db_person
(
id integer,
stat integer
);
CREATE TABLE db_person_fio
(
id_person integer,
dr date,
w integer
);
CREATE TABLE db_person_ages
(
age integer
);
INSERT INTO db_person_ages(age) VALUES (0);
INSERT INTO db_person_ages(age) VALUES (1);
INSERT INTO db_person_ages(age) VALUES (2);
INSERT INTO db_person_ages(age) VALUES (3);
INSERT INTO db_person_ages(age) VALUES (4);
INSERT INTO db_person_ages(age) VALUES (5);
INSERT INTO db_person_ages(age) VALUES (6);
INSERT INTO db_person_ages(age) VALUES (7);
INSERT INTO db_person_ages(age) VALUES (8);
INSERT INTO db_person_ages(age) VALUES (9);
INSERT INTO db_person_ages(age) VALUES (10);
SELECT CONCAT(age*11,'-',age*11+10) as descr,
IFNULL(man,0) as man,
IFNULL(woman,0) as woman
FROM db_person_ages ages
LEFT JOIN
(
SELECT FLOOR(timestampdiff(YEAR,f.dr,CURRENT_DATE)/11) as diff,
SUM(CASE WHEN f.w=1 THEN 1 ELSE 0 END) as man,
SUM(CASE WHEN f.w=0 THEN 1 ELSE 0 END) as woman
FROM db_person p, db_person_fio f
WHERE p.id = f.id_person
AND p.stat>0
GROUP BY 1
) t ON ages.age = t.diff
//функция возвращает список делителей числа
public static List<int> getDenaminators(int num)
{
List<int> result = new List<int>();
for (int i = 2; i < num - 1; i++)
{
if (num % i == 0)
result.Add(i);
}
return result;
}
public static string Func(int num, int count)
{
List<int> deliteli = getDenaminators(num);
Random rnd = new Random();
int key;
string numString = "", operString = "", resultString = "";
int nextVal = 0;
if (count == 0)
return num.ToString();
if (deliteli.Count > 0)
key = rnd.Next(1, 5);
else
key = rnd.Next(1, 4);
switch (key)
{
case 1:
{
nextVal = rnd.Next(1, num);
numString = (num - nextVal).ToString();
operString = "+";
} break;
case 2:
{
nextVal = rnd.Next(1, num);
numString = (num + nextVal).ToString();
operString = "-";
} break;
case 3:
{
nextVal = rnd.Next(1, 11);
numString = (num * nextVal).ToString();
operString = "/";
} break;
case 4:
{
nextVal = deliteli[rnd.Next(0, deliteli.Count)];
numString = (num / nextVal).ToString();
operString = "*";
} break;
}
if (count == 1)
resultString = String.Format("{0} {1} {2}", numString, operString, Func(nextVal, count - 1));
else
resultString = String.Format("{0} {1} ({2})", numString, operString, Func(nextVal, count - 1));
return resultString;
}
static void Main(string[] args)
{
Console.WriteLine(Func(1024, 6));
}