zoukankan      html  css  js  c++  java
  • hrbust-1545-基础数据结构——顺序表(2)

    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: 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数组标记当前数字在之后是否出现过

     1 #include <stdio.h>
     2 
     3 int a[1010];
     4 int flag[1010];
     5 int ans[1010];
     6 
     7 int main(){
     8     int n, i, j;
     9     while(scanf("%d", &n) != EOF){
    10         for(i = 0; i < n; i++){
    11             scanf("%d", &a[i]);
    12             flag[i] = 0;
    13         }
    14         for(i = 0; i < n - 1; i++){
    15             for(j = i + 1; j < n; j++){
    16                 if(a[i] == a[j]){
    17                     flag[j] = 1;
    18                 }
    19             }
    20         }
    21         for(i = j = 0; i < n; i++){
    22             if(flag[i] == 0){
    23                 ans[j] = a[i];
    24                 j++;
    25             }
    26         }
    27         printf("%d ", j);
    28         for(i = 0; i < j - 1; i++){
    29             printf("%d ", ans[i]);
    30         }
    31         printf("%d ", ans[i]);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    win7下命令行添加系统服务
    java执行cmd命令
    grails-BuildConfig.groovy中grails.war.resources
    密码学
    Grails框架优劣势
    groovy+idea+Maven项目加载自身jar包
    cmd查看我的电脑盘符和文件
    MySQL insert插入
    MySQL截取字符串函数方法
    mysql 替换语句
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4014480.html
Copyright © 2011-2022 走看看