zoukankan      html  css  js  c++  java
  • 算法训练 删除数组零元素

     算法训练 删除数组零元素  
    时间限制:1.0s   内存限制:512.0MB
        
      从键盘读入n个整数放入数组中,编写函数CompactIntegers,删除数组中所有值为0的元素,其后元素向数组首端移动。注意,CompactIntegers函数需要接受数组及其元素个数作为参数,函数返回值应为删除操作执行后数组的新元素个数。输出删除后数组中元素的个数并依次输出数组元素。  样例输入: (输入格式说明:5为输入数据的个数,3 4 0 0 2 是以空格隔开的5个整数)
      5
      3 4 0 0 2
      样例输出:(输出格式说明:3为非零数据的个数,3 4 2 是以空格隔开的3个非零整数)
      3
      3 4 2
    样例输入
    7
    0 0 7 0 0 9 0
    样例输出
    2
    7 9
    样例输入
    3
    0 0 0
    样例输出
    0
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int a[10000000];
    int main()
    {
        int n,i,ans;
        while(~scanf("%d",&n))
        {
             int t,ans=0;
             for(i=0;i<n;i++)
            {
                scanf("%d",&t);
                if(t!=0)
                 {
                     a[ans++]=t;
                      break;
                 }
            }
            for(i=i+1;i<n;i++)
            {
                scanf("%d",&t);
                if(t!=0)
                {
                    a[ans++]=t;
                }
            }
            printf("%d
    ",ans);
            if(ans!=0)
            {
            printf("%d",a[0]);
            for(i=1;i<ans;i++)
               printf(" %d",a[i]);
            printf("
    ");
            }
    
        }
        return 0;
    }
  • 相关阅读:
    mysql关联查询
    MySQL数据库面试题(转发来自https://thinkwon.blog.csdn.net/article/details/104778621)
    iview + vue + thinphp5.1 源码
    <!--标签嵌套规则-->
    PHP的基本变量检测方法
    PHP的八种变量
    php变量命名规范
    C++11新特性-常用
    算法设计-DP常见问题及解题技巧
    Web开发-概述
  • 原文地址:https://www.cnblogs.com/cancangood/p/4354427.html
Copyright © 2011-2022 走看看