C Programming: Number of integers ., Number of c Programming Integers

Source: Internet
Author: User

C Programming: Number of integers ., Number of c Programming Integers

// Given k (1 <k <100) positive integers, each of which is equal to or greater than 1 and less than or equal to 10. // Write the program to calculate the number of occurrences of 1, 5, and 10 in the given k positive integers. // Input // The input has two rows: the first row contains a positive integer k, and the second row contains k positive integers, each of which is separated by a space. // Output // The output contains three rows: the number of occurrences of the First Action 1, the number of occurrences of the second action 5, and the number of occurrences of the third Action 10. # Include <stdio. h> int main () {int k; // The number of positive integers int I, j; // cyclic variable, which must be declared here for use below, it is not declared when it cannot be used. vc ++ 6.0 does not support it !! Int a [100] = {0}; // first store the input positive integer in the array, and the initialization is all 0. Int count1 = 0, count5 = 0, count10 = 0; // scanf ("% d", & k); for (I = 0; I <k; ++ I) {scanf ("% d", & a [I]); // note that spaces cannot be added to "% d, otherwise, the program has a problem (one more number is required)} for (j = 0; j <k; j ++) // for cyclically traverses each number in the array and counts the number of occurrences of each number {if (a [j] = 1) count1 ++; if (a [j] = 5) count5 ++; if (a [j] = 10) count10 ++;} printf ("% d \ n ", count1); printf ("% d \ n", count5); printf ("% d \ n", count10); return 0 ;}

-- ===================================================== ==================================

No array:

# Include <stdio. h> int main () {int I, k, x, // cyclic variable I, number of integers k, integer x.int a1, a5, a10; // statistics 1, 5, 10. A1 = a5 = a10 = 0; scanf ("% d", & k); for (I = 1; I <= k; ++ I) {scanf ("% d", & x); if (x = 1) ++ a1; if (x = 5) ++ a5; if (x = 10) + + a10;} printf ("% d \ n", a1, a5, a10 );}


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.