c语言题

来源:互联网 发布:手机自制铃声软件 编辑:程序博客网 时间:2024/04/28 16:06

  c 语言各种补充题

 1、随机生成100万个数据,数据范围[1 ,1000000],如果有重复数据,要求有提示;

//    int array[1000000] = {0};

//    for (int i = 0; i < 1000000; i++) {

//        unsigned int random = arc4random() % 1000000;

//        if (array[random] != 0) {

//            printf("%d重复\n", random);

//        }else {

//            array[random] = random;

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

//        }

 

//    }

2、随机生成100个数据,数据范围[1, 100],要求不能有重复数据;

        int array[100] = {0};

        int count = sizeof(array) / sizeof(array[0]);

        for (int i = 0; i  < 100; i++) {

             array[i] = i + 1;

        }

        for (int i = count - 1; i > 0; i--) {

            unsigned int random = arc4random() % (i + 1);

            int temp = array[random];

            array[random] = array[i];

            array[i] = temp;

        }

        for (int i = 0; i < count - 1; i++) {

            printf("[%2d] : %d\n", i, array[i]);

        }

3、随机生成一个数,这个数的范围在[10,20]或[80,90],要求用四种不同方法;

1  //    int a = arc4random() % 20;

//    if (a >= 0 && a <= 10) {

//        printf("%d\n", a + 10);

//    } else {

//        printf("%d\n", a + 70);

//    }

    

2  //    int a = arc4random() % 2;

//    int b = 0;

//    switch (a) {

//        case 0:

//            b = arc4random() % (20 - 10 + 1) + 10;

//            printf("%d\n", b);

//            break;

//        case 1:

//            b = arc4random() % (90 - 80 + 1) + 80;

//            printf("%d\n", b);

//            break;

//    }

    

3  //    int a = arc4random() % (31 - 10 + 1) + 10;

//    if (a >= 10 && a <= 20) {

//        printf("%d\n", a);

//    } else {

//        printf("%d\n", a + 59);

//    }

    

4  //    int a = arc4random() % (20 - 10 + 1) + 10;

//    switch (arc4random() % 2) {

//        case 0:

//            printf("%d\n", a);

//            break;

//        case 1:

//            printf("%d\n", a + 70);

//            break;

 

//    }

4、系统随机输入一个数,要求这个数在[7,15]或[19,29]或[34,52]范围内;

//    unsigned int random = arc4random() % 39;

//    if (random < 9) {

//        printf("%d\n", random + 7);

//    } else if (random >= 9 && random < 20 ){

//        printf("%d\n", random + 10);

//    } else {

//        printf("%d\n", random + 14);

 

//    }

5、将字符串“a12b345c6d789e”,内的数字去掉,输出结果为“abcde”;

   char a[] = "a12b345c6d78e";

    int i = 0, j = 0;

    while (a[i] != '\0') {

        if (!(a[i] >= '\0' && a[i] <= '9') ){

            a[j] = a[i];

            j++;

        }

        i++;

    }

    a[j] = '\0’;

    puts(a);

6、给出一个整数12345,  

 1)、先顺序单独的输出每一个数的值, 

void printIntAsc(int n)

{

    int count = countOfInt(n);

    int x = 1;

    for (int i = 0 ; i < count - 1; i++) {

        x *= 10;

    }

    while (x > 0 ) {

        printf("%d", n / x);

        n %= x;

        x /= 10;

    }

 

}

 2)、再逆序的输出每一个数的值  

void printIntDesc(int n)

{

    while (n > 0) {

        printf("%d", n % 10);

        n /= 10;

    }

 

}

 3)、写一个函数,,传进去 12345   返回出来54321

int reverseInt(int n)

{

    int newnumber = 0 ;

        while (n > 0 ) {

            newnumber = newnumber * 10 + n % 10;

            n /= 10;

        }

    return newnumber;

 

}

0 0
原创粉丝点击