前
中
1.一边输入一边求每一行的和,然后用flag记下和最大的行数,再用swap互换即可
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin)
#define INF 2147483647
#define eps 1e-7
#define L 100005
#define Fo(i,a,b) for(LL i=(a),_=(b); i<=_; i++)
#define Ro(i,b,a) for(LL i=(b),_=(a); i>=_; i--)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define _ceil(_,__) (_+(__-1))/__
inline LL read() {
LL x = 0, f = 1;char c = getchar();
while (!isdigit(c)) {
if (c == '-')f = -f;c = getchar(); }
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
return x * f;
}
LL n , m , mat[105][105] , maxx=-1 , sum , flag = 1;
int main() {
// fin;
n=read(); m=read();
Fo(i,1,n) {
sum = 0;
Fo(j,1,m) {
mat[i][j]=read();
sum += mat[i][j];
}
if(sum>maxx) {
maxx = sum;
flag = i;
}
}
Fo(i,1,m)
swap(mat[1][i] , mat[flag][i]);
Fo(i,1,n) {
Fo(j,1,m)
printf("%lld ",mat[i][j]);
printf("\n");
}
return 0;
}
2.知识点:指针访问数组(看代码。其他是基础的)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin)
#define INF 2147483647
#define eps 1e-7
#define L 100005
#define Fo(i,a,b) for(LL i=(a),_=(b); i<=_; i++)
#define Ro(i,b,a) for(LL i=(b),_=(a); i>=_; i--)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define _ceil(_,__) (_+(__-1))/__
inline LL read() {
LL x = 0, f = 1;char c = getchar();
while (!isdigit(c)) {
if (c == '-')f = -f;c = getchar(); }
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
return x * f;
}
LL n , mat[105][105] , sum1 , sum2 , sum3;
int main() {
// fin;
n=read();
Fo(i,1,n)
Fo(j,1,n)
mat[i][j]=read();
Fo(i,1,n)
Fo(j,i,n)
sum1+=*(mat[i]+j);
Fo(i,1,n)
Fo(j,1,i)
sum2+=*(mat[i]+j);
Fo(i,1,n)
sum3+=*(mat[i]+i);
printf("%lld %lld %lld",sum1,sum2,sum3);
return 0;
}
3.先把字符输入(很重要,直接cin不行!)然后对输入的字符串进行处理,把是字母的单独放到一个字符串中(是否为字母用ASCII码判断),至于不区分大小写,直接根据字母大小写ASCII码差距32判断就行了。
#include<iostream>
#include<cstring>
using namespace std;
const int SZ = 100;
int main() {
string carray , x;
int is_palindrome = 0 , top = 0 , flag = 0;
cout << "Please input a string .." << endl;
getline(cin, x);
int len = x.size();
while(top<len) {
if((x[top]>='A'&&x[top]<='Z')||(x[top]>='a'&&x[top]<='z')) {
carray[flag] = x[top];
flag++;
}
top++;
}
len = flag;
for (int i = 0; i < len/2; i++)
if (carray[i] == carray[len-1-i] || carray[i]+32 == carray[len-1-i] ||carray[i] == carray[len-1-i]+32)
is_palindrome = 1;
if (is_palindrome)
cout << "The string is a palindrome" << endl;
else
cout << "The string is not a palindrome" << endl;
return 0;
}
4.约瑟夫杀人问题,直接用数组模拟就行了
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin)
#define INF 2147483647
#define eps 1e-7
#define L 100005
#define Fo(i,a,b) for(LL i=(a),_=(b); i<=_; i++)
#define Ro(i,b,a) for(LL i=(b),_=(a); i>=_; i--)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define _ceil(_,__) (_+(__-1))/__
#define _srand srand(time(0))
inline LL read() {
LL x = 0, f = 1;char c = getchar();
while (!isdigit(c)) {
if (c == '-')f = -f;c = getchar(); }
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
return x * f;
}
LL n , m , from , num , vis[10005] , flag , top;
int main() {
// fin;
_srand;
n=read();
m=rand()%n+1;
num=n;
while(1) {
if(num==1) break;
while(flag<m) {
do{
top++;
if(top>n) top=1;
}while(vis[top]);
flag++;
}
vis[top] = 1;
num--;
flag=0;
}
Fo(i,1,n)
if(!vis[i]) {
printf("%lld",i);
break;
}
return 0;
}