第一题
#include<iostream>
using namespace std;
int main() {
int a, b, c, d, e, f, g, h, i;
cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
int A[3][3] = { a, b, c, d, e, f, g, h, i };
int B[3] = { 0,0,0 }, temp[3];
for (int m = 0; m <= 2; m++) {
for (int n = 0; n <= 2; n++) {
B[m] += A[m][n];
}
}
int max = 0;
for (int q = 0; q <= 2; q++) {
if (max < B[q])
max = B[q];
}
for (int t = 0; t <= 2; t++) {
if (B[t] ==max) {
for (int m = 0; m <= 2; m++) {
temp[m] = A[0][m];
A[0][m] = A[t][m];
A[t][m] = temp[m];
}
}
};
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
return 0;
}
第二题
#include<iostream>
using namespace std;
int main() {
int a, b, c, d, e, f, g, h, i;
cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
int A[3][3] = {a,b,c,d,e,f,g,h,i}, sum1 = 0, sum2 = 0, sum3 = 0;
for (int j = 0; j <= 2; j++) {
int* p = &A[j][j];
sum3 += *p;
for (int m = j; m <= 2 ; m++) {
int* p1 = &A[j][m];
sum1 += *p1;
}
for (int n = 0; n <= j; n++) {
int* p2 = &A[j][n];
sum2 += *p2;
}
}
cout << sum1 <<" "<< sum2 <<" "<< sum3 << endl;
return 0;
}
第三题
#include <iostream>
#include <string>
using namespace std;
const int SZ = 100;
int main()
{
int i;
string carray;
string carray1;
int is_palindrome = 1;
cout << "Please input a string .." << endl;
getline(cin, carray);
int len1 = carray.size();
for (i = 0; i < len1; ++i)
{
if ((carray[i] <= 122) && (carray[i] >= 97))
{
carray[i] -= 32;
carray1 += carray[i];
}
else if ((carray[i] <= 90) && (carray[i] >= 65))
carray1 += carray[i];
}
int len = carray1.size();
for (int i = 0; i < len / 2; i++)
{
if (carray1[i] != carray1[len - 1 - i])
{
is_palindrome = 0;
break;
}
}
if (is_palindrome)
cout << "The string is a palindrome" << endl;
else
cout << "The string is not a palindrome" << endl;
return 0;
}
第四题
#include<iostream>
#include<vector>
#include<time.h>
using namespace std;
int main()
{
srand(time(0));
int n, i = 0, a, b, c;
cout << "请输入人数";
cin >> n;
vector<bool>vi(n, 1);
int m = rand() % (n + 1);
cout <<"随机数为"<< m << endl;
for (a = 0; a < n - 1; ++a)
{
for (b = 1; b < m; ++i)
{
if (i >= n)
i = i % n;
if (vi[i] == 1)
++b;
}
if (i >= n)
i = i % n;
while (vi[i] == 0)
{
++i;
if (i >= n)
i = i % n;
}
if (vi[i] == 1)
vi[i] = 0;
}
for (c = 0; c < n; ++c)
{
if (vi[c] == 1)
cout <<"剩下的序号为"<< c + 1 << endl;
}
return 0;
}