美文网首页
北京自考-计算机-独本-20170313数据结构B卷

北京自考-计算机-独本-20170313数据结构B卷

作者: 海德堡绝尘 | 来源:发表于2017-03-13 19:23 被阅读29次
试卷截图

第2题解答:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1000

/*
*   【 2017-03-13 数据结构B卷-- 偶数号 】
*   编一C程序,它能读入一串(n个)整数(n<1000, 以-9999为结束标记), 
*   并判断第n个整数(即-9999的前一个)在前(n-1)个整数中出现的次数,在输出该次数。
*   (输入时,两个相邻的整数用空格隔开)。
*/

int main(void){
    int arr[SIZE];
    int i, j, temp, length, last, count;

    i = 0; // 输入时的计数下标
    j = 0; // 输出存储结果时的计数下标
    temp = 0; // 扫描输入时的临时变量
    length = 0; // 数组的数据长度

    printf("=================================================\n");
    printf("请输入一串(n个)整数(n<1000, 以-9999为结束标记):\n");
    while(temp != -9999){
        scanf("%d", &temp);
        if(temp == -9999){
            break;
        }

        arr[i] = temp;
        i++;
    }

    length = i;
    printf("=================================================\n");
    printf("您输入的一串整数为:\n");
    for(j=0; j<length; j++){
        printf("%d ", arr[j]);
    }
    printf("\n");


    last = arr[length-1];
    count = 0;
    for(j=0; j<length-1; j++){
        if(last == arr[j]){
            count++;
        }
    }
    printf("=================================================\n");
    printf("您输入的一串整数中第n位[ %d ]在前面的(n-1)位中出现一共[ %d ]次!\n", last, count);
    
    printf("\n");
    printf("===============[ 程序结束 ]======================\n");
    
    return 0;
}

运行结果:

运行结果截图

相关文章

网友评论

      本文标题:北京自考-计算机-独本-20170313数据结构B卷

      本文链接:https://www.haomeiwen.com/subject/tdeqnttx.html