zoukankan      html  css  js  c++  java
  • 南邮NOJ 1014 数据的插入与删除

    数据的插入与删除

    时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
    总提交 : 1796            测试通过 : 355 

    题目描述

    在一组数据(数目不超过10000)中,插入新数,删除所有与给定数相等的数据。



    输入

    第一行是未排序的一组非负整数,数目不超过10000。以-1作为结束标志。

    第二行是要插入的数。

    第三行是要删除的数。

    输出

    第一行输出自小到大排好序的数。如果没有元素,输出“No elements.”(不包括引号)。

    第二行输出插入后自小到大排好序的数,以“,”隔开。

    第三行输出删除后自小到大排好序的数,以“,”隔开。如果没有元素,输出“No elements.”(不包括引号)。

    样例输入

    100 98 79 63 44 99 -1
    88
    79

    样例输出

    44,63,79,98,99,100
    44,63,79,88,98,99,100
    44,63,88,98,99,100

    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std;
    int comp(const void *a,const void *b)
    {
        return *(int*)a-*(int*)b;
    }
    int main()
    {
        int n,i=0;
        int a[10000+1];
        while(scanf("%d",&n)==1&&n!=-1)
        {
           a[i++]=n;
        }
        int in,de,c=0,p=0;
        scanf("%d",&in);
        scanf("%d",&de);
        if(i==0)
        {
            printf("No elements.
    ");
            a[i++]=in;
            printf("%d
    ",in);
            if(in!=de)
                printf("%d
    ",in);
            else
                printf("No elements.
    ");
        }
        else {
        qsort(a,i,sizeof(int),comp);
        for(int j=0;j<i;j++)
        {
            if(j==0)
            printf("%d",a[j]);
            else
            printf(",%d",a[j]);
        }
        printf("
    ");
        a[i++]=in;
        qsort(a,i,sizeof(int),comp);
        for(int j=0;j<i;j++)
        {
            if(j==0)
            printf("%d",a[j]);
            else
            printf(",%d",a[j]);
        }
        printf("
    ");
        for(int j=0;j<i;j++)
        {
    
            if(a[j]!=de){
                    p++;
            if(c==0){
            printf("%d",a[j]);
            c++;
            continue;
            }
            if(c==1)
            {
                printf(",%d",a[j]);
            }
        }
        }
        if(p==0)
        {
            printf("No elements.");
        }
        printf("
    ");
        }
    }
    

    主要是无元素情况下的考虑。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    27. Remove Element
    列表变成字典
    1. Two Sum
    CVPR2019:What and How Well You Performed? A Multitask Learning Approach to Action Quality Assessment
    959. Regions Cut By Slashes
    118. Pascal's Triangle
    loj3117 IOI2017 接线 wiring 题解
    题解 NOI2019 序列
    题解 省选联考2020 组合数问题
    题解 Educational Codeforces Round 90 (Rated for Div. 2) (CF1373)
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965722.html
Copyright © 2011-2022 走看看