码迷,mamicode.com
首页 > 其他好文 > 详细

hrbust-1545-基础数据结构——顺序表(2)

时间:2014-10-09 22:47:58      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545

基础数据结构——顺序表(2)
Time Limit: 1000 MSMemory Limit: 10240 K
Total Submit: 412(165 users)Total Accepted: 188(150 users)Rating: bubuko.com,布布扣bubuko.com,布布扣Special Judge: No
Description

在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。

Input

第一行输入表的长度n;

第二行依次输入顺序表初始存放的n个元素值。

Output

第一行输出完成多余元素删除以后顺序表的元素个数;

第二行依次输出完成删除后的顺序表元素。

Sample Input

12

5 2 5 3 3 4 2 5 7 5 4 3

Sample Output

5

5 2 3 4 7

解题思路:用一个flag数组标记当前数字在之后是否出现过 

bubuko.com,布布扣
#include <stdio.h>

int a[1010];
int flag[1010];
int ans[1010];

int main(){
    int n, i, j;
    while(scanf("%d", &n) != EOF){
        for(i = 0; i < n; i++){
            scanf("%d", &a[i]);
            flag[i] = 0;
        }
        for(i = 0; i < n - 1; i++){
            for(j = i + 1; j < n; j++){
                if(a[i] == a[j]){
                    flag[j] = 1;
                }
            }
        }
        for(i = j = 0; i < n; i++){
            if(flag[i] == 0){
                ans[j] = a[i];
                j++;
            }
        }
        printf("%d\n", j);
        for(i = 0; i < j - 1; i++){
            printf("%d ", ans[i]);
        }
        printf("%d\n", ans[i]);
    }
    return 0;
}
View Code

hrbust-1545-基础数据结构——顺序表(2)

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/angle-qqs/p/4014480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!