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 );}