问答详情
源自:5-6 练习题

如何改下面的代码rand 输出 Y 或 N

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#define random(x) (rand()%x)

main()

{

srand((int)time(0));

for(int x=0;x<10;x++)

printf("%d\n",random(100));


}



提问者:萌新程序猿orz 2018-11-12 11:14

个回答

  • qq_慕尼黑1241971
    2019-10-20 02:44:56

    #include<stdio.h>

    #include<stdlib.h>

    #include<time.h>

    char random(int x)

    {

        if (rand() % x)

            return 'y';

        else

            return 'n';

    }

    int main()

    {

        srand((int)time(0));

        for (int i = 0; i < 10; i++)

            printf("%c\n", random(2));

        return 0;

    }

    这样也可以

  • qq_慕尼黑1241971
    2019-10-20 02:39:57

    #include<stdio.h>

    #include<stdlib.h>

    #include<time.h>

    #define random(x) (rand()%x)

    int main()

    {

        srand((int)time(0));

        for (int i = 0; i < 10; i++)

            if(random(2))

                printf("%c\n", 'y');

            else

                printf("%c\n", 'n');

        return 0;

    }


  • qq_慕尼黑1241971
    2019-10-20 02:36:15

    #include<stdio.h>

    #include<stdlib.h>

    #include<time.h>

    #define random(x) (rand()%2)

    int main()

    {

        srand((int)time(0));

        for (int i = 0; i < 10; i++)

            if(random(100))

                printf("%c\n", 'y');

            else

                printf("%c\n", 'n');

        return 0;

    }


  • 破釜chenzhou
    2018-11-12 20:35:16

    天哪,完全看不明白你的问题,程序也看不明白。。。。。。