Jump to content
Byte-ul

[Mini] C challenge

Recommended Posts

Inlocuiti "...." mai jos pentru a afisa "Muie Dragnea" in urma rularii.

 

int main()
{
  if (....)
    printf("Muie ");
  else
    printf("Dragnea");
  
  return 0;
}

Fara printf, cout, puts, etc :)

 

Au rezolvat: @RAZOR1g, @Philip.J.Fry, @Hertz, @kznamst, @Nytro, @adyshake

Fara stackoverflow bai bulangiilor :)) 

 

Raspunsurile pe PM.

Edited by Byte-ul
  • Upvote 7

Share this post


Link to post
Share on other sites

De ce solutiile numai pe privat?

Cred ca ar fi mai misto sa fie facute publice, astfel poate am invata unele chestii noi.

 

 

Share this post


Link to post
Share on other sites

Pune solutiile pt curiosi.

 

EDIT: Eu l-am rezolvat cred. Astept confirmarea.

Edited by yoyois

Share this post


Link to post
Share on other sites
int main()
{
  if (ChuckNorris())
    printf("Muie ");
  else
    printf("Dragnea");
  
  return 0;
}

Era banal...

  • Upvote 1

Share this post


Link to post
Share on other sites
34 minutes ago, Nytro said:

int main()
{
  if (ChuckNorris())
    printf("Muie ");
  else
    printf("Dragnea");
  
  return 0;
}

Era banal...

 

Nu cred ca toti stiu de "

You must be logged in to see the content.
"

 

"Hrusca stie cat dureaza vesnica pomenire"

Edited by BogdanNBV

Share this post


Link to post
Share on other sites

Cel mai usor era:

 

int main()
{
  if (fork()) printf("Muie ");
  else printf("Dragnea");
}

Nu mai e nevoie de

return 0;

ca nu mai suntem in 2008.

  • Upvote 1

Share this post


Link to post
Share on other sites
22 minutes ago, MrGrj said:

Cel mai usor era:

 


int main()
{
  if (fork()) printf("Muie ");
  else printf("Dragnea");
}

Nu mai e nevoie de


return 0;

ca nu mai suntem in 2008.

Bai garaj lasă c-ul ca ești paralel cu el :)) Ramai la Python. 

Share this post


Link to post
Share on other sites
2 minutes ago, Byte-ul said:

Bai garaj lasă c-ul ca ești paralel cu el :)) Ramai la Python. 

 

How come ? :)

 

Share this post


Link to post
Share on other sites
1 hour ago, MrGrj said:

 

Nu mai e nevoie de


return 0;

ca nu mai suntem in 2008.

Am citit si eu ceva asemanator intr-o carte de C++, a fost nasoala cartea..

1 hour ago, MrGrj said:

 

How come ? :)

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(void) {
    pid_t child_pid;
    char *tasks[] = {"Task1", "Task2", "Task3", "Task4", "Task5"};
    const int task_no = 5;
    for (int i = 0; i < task_no; ++i) {
        child_pid = fork();
        if (child_pid == 0) { // Child
            printf("Child: I got to do %s\n", tasks[i]);
            _exit(i);
        } else { // Parrent
            printf("Parrent: Spawned child %d\n", i);
        }
    }
 
    printf("\nI'm going to wait for my children now.\n");
    printf("Hopefully they don't turn into zombies\n\n");
    int status = -1;
    for (int i = 0; i < task_no; ++i) {
        wait(&status);
        if (WIFEXITED(status)) {
            printf("Parrent: My child is not a zombie, yay.\n");
            printf("Parrent: Child %d finished, yay!\n", WEXITSTATUS(status));
        }
    }
    return 0;
}

 

Edited by Philip.J.Fry
  • Upvote 1

Share this post


Link to post
Share on other sites
3 minutes ago, Philip.J.Fry said:

Am citit si eu ceva asemanator intr-o carte de C++, a fost nasoala cartea..


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(void) {
    pid_t child_pid;
    char *tasks[] = {"Task1", "Task2", "Task3", "Task4", "Task5"};
    const int task_no = 5;
    for (int i = 0; i < task_no; ++i) {
        child_pid = fork();
        if (child_pid == 0) { // Child
            printf("Child: I got to do %s\n", tasks[i]);
            _exit(i);
        } else { // Parrent
            printf("Parrent: Spawned child %d\n", i);
        }
    }
 
    printf("\nI'm going to wait for my children now.\n");
    printf("Hopefully they don't turn into zombies\n\n");
    int status = -1;
    for (int i = 0; i < task_no; ++i) {
        wait(&status);
        if (WIFEXITED(status)) {
            printf("Parrent: My child is not a zombie, yay.\n");
            printf("Parrent: Child %d finished, yay!\n", WEXITSTATUS(status));
        }
    }
    return 0;
}

 

 

Nu inteleg ce vrei sa demonstrezi cu postul de mai sus :) 

 

 

Share this post


Link to post
Share on other sites
23 hours ago, MrGrj said:

 

How come ? :)

 

Am uitat sa-ti raspund. Compileaza aia si iti dai seama singur.

tl;dr unele standarde C au nevoie de return in main sau rezulta undefined behavior.

Edited by Byte-ul

Share this post


Link to post
Share on other sites

Zygote performs a fork, creating a new process that is a clone of itself, drops privileges and sets its UID appropriately for the application’s sandbox, and finishes initialization of Dalvik in that process so that the Java runtime is fully executing. For example, it must start threads like the garbage collector after it forks.

Share this post


Link to post
Share on other sites
On 3/22/2017 at 0:19 PM, gigiRoman said:

Zygote performs a fork, creating a new process that is a clone of itself, drops privileges and sets its UID appropriately for the application’s sandbox, and finishes initialization of Dalvik in that process so that the Java runtime is fully executing. For example, it must start threads like the garbage collector after it forks.

Ce treaba are Androidul cu challenge-ul asta ?

Share this post


Link to post
Share on other sites
5 hours ago, Okjokes said:

Da? Care-s alea?

prima pui in if prima conditie negata , si a2a , egalezi prima conditie cu ce vrei tu 

if(!printf("Mue "))

if(printf("Mue ")==oricenumar)

Share this post


Link to post
Share on other sites
6 hours ago, tjt said:

Ce treaba are Androidul cu challenge-ul asta ?

Cred ca e offtopic ce a scris el, dar merge compilat si pe Android, ca doar e linux (try "termux" and you get console).

 

6 hours ago, Okjokes said:

Da? Care-s alea?

Exista cel putin 2 metode (fork() - deja precizat de MrGrj sau vfork())

 

Am o curiozitate ce ma macina. Ar putea merge vreodata ceva ce seamana cu ceea ce e scris mai jos?

(Incerc sa rescriu "jmp" care apare in locul "else"-ului, in functia "main". Din cunostintele mele e o incercare nereusita, din cauza optimizarilor diferitelor compilatoare, dar.. ar putea merge ceva de genul macar pe un compilator, cu optiunile de compilare specificate? A treia conditie din "if" era o incercare de a pacali compilatorul ca pot folosi ambele ramuri, atat "if" cat si "else". Nu merge intotdeauna si uneori poate optimiza si sa dispara una din ramuri "if"/"else" la compilare. Pozitiile 39 si 40 nu sunt chiar random, sunt preluate cu un hexeditor (diferenta de la inceputul adresei functiei main pana la adresa "else" in asamblare, iar instructiunea "jmp" in cazul meu era pe 2 octeti asa ca am pus 2 NOP (0x90 pentru x86) ). Codul nu mi-a mers, am primit un "Segmentation fault (core dumped)". )

Spoiler

int main()
{
  if ( ((((char*)main)[39] = 0x90) == 0x90) &&
       ((((char*)main)[40] = 0x90) == 0x90) &&
       (((char*)main)[10] != 1) )
    printf("Muie ");
  else // aici face "jmp" la adresa la care se face "return 0;"; am incercat sa rescriu acest "jmp"
    printf("Dragnea");
  return 0;
}

 

Edited by a13x4nd7u
Adaugare explicatii

Share this post


Link to post
Share on other sites
8 minutes ago, a13x4nd7u said:

Cred ca e offtopic ce a scris el, dar merge compilat si pe Android, ca doar e linux (try "termux" and you get console).

 

Exista cel putin 2 metode (fork() - deja precizat de MrGrj sau vfork())

 

Am o curiozitate ce ma macina. Ar putea merge vreodata ceva ce seamana cu ceea ce e scris mai jos?

(Incerc sa rescriu "jmp" care apare in locul "else"-ului, in functia "main". Din cunostintele mele e o incercare nereusita, din cauza optimizarilor diferitelor compilatoare, dar.. ar putea merge ceva de genul macar pe un compilator, cu optiunile de compilare specificate? A treia conditie din "if" era o incercare de a pacali compilatorul ca pot folosi ambele ramuri, atat "if" cat si "else". Nu merge intotdeauna si uneori poate optimiza si sa dispara una din ramuri "if"/"else" la compilare. )

You must be logged in to see the content.
Hide contents

int main()
{
  if ( ((((char*)main)[39] = 0x90) == 0x90) &&
       ((((char*)main)[40] = 0x90) == 0x90) &&
       (((char*)main)[10] != 1) )
    printf("Muie ");
  else
    printf("Dragnea");
  return 0;
}

 

 

 

La asa ceva de genu' m-am gandit si eu, dar nu cred ca e posibil. 

Experienta mea e limitata in ASM, asa ca poate gresesc, dar cred ca in momentul in care ai facut jump nu poti sa mai faci return ci pleaca de la linia respectiva. 

Edited by tjt
  • Upvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×