Tobbelang
Tobbelang
CC#
Created by Tobbelang on 3/15/2024 in #help
✅ Need help with recursive solution on vjudge Homer Simpson UVA - 10465 problem with C
I am trying to solve https://vjudge.net/problem/UVA-10465 or at least get "Time exceeded" (3000ms) instead of "Wrong answer" with my current code. In VSCode I've tried several cases and seem to get the correct answer, Any help or tips to improve my code (but needs to be a recursive function) will be greatly appreciated When testing code on the website u can hand it in as c++ instead of c and it will compile My code:
#include <stdio.h>

int recursion(int m, int n, int t, int *hamburger, int *beer) {
if(t <= 0 || (t < n && t < m)) {
*beer = t;
return *hamburger;
} else {
int best_time_burger = m;
int worst_time_burger = n;
if (n < m) {
best_time_burger = n;
worst_time_burger = m;
}
int burger_time = worst_time_burger;


if (t % best_time_burger == 0) {
*hamburger += t / best_time_burger;
t = burger_time;
} else if(t-worst_time_burger < worst_time_burger && t-worst_time_burger != 0) {
burger_time = best_time_burger;
*hamburger+=1;
} else {
*hamburger+=1;
}

return recursion(m, n, t-burger_time, hamburger, beer);
}
}

int main(void) {
int m, n, t;
while (scanf("%d %d %d", &m, &n, &t) == 3) {
int hamburger = 0;
int beer = 0;
printf("%d ", recursion(m, n, t, &hamburger, &beer));
if(beer > 0) {
printf("%d", beer);
}
printf("\n");
}

return 0;
}
#include <stdio.h>

int recursion(int m, int n, int t, int *hamburger, int *beer) {
if(t <= 0 || (t < n && t < m)) {
*beer = t;
return *hamburger;
} else {
int best_time_burger = m;
int worst_time_burger = n;
if (n < m) {
best_time_burger = n;
worst_time_burger = m;
}
int burger_time = worst_time_burger;


if (t % best_time_burger == 0) {
*hamburger += t / best_time_burger;
t = burger_time;
} else if(t-worst_time_burger < worst_time_burger && t-worst_time_burger != 0) {
burger_time = best_time_burger;
*hamburger+=1;
} else {
*hamburger+=1;
}

return recursion(m, n, t-burger_time, hamburger, beer);
}
}

int main(void) {
int m, n, t;
while (scanf("%d %d %d", &m, &n, &t) == 3) {
int hamburger = 0;
int beer = 0;
printf("%d ", recursion(m, n, t, &hamburger, &beer));
if(beer > 0) {
printf("%d", beer);
}
printf("\n");
}

return 0;
}
4 replies