"Knapsack problem" işime yarar gibiydi ancak bir türlü kendime uyarlayamadım; ya da koda aktaramadım diyelim. Kendim bir şeyler yazmaya çalıştım. 1-170 arasını farklı aralıklara böldüm, her aralıkta hesaplama farklı oluyor. Aralıkları da tabii en az dart atışına ve en kolay atılacak yerlere göre böldüm. Kodun içinde milyonlarca if-else var ancak anlaşılıyor gibi, hepsi bir aralığı belirtiyor. Girdiğimiz değer negatif olunca ise bir önceki sayıdan düşüp yeni sayıya göre işlem yapıyor. İstediğim gibi çalışıyor. Paylaşayım.
#include <stdio.h>
#include <stdlib.h>
int number;
int previous_number;
int first_dart;
int second_dart;
int final_dart;
int needed;
void calculation()
{
if(number > 170 || (number < 2 && number >= 0))
{
printf("Can not finish!\n\n");
}
else
{
if(number <= 40 || number == 50)
{
if(number % 2 == 0)
{
final_dart = number/2;
printf("D%d\n\n", final_dart);
}
else
{
final_dart = (number - 1)/2;
printf("1, D%d\n\n", final_dart);
}
}
else if((number >= 41 && number <= 60) || number == 65)
{
needed = number - 40;
first_dart = needed;
final_dart = 20;
printf("%d, D%d\n\n", first_dart, final_dart);
}
else if(number >= 61 && number <= 70)
{
needed = number - 50;
first_dart = needed;
final_dart = 25;
printf("%d, D%d\n\n", first_dart, final_dart);
}
else if(number >= 71 && number <= 100)
{
if((number - 40) % 2 == 1)
{
if(number == 99)
{
printf("D20, 19, D20\n\n");
}
else
{
needed = (number - 57)/2;
first_dart = 19;
final_dart = needed;
printf("T%d, D%d\n\n", first_dart, final_dart);
}
}
else
{
if(number >= 72 && number <= 80)
{
needed = (number - 40)/2;
first_dart = needed;
final_dart = 20;
printf("D%d, D%d\n\n", first_dart, final_dart);
}
else
{
needed = (number - 60)/2;
first_dart = 20;
final_dart = needed;
printf("T%d, D%d\n\n", first_dart, final_dart);
}
}
}
else if(number >= 101 && number <= 120)
{
if((number - 102) % 3 == 0 || (number - 103) % 3 == 0)
{
first_dart = 20;
second_dart = (number - 100);
final_dart = 20;
printf("T%d, %d, D%d\n\n", first_dart, second_dart, final_dart);
}
else
{
if(number >= 101 && number <= 110)
{
needed = (number - 50)/3;
first_dart = needed;
final_dart = 25;
printf("T%d, D%d\n\n", first_dart, final_dart);
}
else
{
needed = number - 100;
first_dart = 20;
second_dart = needed;
final_dart = 20;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
}
}
else if(number >= 121 && number <= 150)
{
if(number % 3 == 0)
{
needed = (number - 30)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 15;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
else if(number % 3 == 1)
{
needed = (number - 40)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 20;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
else
{
needed = (number - 50)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 25;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
}
else if(number > 150)
{
if(number < 160)
{
if((number - 150)%3 == 0)
{
printf("Can not finish!\n\n");
}
else if((number - 150)%3 == 1)
{
needed = (number - 40)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 20;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
else
{
needed = (number - 50)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 25;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
}
else
{
if((number - 160) % 3 == 1)
{
needed = (number - 50)/3;
first_dart = 20;
second_dart = needed - 20;
final_dart = 25;
printf("T%d, T%d, D%d\n\n", first_dart, second_dart, final_dart);
}
else
{
printf("Can not finish!\n\n");
}
}
}
}
}
int main()
{
while(1)
{
printf("Number: \n");
scanf("%d", &number);
if(number < 0)
{
number = previous_number + number;
calculation();
}
else
{
calculation();
}
previous_number = number;
}
}