Algorithm training to delete an array of 0 elements

Source: Internet
Author: User

algorithm training to delete an array of 0 elementsTime limit: 1.0s memory limit: 512.0MB    Reads n integers from the keyboard into the array, writes the function compactintegers, deletes all elements with a value of 0 in the array, and then moves the elements to the first end of the array. Note that the Compactintegers function needs to accept the array and its number of elements as arguments, and the function return value should be the number of new elements of the array after the delete operation executes.  Outputs the number of elements in the array after deletion and sequentially outputs the array elements. Sample input: (Input format Description: 5 is the number of input data, 3 4 0 0 2 is a space-separated 5 integers)
5
3 4 0) 0 2
Sample output: (Output Format Description: 3 is the number of non-0 data, 3 4 2 is a space-separated 3 non-zero integers)
3
3 4 2 Sample Input 7
0 0 7 0 0 9 0 Sample Output 2
7 9 Example Input 3
0 0 0 Sample Output 0
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;inta[10000000];intMain () {intN,i,ans;  while(~SCANF ("%d",&N)) {intt,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\n", ans); if(ans!=0) {printf ("%d", a[0]);  for(i=1; i<ans;i++) printf ("%d", A[i]); printf ("\ n"); }    }    return 0;}

Algorithm training to delete an array of 0 elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.