//选票系统 #include<iostream> using namespace std; int main() { int i, v0 = 0, v1 = 0, v2 = 0, v3 = 0, n, a[50]; cout << "Please enter the number of elestorrate:"; cin >> n; cout << "Please enter 1 or 2 or 3 "; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) { if (a[i] ==1) v1++; if (a[i] == 2) v2++; if (a[i] == 3) v3++; else v0++; } cout << "candiate 1:" << v1 << endl; cout << "candiate 2:" << v2 << endl; cout << "candiate 3:" << v3 << endl; cout << "not use:" << v0<< endl; return 0; }
矩阵转置
//矩阵转置 #include<iostream> using namespace std; int main() { int i, j, i1, j1, a[101][101], b[101][101]; cout << "please enter the number of rows(<=100)"; cin >> i1; cout << "please enter the number of columns(<=100)"; cin >> j1; for (i = 1; i <= i1; i++) for (j = 1; j <= j1; j++) cin >> a[i][j]; cout << "a[i][j]= " << endl; for (i = 1; i <= i1; i++) { for (j = 1; j <= j1; j++) { cout << a[i][j] << " "; } cout << endl; } cout << "b[i][j]= " << endl; for (j = 1; j<= j1; j++) { for (i = 1;i <= i1; i++) { cout << a[i][j] << " "; } cout << endl; } return 0; }
统计字符类别
//main program #include<iostream> using namespace std; int main() { char c[5]; int i; int letters = 0, space = 0, digit = 0, others = 0; cout << "Please input some characters:" << endl; for (i = 0; i < 5; i++) cin >> c[i]; for (i = 0; i < 5;i++) { if (c[i] >= 'a'&& c[i] <= 'z' || c[i] >= 'A'&& c[i] <= 'Z') letters++; else if (c[i] == ' ') space++; else if (c[i] >= '0'&& c[i] <= '9') digit++; else others++; } cout << "char= " << letters << endl; cout << "space= " << space << endl; cout << "digit= " << digit << endl; cout << "others= " << others << endl; }
递归分鱼
//递归解决分鱼问题 #include<iostream> using namespace std; int yu(int i, int num); int main() { int n = 1,sum; int num; cout << "please enter the number of fish:"<<endl; cin >> num; do { sum = yu(5,num); num++; } while (sum%1 != 0); cout << "the total fish is " << sum << endl; return 0; } int yu(int i,int num) { if (i == 1) return (5*num + 1); else return (5*yu(i - 1,num) + 1); }