几道经典c语言题目

2.编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
    char str[255] = {0};    printf("请输入一个字符串:\n");    scanf("%[^\n]", str); //    gets(str);    int maxLength = 0, maxIndex = 0;    int length = 0;    int i = 0;    while (str[i] != '\0') {        if (str[i] != ' ') {            length++;        } else {            if (maxLength < length) {                maxLength = length;                maxIndex = i - maxLength;            }            length = 0;        }        i++;    }    if (maxLength < length) {        maxLength = length;        maxIndex = i - maxLength;    }    for (int j = maxIndex; j < maxIndex + maxLength; j++) {        printf("%c", str[j]);    }    
3、耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,最后留在圈子内的人就是出卖耶稣的叛徒。请找出它原来的序号。
    int a[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};    int number = 13; //记录圈内剩余的人数    int count = 0; //通过计算器来模拟报数    int i = 0; //循环变量    while (number > 1) {        if (a[i] != 0) {            count++;        }        if (count == 3) {            a[i] = 0;  //让该元素置0,模拟踢出圈子            count = 0; //然后计数器重新归0            number--;  //踢出圈子之后人数-1        }        i++;        if (i == 13) {            i = 0; //当到最后一个人的时候, 再转到第一个人继续报数        }    }    for (int j = 0; j < 13; j++) {        if (a[j] > 0) {            printf("叛徒就是第%d个人.", a[j]);        }    }
4.将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。
    int a[10] = {0};    int b[10] = {0};    int c[20] = {0};    //给数组a赋值    for (int i = 0; i < 10; i++) {        a[i] = arc4random() % (40 - 20 + 1) + 20;    }    //给数组b赋值    for (int i = 0; i < 10; i++) {        b[i] = arc4random() % (40 - 20 + 1) + 20;    }    //将数组a排序    for (int i = 0; i < 10 - 1; i++) {        for (int j = 0; j < 10 - 1 - i; j++) {            if (a[j] > a[j + 1]) {                int temp = a[j];                a[j] = a[j + 1];                a[j + 1] = temp;            }        }    }    //将数组b排序    for (int i = 0; i < 10 - 1; i++) {        for (int j = 0; j < 10 - 1 - i; j++) {            if (b[j] > b[j + 1]) {                int temp = b[j];                b[j] = b[j + 1];                b[j + 1] = temp;            }        }    }    //输出数组a    for (int i = 0; i < 10; i++) {        printf("%d ", a[i]);    }    printf("\n");    //输出数组b    for (int j = 0; j < 10; j++) {        printf("%d ", b[j]);    }    printf("\n");    //将数组a和数组b合并到数组c    int i = 0, j = 0, k = 0;    while (i < 10 && j < 10 ) {        if (a[i] > b[j]) {            c[k++] = b[j++];        } else {            c[k++] = a[i++];        }    }    while (i < 10) {        c[k++] = a[i++];    }    while (j < 10) {        c[k++] = b[j++];    }    //输出排序好的数组c    for (int m = 0; m < 20; m++) {        printf("%d ", c[m]);    }
5.有1000000个数,每个数取值范围是0-999999
找出其中重复的数,重复次数。
    //第一种方式
    int a[COUNT] = {0};    int number = 0;    for (int i = 0; i < COUNT; i++) {        number = arc4random() % COUNT;        printf("%d ", number);        a[number]++;    }    printf("\n");    for (int i = 0; i < COUNT; i++) {        if (a[i] > 1) {            printf("%d : %d\n", i, a[i]);        }    }
    //第二种形式
    int a[COUNT] = {0};    for (int i = 0; i < COUNT ; i++) {        a[i] = arc4random() % COUNT;        printf("%d ", a[i]);    }    printf("\n");    int temp = 0; //临时变量,用来存储取出来的数    int count = 1; //记录重复的个数    for (int i = 0; i < COUNT; i++) {        temp = a[i];        for (int j = i + 1; j < COUNT; j++) {            if (a[j] == -1) {                continue;            }            if (a[j] == temp) {                count++;                a[j] = -1; //将已经计算过的数置为-1.            }        }        if (count > 1) {            printf("%d : %d\n", temp, count);            count = 1;        }    }
2.找出一个二维数组中的鞍点,即该位置上的元素在该行中最大,在该列中最小(也可能没鞍点),打印出有关信息。    int a[3][4] = {0};    for (int i = 0; i < 3; i++) {        for (int j = 0; j < 4; j++) {            a[i][j] = 1;//arc4random() % (40 - 20 + 1) + 20;            printf("%d ", a[i][j]);        }        printf("\n");    }    //遍历数组中的每个元素,验证每个元素是否是鞍点.    int count = 0; //用来记录鞍点的个数    for (int i = 0; i < 3; i++) {        for (int j = 0; j < 4; j++) {            //首先假定该数是该行中最大的            BOOL isMax = YES;            //验证该数是否是该行中最大的            for (int column = 0; column < 4; column++) {                if (a[i][column] > a[i][j]) {                    isMax = NO; //如果不是最大的, isMax标识为NO.                    break; //结束当前循环                }            }                        if (isMax == NO) {                //如果为NO,说明不是该行中最大的,已经不满足鞍点的条件了.                continue; //直接进入下一循环            }            //先假设是该列中最小的.            BOOL isMin = YES;            //验证该数是否是该列中最小的.            for (int row = 0; row < 3; row++) {                if (a[row][j] < a[i][j]) {                    isMin = NO;                    break;                }            }            if (isMin == NO) {                continue;            }            count++;            printf("row:%d,column:%d = %d\n", i + 1, j + 1, a[i][j]);        }    }    if (count == 0) {        printf("没有鞍点");    }   



你可能感兴趣的:(字符串,C语言,经典,单词,字母)