Click here to Skip to main content
15,885,921 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
//c++
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, a, b;
        cin >> n >> a >> b;

        int x = min(a, b);
        int y = max(a, b);
        int ans = n;

        for (int i = x; i < y; i++)
        {
            int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
            ans = min(ans, temp);
        }

        cout << ans <<endl;
    }
}


What I have tried:

//c lang

#include <stdio.h>
#define min(x,y) (x <= y ? x : y)
#define max(x,y) (x <= y ? x : y)

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n, a, b;
        scanf("%d%d%d",&n,&a,&b);

        int x = min(a, b);
        int y = max(a, b);
        int ans = n;

        for (int i = x; i < y; i++)
        {
            int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
            ans = min(ans, temp);
        }

        printf(ans,"\n");
    }
}

error
timeout: the monitored command dumped core
Posted
Updated 26-May-21 11:35am
v2
Comments
Dave Kreskowiak 25-May-21 12:16pm    
That's not "your code". That's code you got off the web and modified so it wouldn't be so obvious you're using someone's code.

Now you want it converted to C so you can turn in someone's work as your own.

See __min | Microsoft Docs[^].

[edit]

Maybe you are using gcc in which case you will need to create your own macros or inline functions for min and max. something like:
C++
#define min(x,y) (x <= y ? x : y)


[/edit]
 
Share this answer
 
v2
Comments
CPallini 25-May-21 7:04am    
5.
Richard MacCutchan 25-May-21 7:48am    
Thanks.
NISHANK BABU 26-May-21 3:26am    
#include <stdio.h>
#define min(x,y) (x <= y ? x : y)
#define max(x,y) (x <= y ? x : y)

int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n, a, b;
scanf("%d%d%d",&n,&a,&b);

int x = min(a, b);
int y = max(a, b);
int ans = n;

for (int i = x; i < y; i++)
{
int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
ans = min(ans, temp);
}

printf(ans,"\n");
}
}
error
timeout: the monitored command dumped core
Richard MacCutchan 26-May-21 3:38am    
You have defined max the same as min. The max definition needs to test x as greater than y.
NISHANK BABU 26-May-21 3:40am    
i didn't get u..
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And to be honest, if you wrote that code in C++, you should be able to rewrite it for C in minutes, if not seconds - C is a subset of C++.
 
Share this answer
 
I suggest activating the compiler flags for warnings. With gcc this would be -Wall and -Wconversion for example.

An error message like the following should definitely be displayed:
C
error C2664: "int printf(const char *const ,...)" 


Thats because
C
printf(ans,"\n");

Is definitely not functional.

As already written, the macro for max () is wrong.

The loop works, but it probably doesn't make much sense:
C
for (int i = x; i < y; i++) {
  int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
  ans = min(ans, temp);
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900