A two-dimensional array, a nested loop, the console input and output
#include <iostream> a using namespace std; / * Function: gold, silver and bronze medals total number of days statistics obtained row A country with a space separate ideas: 1 digital capture console 2 receives a two-dimensional array of digital storage 3 silver and bronze medals calculated total number * / int main () { int Row; // Row represents the number of days CIN >> Row; // COUT << Row; // int COL = 3; // int Medals [3] [. 3] = {{1,0,3}, {3,1,0}, {0,3,0}}; // Golds. 4 =, =. 4 Silvers, Bronzes. 3 = // int Medals [. 3] [. 3] = {3,2,3,3,2,3,3,2,3}; // Golds =. 9, Silvers =. 6, Bronzes. 9 = int Medals [Row] [ . 3 ]; char ch; if ((ch=getchar())=='\n') { char c; while((c=getchar())!='\n') { if(c>='0'&&c<='9') { ungetc(c,stdin); for(int k = 0;k < row;k++) { for(int l = 0; l < 3;l++) { cin>>medals[k][l]; } } } } } int i,j; int total; for(i = 0;i < 3;i++) { // 控制列 /* for(j = 0;j < 3;j++) { cout << medals[i][j] << " "; if(j / 2 == 1) { cout << endl; } } */ //cout << i << endl; int sum = 0; for(j = 0;j < row;j++) { // 控制行 sum += medals[j][i]; total += medals[j][i]; } cout << sum << " "; } /* int m,n; for (m = 0;m < 3;m++) { for(n = 0;n < 3;n++) { total += medals[m][n]; } } */ cout << total << endl; //cout << sum << endl; //total = golds + silvers + bronzes; //cout << golds << " " << silvers << " " << bronzes << " " << total << endl; return 0; }
Test Results: