求矩阵的转置。
输入数据:
2 1 3
3 3 1
1 2 1
输出数据:
2 3 1
1 3 2
3 1 1
#include <iostream> #include <iomanip> using namespace std; const int n=3; int a[n+1][n+1]; int main(){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin >> a[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout << setw(5) << a[j][i]; } cout << endl; } return 0; }
已知一个6*6的矩阵(方阵),把矩阵两条对角线上的元素加上10,然后输出这个新矩阵。
#include <iostream> #include <iomanip> using namespace std; int a[7][7]; int main(){ for(int i=1;i<=6;i++) for(int j=1;j<=6;j++) cin >> a[i][j]; for(int i=1;i<=6;i++) for(int j=1;j<=6;j++) if((i==j)||(i+j==7)) a[i][j]+=10; for(int i=1;i<=6;i++){ for(int j=1;j<=6;j++){ cout << setw(5) << a[i][j]; } cout << endl; } return 0; }
大部分元素是0的矩阵称为稀疏矩阵,假设有k个非0元素,则可把稀疏矩阵用k*3的矩阵简记之,其中第一列是行号,第二列是列号,第三列是该行、该列下的该元素的值。如:
0 0 0 5 简记成: 1 4 5 //第1行第4列有个数是5
0 2 0 0 2 2 2 //第2行第2列有个数是2
0 1 0 0 3 2 1 //第3行第2列有个数是1
试编程读入一个稀疏矩阵,转换成简记形式,并输出。
#include <iostream> #include <iomanip> using namespace std; const int n=3,m=5; int a[n+1][m+1],b[101][4],k=0; int main(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) cin >> a[i][j]; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]!=0){ k++; b[k][1]=i; b[k][2]=j; b[k][3]=a[i][j]; } for(int i=1;i<=k;i++){ for(int j=1;j<=3;j++) cout << setw(3) << b[i][j]; cout << endl; } return 0; }
打印杨辉三角的前10行。
1 1
1 1 1 1
1 2 1 1 2 1
1 3 3 1 1 3 3 1
1 4 6 4 1 1 4 6 4 1
#include <iostream> #include <iomanip> using namespace std; int a[100][100]; int main(){ a[1][1]=1; for(int i=2;i<=10;i++){ a[i][1]=1; a[i][i]=1; for(int j=2;j<i;j++) a[i][j]=a[i-1][j-1] + a[i-1][j]; } for(int i=1;i<=10;i++){ if(i!=10) cout << setw(30-3*i) << " "; for(int j=1;j<=i;j++) cout << setw(6) << a[i][j]; cout << endl; } return 0; }
输入一串字符,字符个数不超过100,且以“.”结束。判断它们是否构成回文。例如:12321,ABCBA,AA等。
#include <iostream> using namespace std; char ch,letter[101]; int i=0,j=1; int main(){ cout << "Input a string:"; cin >> ch; while(ch!='.'){ letter[++i]=ch; cin >> ch; } while(j<i && letter[j]==letter[i]){ i--; j++; } if(j>=i) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
蛇形填数:在n*n方阵里填入1,2,3,...,n*n,要求填成蛇形。例如n=4时方阵为:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
#include <iostream> #include <cstring> using namespace std; #define MAXN 100 int a[MAXN][MAXN]; int n,x,y,tot; int main(){ scanf("%d",&n); memset(a,0,sizeof(a)); tot=a[x=0][y=n-1]=1; while(tot<n*n){ while(x+1<n && !a[x+1][y]) a[++x][y]=++tot; while(y-1>=0 && !a[x][y-1]) a[x][--y]=++tot; while(x-1>=0&& !a[x-1][y]) a[--x][y]=++tot; while(y+1<n && !a[x][y+1]) a[x][++y]=++tot; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) printf("%6d",a[i][j]); printf("\n"); } return 0; }
#include <iostream> using namespace std; int main(){ int mat[5][5],n,m; for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ cin >> mat[i][j]; } } cin >> n >> m; n--,m--; for(int i=0;i<5;i++){ int tmp = mat[n][i]; mat[n][i] = mat[m][i]; mat[m][i] = tmp; } for(int i=0;i<5;i++){ cout << mat[i][0] ; for(int j=1;j<5;j++){ cout << " " << mat[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main(){ int n,x,y; cin >> n >> x >> y; cout << "(" << x << "," << 1 << ")"; for(int i=2;i<=n;i++) cout << " (" << x << "," << i << ")"; cout << endl; cout << "(" << 1 << "," << y << ")"; for(int i=2;i<=n;i++) cout << " (" << i << "," << y << ")"; cout << endl; int i=x,j=y; while(i>1 && j>1){ i--; j--; } cout << "(" << i++ << "," << j++ << ")"; while(i<=n && j<=n) cout << " (" << i++ << "," << j++ << ")"; cout << endl; i=x,j=y; while(i<n && j>1){ i++; j--; } cout << "(" << i-- << "," << j++ << ")"; while(i>=1 && j<=n) cout << " (" << i-- << "," << j++ << ")"; cout << endl; return 0; }
#include <iostream> using namespace std; int a[105][105],n,m; long long sum=0; int main(){ cin >> n >> m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j]; for(int i=0;i<m;i++) sum = sum + a[0][i]; if(n>1) for(int i=0;i<m;i++) sum = sum + a[n-1][i]; if(n>2){ for(int i=1;i<n-1;i++) sum = sum + a[i][0]; if(m>1) for(int i=1;i<n-1;i++) sum = sum + a[i][m-1]; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int a[100][100],n; int row[100],col[100]; int rn=0,cn=0,r,c; int main(){ cin >> n; for(int i=0;i<n;i++) for(int j=0;j<n;j++){ cin >> a[i][j]; if(a[i][j]==1){ row[i]++; col[j]++; } } for(int i=0;i<n;i++){ if(row[i]%2!=0){ rn++; r=i+1; } if(col[i]%2!=0){ cn++; c=i+1; } } if(rn==0 && cn==0) cout << "OK" << endl; else if(rn==1 && cn==1) cout << r << " " << c << endl; else cout << "Corrupt" << endl; return 0; }
#include <iostream> using namespace std; int a[6][6]; int row[6],col[6]; int main(){ for(int i=0;i<6;i++) col[i]=0x7fffffff; for(int i=0;i<5;i++) for(int j=0;j<5;j++){ cin >> a[i][j]; if(a[i][j]>row[i])row[i]=a[i][j]; if(a[i][j]<col[j])col[j]=a[i][j]; } int flag = 1; for(int i=0;i<5;i++) for(int j=0;j<5;j++){ if(row[i]==col[j]){ cout << i+1 << " " << j+1 << " " << a[i][j] << endl; flag = 0; } } if(flag) cout << "not found" << endl; return 0; }
#include <iostream> using namespace std; int a[105][105],n,m,p; double sum = 0; int main(){ cin >> n >> m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j]; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ cin >> p; if(p==a[i][j]){ sum = sum + 1; } } printf("%.2f\n",100*sum/(n*m)); return 0; }
#include <iostream> using namespace std; int a[105][105],n; int row[105],col[105]; void print(){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cout << a[i][j] << " "; } cout << endl; } cout << endl; } int main(){ cin >> n; for(int i=0;i<n;i++){ row[i]=0x7fffffff; col[i]=0x7fffffff; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin >> a[i][j]; if(a[i][j]<row[i]) row[i]=a[i][j]; } } int t = n; do{ cout << a[1][1] << endl; for(int i=0;i<t;i++){ for(int j=0;j<t;j++){ a[i][j] = a[i][j]-row[i]; if(a[i][j]<col[j]) col[j]=a[i][j]; } } //print(); for(int i=0;i<t;i++){ for(int j=0;j<t;j++){ a[j][i] = a[j][i] - col[i]; } } //print(); for(int i=0;i<t;i++){ row[i]=a[i][0]; for(int j=1;j<t-1;j++){ a[i][j]=a[i][j+1]; if(a[i][j]<row[i]) row[i]=a[i][j]; } col[i]=a[0][i]; } //print(); for(int i=1;i<t-1;i++){ for(int j=0;j<t;j++){ a[i][j]=a[i+1][j]; } row[i]=row[i+1]; } }while(--t>0); return 0; }
#include <iostream> #include <cstdio> using namespace std; int a[105][105]; int n,m,b; int main(){ cin >> n >> m; for(int i=1;i<=n;i++) for(int j=1;j<=m ;j++) cin >> a[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ cin >> b; a[i][j]+=b; } for(int i=1;i<=n;i++){ cout << a[i][1]; for(int j=2;j<=m;j++){ cout << " " << a[i][j]; } cout << endl; } return 0; }
#include <iostream> #include <cstdio> using namespace std; int n,m,k; int a[105][105],b[105][105],c[105][105]; int main(){ cin >> n >> m >> k; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin >> a[i][j]; for(int i=1;i<=m;i++) for(int j=1;j<=k;j++) cin >> b[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) for(int l=1;l<=m;l++) c[i][j] = c[i][j] + a[i][l] * b[l][j]; for(int i=1;i<=n;i++){ cout << c[i][1]; for(int j=2;j<=k;j++){ cout << " " << c[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; int n,m,a[105][105]; int main(){ cin >> n >> m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout << a[j][i] << " "; cout << endl; } return 0; }
#include <iostream> using namespace std; int n,m,a[105][105]; int main(){ cin >> n >> m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j]; for(int i=0;i<m;i++){ for(int j=n-1;j>=0;j--) cout << a[j][i] << " "; cout << endl; } return 0; }
#include <iostream> using namespace std; int n; char a[10][10],b[10][10]; bool judge1(); bool judge2(); bool judge3(); bool judge4(); int main(){ cin >> n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> a[i][j]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> b[i][j]; if(judge1()) cout << 1 << endl; else if(judge2()) cout << 2 << endl; else if(judge3()) cout << 3 << endl; else if(judge4()) cout << 4 << endl; else cout << 5 << endl; return 0; } bool judge1(){ bool ret = true; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i][j]!=b[j][n-i-1]){ ret = false; } return ret; } bool judge2(){ bool ret = true; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i][j]!=b[n-j-1][i]){ ret = false; } return ret; } bool judge3(){ bool ret = true; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i][j]!=b[n-i-1][n-j-1]){ ret = false; } return ret; } bool judge4(){ bool ret = true; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i][j]!=b[i][j]){ ret = false; } return ret; }
#include <iostream> using namespace std; int n,m,b[105][105]; double a[105][105]; int main(){ cin >> n >> m; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ cin >> a[i][j]; b[i][j] = a[i][j]; } if(n>=3 && m>=3) for(int i=1;i<n-1;i++) for(int j=1;j<m-1;j++){ b[i][j] = (a[i][j]+a[i][j-1]+a[i][j+1]+a[i-1][j]+a[i+1][j])/5+0.5; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++) cout << b[i][j] << " "; cout << endl; } return 0; }
#include <iostream> using namespace std; char d[105][105]; char mine[105][105]; int x[8]={-1,0,1,-1,1,-1,0,1}; int y[8]={-1,-1,-1,0,0,1,1,1}; int n,m; int main(){ cin >> n >> m; for(int i=0;i<n;i++) scanf("%s",d[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(d[i][j]=='*') mine[i][j]='*'; else{ int tmp = '0'; for(int k=0;k<8;k++){ int x1 = i+x[k]; int y1 = j+y[k]; if(x1>=0 && x1<=n && y1>=0 && y1<=m){ if(d[x1][y1]=='*') tmp = tmp + 1; } } mine[i][j]=tmp; } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cout << mine[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; long long a[12][12],b[12][12],m,n; int x[8]={-1,0,1,-1,1,-1,0,1}; int y[8]={-1,-1,-1,0,0,1,1,1}; int main(){ cin >> m >> n; a[5][5] = m; for(int t=1;t<=n;t++){ for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ b[i][j] = b[i][j]+ a[i][j]*2; for(int k=0;k<8;k++){ int x1=i+x[k]; int y1=j+y[k]; b[x1][y1] = b[x1][y1] + a[i][j]; } } } for(int i=1;i<=9;i++) for(int j=1;j<=9;j++){ a[i][j] = b[i][j]; b[i][j] = 0; } } for(int i=1;i<=9;i++){ cout << a[i][1]; for(int j=2;j<=9;j++) cout << " " << a[i][j]; cout << endl; } return 0; }
#include <iostream> using namespace std; char a[101][101],b[101][101]; int c,r,n; int main(){ cin >> r >> c >> n; for(int i=0;i<r;i++) for(int j=0;j<c;j++){ cin >> a[i][j]; b[i][j] = a[i][j]; } while(n--){ for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ switch(a[i][j]){ case 'R': if((i+1<r&&a[i+1][j]=='P')||(j+1<c&&a[i][j+1]=='P')||(i-1>=0&&a[i-1][j]=='P')||(j-1>=0&&a[i][j-1]=='P')) b[i][j]='P'; break; case 'S': if((i+1<r&&a[i+1][j]=='R')||(j+1<c&&a[i][j+1]=='R')||(i-1>=0&&a[i-1][j]=='R')||(j-1>=0&&a[i][j-1]=='R')) b[i][j]='R'; break; case 'P': if((i+1<r&&a[i+1][j]=='S')||(j+1<c&&a[i][j+1]=='S')||(i-1>=0&&a[i-1][j]=='S')||(j-1>=0&&a[i][j-1]=='S')) b[i][j]='S'; break; } } } for(int i=0;i<r;i++) for(int j=0;j<c;j++){ a[i][j] = b[i][j]; } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cout << a[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; char f[101][101]; int r,c; int main(){ cin >> r >> c; for(int i=0;i<r;i++) cin >> f[i]; int cnt = 0; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if(f[i][j]=='#'){ f[i][j]='.'; if(f[i][j+1]=='#'){ f[i][j+1]='.'; }else if(f[i+1][j]=='#'){ f[i+1][j]='.'; } cnt++; } } } cout << cnt << endl; return 0; }
#include <iostream> using namespace std; int p[1005][1005],n,x1,y1,x2,y2; int main(){ cin >> n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> p[i][j]; for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(p[i][j]==0){ if(i<(n-1) && j<(n-1) && p[i+1][j]==0&&p[i][j+1]==0){ x1=i; y1=j; }else if(j>0&&i>0&&p[i-1][j]==0&&p[i][j-1]==0){ x2=i; y2=j; } } } if(x1!=0 || x2!=0) cout << (x2-x1-1)*(y2-y1-1) << endl; else cout << 0 << endl; return 0; }
#include <iostream> using namespace std; int p[105][105],n,s,l; int main(){ cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin >> p[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(p[i][j]<=50){ s++; if(i==0 || j==0 || i==n-1 || j==n-1) l++; else if(p[i-1][j]>50 || p[i+1][j]>50 || p[i][j-1]>50 || p[i][j+1]>50) l++; } } } cout << s << " " << l << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char w[105][21],s[205]; int n,x,y; int main(){ cin >> n >> s; int len = strlen(s); x = -1; for(int i=0;i<len;i++){ y = i %n; if(y==0) x++; if(x%2==0) w[x][y] = s[i]; else w[x][n-y-1] = s[i]; } for(int j=0;j<n;j++){ for(int i=0;i<=x;i++){ cout << w[i][j]; } } cout << endl; return 0; }
#include <iostream> using namespace std; int row,col,arr[105][105],x,y,tot,r,c; int main(){ cin >> row >> col; for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ cin >> arr[i][j]; } } tot = row * col; r=x = 0; c=y = 0; while(tot--){ cout << arr[x][y] << endl; if(y==0 || x==row-1){ c++; if(c>=col){ y = col-1; r++; x = r; }else{ y=c; x=0; } }else{ x++; y--; } } return 0; }
#include <iostream> using namespace std; int n,m[50][50],x1,x2,y1,y2,c,t,p; int main(){ cin >> n; x1 = 0; y1 = n-1; p = 1; t=(2*n-1)*(2*n-1)-1; m[x1][y1] = p; while(t--){ if(x1==0){ if(y1==2*n-2){ x2 = x1+1; y2 = y1; }else{ x2 = 2*n-2; y2= y1+1; } }else if(y1==2*n-2){ y2=0; x2=x1-1; }else{ y2=y1+1; x2=x1-1; if(m[x2][y2]!=0){ x2=x1+1; y2=y1; } } m[x2][y2]=++p; x1 = x2; y1 = y2; } t = 2*n-1; for(int i=0;i<t;i++){ cout << m[i][0]; for(int j=1;j<t;j++){ cout << " " << m[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; int a[100][100],n,m,row=1,col=1; int lft,rgt,top,btm; int main(){ cin >> n >> m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> a[i][j]; } } while(row<=n||col<=m){ for(int i=row,j=col;j<=m&&row<=n;j++) cout << a[i][j] << endl; row++; for(int i=row,j=m;i<=n&&col<=m;i++) cout << a[i][j] << endl; m--; for(int i=n,j=m;j>=col&&row<=n;j--) cout << a[i][j] << endl; n--; for(int i=n,j=col;i>=row&&col<=m;i--) cout << a[i][j] << endl; col++; } return 0; }
#include <iostream> #include <cstdlib> using namespace std; int a[20][20],x,y,t,n,p=1; int main(){ cin >> n; t = n*n; x=0; y=0; a[x][y]=p++; while(p<=t){ if(x==0){ y++; a[x][y]=p++; while(y!=0 && p<=t){ x++; y--; a[x][y]=p++; } } if(y==0 && x!=n-1 && p<=t){ x++; a[x][y]=p++; while(x!=0){ x--; y++; a[x][y]=p++; } } if(x==n-1 && p<=t){ y++; a[x][y]=p++; while(y!=n-1){ x--; y++; a[x][y]=p++; } } if(y==n-1 && p<=t){ x++; a[x][y]=p++; while(x!=n-1){ x++; y--; a[x][y]=p++; } } } for(int i=0;i<n;i++){ cout << a[i][0]; for(int j=1;j<n;j++){ cout << " " << a[i][j]; } cout << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int r,c; char m[21][21],dst[21][21]; string s,src; string getc(char ch){ char t[6]; int cn=0; if(ch!=' ') cn = ch-'A' + 1; for(int i=0;i<5;i++){ t[4-i]=char(48+cn%2); cn= cn /2; } t[5]='\0'; return t; } int main(){ cin >> r >> c ; int x,y,row=0,col=0; x=r; y=c; getchar(); getline(cin,s); src=""; for(int i=0;i<s.size();i++) src = src + getc(s[i]); int tt = r*c,p=0; for(int i=src.size();i<=tt;i++) src = src + '0'; while(col<=c || row<=r){ for(int i=row,j=col;j<c&&row<r;j++) dst[i][j] = src[p++]; row++; for(int i=row,j=c-1;i<r&&col<c;i++) dst[i][j] = src[p++]; c--; for(int i=r-1,j=c-1;j>=col&&row<r;j--) dst[i][j] = src[p++]; r--; for(int i=r-1,j=col;i>=row&&col<c;i--) dst[i][j] = src[p++]; col++; } for(int i=0;i<x;i++){ for(int j=0;j<y;j++) cout << dst[i][j]; } return 0; }
按字母表顺序和逆序每隔一个字母打印,即打印出:
a c e g i k m o q s u w y
z x v t r p n l j h f d b
#include <iostream> #include <iomanip> using namespace std; int main(){ for(char letter='a';letter<='z';letter+=2) cout << setw(3) << letter; cout << endl; for(char letter='z';letter>='a';letter-=2) cout << setw(3) << letter; cout << endl; return 0; }
在应用计算机编辑文档的时候,我们经常遇到替换任务。如把文档中的“电脑”都替换成“计算机”。现在请你编程模拟一下这个操作。
输入两行内容,第1行是原文(长度不超过200个字符),第2行包含以空格分隔的两个字符A和B,要求将原文中所有字符A都替换成字符B。注意:区分大小写字母。
输入样例:
I Love China. I love Beijing.
I U
输出样例:
U Love China. U love Beijing.
#include <iostream> using namespace std; char a,b,str[200]; int n=0; int main(){ while((str[n++]=getchar())!='\n'); a=getchar(); getchar(); b=getchar(); for(int i=0;i<n;i++) if(str[i]==a) cout << b; else cout << str[i]; cout << endl; return 0; }
过滤多余的空格。一个句子中也许有多个连续的空格,过滤掉多余的空格,只留下一个空格。
输入:一行,一个字符串(长度不超过200),句子的头和尾都没有空格。
输出:过滤之后的句子。
样例输入:Hello world. This is c language.
样例输出:Hello world. This is c language.
#include <iostream> using namespace std; char st[200]; int main(){ while(scanf("%s",st)==1) printf("%s ",st); return 0; }
C++中,一个字符串中的字符可以通过其对应的下标灵活使用。
#include <iostream> #include <cstring> using namespace std; char st[100]; int main(){ gets(st); for(int i=0;i<strlen(st);i++) cout << st[i]; cout << endl; return 0; }
对给定的10个国家名,按其字母的顺序输出。
#include <iostream> #include <cstring> using namespace std; char t[21],cname[11][12]; int main(){ for(int i=1;i<=10;i++) gets(cname[i]); for(int i=1;i<=10;i++){ int k=i; for(int j=i+1;j<=10;j++) if(strcmp(cname[k],cname[j])>0) k=j; strcpy(t,cname[i]); strcpy(cname[i],cname[k]); strcpy(cname[k],t); } for(int i=1;i<=10;i++) cout << cname[i] << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; string cname[10]; int main(){ for(int i=0;i<10;i++) getline(cin,cname[i]); sort(cname,cname+10); for(int i=0;i<10;i++) cout << cname[i] << endl; return 0; }
字符串判等:判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。
输入:两行,每行包含一个字符串。
输出:若两个字符串相等,输出YES,否则输出NO。
输入样例:
a A bb BB ccc CCC
Aa BBbb CCCccc
输出样例:
YES
#include <iostream> #include <cstring> using namespace std; const int N = 256; char s1[N],s2[N],a[N],b[N]; int l1,l2; int main(){ gets(s1); gets(s2); strlwr(s1); strlwr(s2); for(int i=0;i<strlen(s1);i++) if(s1[i]!=' ') a[l1++] = s1[i]; for(int i=0;i<strlen(s2);i++) if(s2[i]!=' ') b[l2++] = s2[i]; if(strcmp(a,b)==0) printf("YES\n"); else printf("NO\n"); return 0; }
字符串移位包含问题。对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。
给定两个字符串s1和s2,要求判定其中一个字符串是否是另一个字符串通过若干次循环移位后的新字符串的子串。例如CDAA是由AABCD两次移位后产生的新串BCDAA的子串,而ABCD与ACBD则不能通过多次移位来得到其中一个字符串是新的子串。
输入:一行,包含两个字符串,中间由单个空格隔开。字符串只包含字母和数字,长度不超过30。
输出:如果一个字符串是另一个字符串通过若干次循环移位产生的新串的子串,则输出true,否则输出false。
输入样例:AABCD CDAA
输出样例:true
#include <iostream> #include <cstring> using namespace std; const int N=61; char s1[N],s2[N],x[N],t[N]; int l1,l2; int main(){ scanf("%s%s",&s1,&s2); if(strlen(s1)<strlen(s2)){ strcpy(t,s1); strcpy(s1,s2); strcpy(s2,t); } strcpy(x,s1); if(strstr(strcat(s1,x),s2)==NULL) printf("false\n"); else printf("true\n"); return 0; }
#include <iostream> #include <cstring> using namespace std; int main(){ char cs[300]; cin.getline(cs,300); int len = strlen(cs); int sum = 0; for(int i=0;i<len;i++) if(cs[i]>='0' && cs[i]<='9') sum = sum + 1; cout << sum << endl; return 0; }
/* example 1: abcabcdefabcd abcdef 333211 abcabcdefabcd 33333321 steps: step1: read string into memory step2: count number of each character step3: find first character of number one step4: output answer */ #include <iostream> using namespace std; int count[26]; int main(){ char txt[100005]; int ntxt=0,flag=0; while((txt[ntxt]=getchar())!='\n'){ count[txt[ntxt]-'a'] = count[txt[ntxt]-'a']+1; ntxt++; } for(int i=0;i<ntxt;i++) if(count[txt[i]-'a']==1){ cout << txt[i] << endl; flag=1; break; } if(!flag){ cout << "no" << endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; int main(){ double h,count=0.0; char a[500]; char b[500]; cin >> h; cin >> a; cin >> b; int len = strlen(a); for(int i=0;i<len;i++){ if(a[i]==b[i]){ count = count + 1; } } if(count/len>h) cout << "yes" << endl; else cout << "no" << endl; return 0; }
#include <iostream> using namespace std; int p(char a){ return (int)a; } int main(){ int n; char s1[10],s2[10]; cin >> n; for(int i=0;i<n;i++){ cin >> s1 >> s2 ; if(p(s1[0])==p(s2[0])){ cout << "Tie" << endl; }else{ if(p(s1[0])==80){ //player1==Paper if(p(s2[0])==82) //player2=Rock cout << "Player1" << endl; else //player2=Scissors cout << "Player2" << endl; }else if(p(s1[0])==82){ //player1=Rock if(p(s2[0])==80) //player2=paper cout << "Player2" << endl; else //player2=Scissors cout << "Player1" << endl; }else{ //player1=Scissors if(p(s2[0])==80) //player2=paper cout << "Player1" << endl; else //player2=rock cout << "Player2" << endl; } } } return 0; }
#include <iostream> using namespace std; string s; int main(){ getline(cin,s); for(int i=0;i<s.size()-1;i++){ cout << char(s[i]+s[i+1]); } cout << char(s[0]+s[s.size()-1]) << endl; return 0; }
#include <iostream> using namespace std; int main(){ string s; getline(cin,s); int i=0; for(;i<s.size();i++){ if(s[0]>='0' && s[0]<='9'){ break; } if(s[i]=='_' || (s[i]>='0' && s[i]<='9') || (s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z')) continue; else break; } if(i==s.size()){ cout << "yes" << endl; }else{ cout << "no" << endl; } return 0; }
#include <iostream> using namespace std; int main(){ string s; getline(cin,s); for(int i=0;i<s.size();i++){ switch(s[i]){ case 'A':cout << 'T';break; case 'T':cout << 'A';break; case 'G':cout << 'C';break; case 'C':cout << 'G';break; } } return 0; }
#include <iostream> #include <cstring> using namespace std; char str[40],src,dst; int main(){ cin >> str >> src >> dst; int len = strlen(str); for(int i=0;i<len;i++){ if(str[i]==src){ str[i] = dst; } } for(int i=0;i<len;i++){ cout << str[i]; } cout << endl; return 0; }
#include <iostream> using namespace std; int main(){ string s; getline(cin,s); for(int i=0;i<s.size();i++){ if(s[i]>='a' && s[i]<='z'){ s[i] = 'a' + (s[i]-'a' +1) % 26; } if(s[i]>='A' && s[i]<='Z'){ s[i] = 'A' + (s[i]-'A' +1) % 26; } } cout << s << endl; return 0; }
#include <iostream> using namespace std; int main(){ int tmp; string s; getline(cin,s); for(int i=0;i<s.size();i++){ if(s[i]>='A' && s[i]<='Z'){ tmp = s[i]-5; if(tmp < 65) s[i]='Z'+tmp-64; else s[i] = tmp; } } cout << s << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char inf[105],src[105],dst[105]; int book[300],num[300],inflen,srclen,dstlen; bool flag=true; int main(){ scanf("%s",inf); scanf("%s",src); scanf("%s",dst); inflen = strlen(inf); srclen = strlen(src); dstlen = strlen(dst); for(int i=0;i<inflen;i++){ if(book[inf[i]]==0 && src[i]!=0){ book[inf[i]]=src[i]; }else if(book[inf[i]]!=src[i]){ flag=false; } } if(flag){ for(int i=65;i<=90;i++){ if(book[i]==0){ flag=false; break; } } } for(int i=0;i<srclen;i++){ if(num[src[i]]==0 && inf[i]!=0){ num[src[i]]=inf[i]; }else if(num[src[i]]!=inf[i]){ flag = false; } } if(flag){ for(int i=65;i<=90;i++){ if(num[i]==0){ flag=false; break; } } } if(flag){ for(int i=0;i<dstlen;i++){ cout << char(book[dst[i]]); } cout << endl; }else{ cout << "Failed" << endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; char src[100],dst[100]; int main(){ cin >> src; int len = strlen(src); for(int i=0;i<len;i++){ char c = src[len-i-1]; if(c>='A' && c<='Z'){ c = c + 35; if(c>'z') c = 'a' + (c-'z'-1); } else if(c>='a' && c<='z'){ c = c - 29; if(c>'Z') c = 'A' + (c-'Z'-1); } dst[i] = c; } cout << dst << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char s[105]; int main(){ gets(s); int len = strlen(s); for(int i=0;i<len;i++){ if(s[i]>='a' && s[i]<='z') s[i]=s[i]-32; } cout << s << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char s[90]; int main(){ gets(s); int len = strlen(s); for(int i=0;i<len;i++){ if(int(s[i])>=65 && int(s[i])<=90) cout << char(s[i]+32); else if(int(s[i])>=97 && int(s[i])<=122) cout << char(s[i]-32); else cout << s[i]; } cout << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; int n,len; char s[100]; int main(){ cin >> n; getchar(); for(int i=0;i<n;i++){ gets(s); len = strlen(s); if(s[0]>='a' && s[0]<='z') s[0]=s[0]-32; for(int j=1;j<len;j++){ if(s[j]>='A' && s[j]<='Z') s[j]=s[j]+32; } cout << s << endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; char src[100],dst[100]; int ls,ld,i; void toUp(char * a){ int len= strlen(a); for(int i=0;i<len;i++){ if(a[i]>='a' && a[i]<='z') a[i]=a[i]-32; } } int main(){ gets(src); toUp(src); gets(dst); toUp(dst); ls = strlen(src); ld = strlen(dst); int len = ls < ld ? ls:ld; for(i=0;i<len;i++){ if(src[i]>dst[i]){ cout <<">" << endl; break; }else if(src[i]<dst[i]){ cout << "<" << endl; break; } } if(i==len){ if(ls==ld){ cout << "=" << endl; }else{ char c = ls<ld?'<':'>'; cout << c << endl; } } return 0; }
#include <iostream> #include <string> using namespace std; string src1,src2,dst1,dst2; string filt(string s){ string tmp = ""; int len = s.length(); for(int i=0;i<len;i++){ if(s[i]!=' ') if(s[i]>='a' && s[i]<='z') tmp = tmp + char(s[i]-32); else tmp = tmp + s[i]; } return tmp; } int main(){ getline(cin,src1); getline(cin,src2); dst1 = filt(src1); dst2 = filt(src2); if(dst1.compare(dst2)==0){ cout << "YES" << endl; }else{ cout << "NO" << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string s1,s2; int main(){ getline(cin,s1); getline(cin,s2); string::size_type idx1,idx2; idx1 = s1.find(s2); idx2 = s2.find(s1); if(idx1==string::npos && idx2==string::npos){ cout << "No substring" << endl; }else if(idx1==string::npos){ cout << s1 <<" is substring of " << s2 << endl; }else{ cout << s2 <<" is substring of " << s1 << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string s1,s2,d1,d2; int main(){ cin >> s1 >> s2; d1 = s1+s1; d2 = s2+s2; if(d1.find(s2)!=-1 && s2.size()<=s1.size()){ cout << "true" << endl; }else if(d2.find(s1)!=-1 && s1.size()<=s2.size()){ cout << "true" << endl; }else{ cout << "false" << endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; char post[3][5]={"er","ly","ing"},s[50]; int main(){ cin >> s; int l = strlen(s); for(int i=0;i<3;i++){ int len = strlen(post[i]),j; for(j=0;j<len;j++){ if(post[i][len-j-1]!=s[l-j-1]) break; } if(j==len){ s[l-j] = '\0'; break; } } cout << s << endl; return 0; }
#include <iostream> #include <string> using namespace std; string st[100],src,dst; int n=0; int main(){ while(cin >> st[n++]); src = st[n-3]; dst = st[n-2]; n=n-3; for(int i=0;i<n;i++){ if(st[i].compare(src)==0){ st[i] = dst; } } cout << st[0]; for(int i=1;i<n;i++) cout << " " << st[i]; cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; string email,mail,usr,psw; int n,flag=1; int main(){ cin >> email >> n; for(int i=0;i<n;i++){ cin >> usr >> psw >> mail; if(email.compare(mail)==0){ flag = 0; int len = psw.size(); for(int i=0;i<len;i++){ if(psw[i]>='A' && psw[i]<='Z') psw[i] = psw[i] + 32; else if(psw[i]>='a' && psw[i]<='z') psw[i] = psw[i] - 32; } cout << usr << " " << psw << endl; } } if(flag){ cout << "empty" << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main(){ string s,temp; cin >> s; while(cin >> temp){ s += ' ' + temp; } cout << s << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char s[1005]; int main(){ scanf("%s",s); cout << strlen(s); while(scanf("%s",s)==1){ cout << "," << strlen(s); } cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; string st[300]; int n=0,mn=100000,mx=0,pmax,pmin; int main(){ while(cin>>st[n]){ int len = st[n].size(); if(st[n][0]==',') st[n].erase(0,1); if(st[n][len-1]==',') st[n].erase(len-1,0); n++; } for(int i=0;i<n;i++){ if(st[i].length()>mx){ mx = st[i].length(); pmax = i; } if(st[i].length()<mn){ mn = st[i].length(); pmin = i; } } cout << st[pmax] << endl; cout << st[pmin] << endl; return 0; }
#include <iostream> #include <string> using namespace std; string s,src,lft,rgt; int len,l,r; int main(){ cin >> s; len = s.length(); l = s.find_first_of(","); r = s.find_last_of(","); src = s.substr(0,l); lft = s.substr(l+1,r-l-1); rgt = s.substr(r+1,len-r-1); l = src.find(lft); r = src.rfind(rgt); if(l==-1 || r ==-1){ cout << -1 << endl; }else{ if(r>=l+lft.length()) cout << r -l -lft.length() << endl; else cout << "-1" << endl; } return 0; }
#include <iostream> #include <cstring> #include <algorithm> using namespace std; char str[505]; int l,r=0; int main(){ gets(str); int len = strlen(str); while(r<len){ while(str[r]==' '&&r<len) r++; l=r; while(str[r]!=' ' && r<len){ r++; } reverse(str+l,str+r); } cout << str << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; vector<string> vs; string s; int main(){ while(cin>>s){ vs.push_back(s); } int len = vs.size(); cout << vs[len-1]; for(int i=len-2;i>=0;i--) cout << " " << vs[i]; cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; string isbn; int sum = 0,p=1; int main(){ cin >> isbn; sum = 0; for(int i=0;i<11;i++) if(isbn[i]!='-') sum = sum + (isbn[i]-'0')*p++; if((isbn[12]=='X' && sum%11==10) || (isbn[12]-'0'==sum%11)) cout << "Right" << endl; else if(sum%11==10){ isbn[12]='X'; cout << isbn <<endl; }else{ isbn[12] = sum%11+'0'; cout << isbn << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string s1,s2,s; int len,mx=0; int main(){ cin >> s1 >> s2; if(s1.length()>s2.length()){ s = s1; s1= s2; s2 = s; } len = s1.length(); s1 = s1 + s1; s2 = s2 + s2; mx = 0; for(int l=1;l<=len;l++){ for(int i=0;i<=2*len-l;i++){ s = s1.substr(i,l); if(s2.find(s)!=string::npos){ if(s.size()>mx){ mx = s.size(); } } } } cout << mx << endl; return 0; }
#include <iostream> #include <cstring> #include <string> #include <stack> using namespace std; char s[1005],ans[2005],d; int len,cnt,p; string ntos(int a){ stack<char> stk; string t = ""; while(a){ stk.push(char(a%10+48)); a=a/10; } while(!stk.empty()){ t = t + stk.top(); stk.pop(); } return t; } int main(){ cin >> s; len = strlen(s); d=s[0]; cnt = 1; p=0; for(int i=1;i<len;i++){ if(s[i]!=d){ string r = ntos(cnt); for(int j=0;j<r.size();j++){ ans[p++] = r[j]; } ans[p++] = d; cnt=1; d=s[i]; }else{ cnt++; } } string r = ntos(cnt); for(int j=0;j<r.size();j++){ ans[p++] = r[j]; } ans[p++] = d; cout << ans << endl; return 0; }
#include <iostream> #include <cstring> #include <string> #include <stack> using namespace std; char s[1005],ans[2005],d; int len,cnt; void toup(char a[]){ int l = strlen(a); for(int i=0;i<l;i++) if(a[i]>='a' && a[i]<='z') a[i]=a[i]-32; } int main(){ cin >> s; toup(s); len = strlen(s); d=s[0]; cnt = 1; for(int i=1;i<len;i++){ if(s[i]!=d){ cout << "(" << d << "," << cnt << ")"; cnt=1; d=s[i]; }else{ cnt++; } } cout << "(" << d << "," << cnt << ")" << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; char c[105]; int len,flag=1,mid; int main(){ cin >> c ; len = strlen(c); mid = len/2; for(int i=0;i<mid;i++){ if(c[i]!=c[len-i-1]){ flag=0; break; } } if(flag) cout << "yes" << endl; else cout << "no" << endl; return 0; }
#include <iostream> #include <string> using namespace std; string s; int main(){ cin >> s; int len = s.size(),end,right,left; for(int i=2;i<=len;i++){ //枚举子串的长度 end = len - i; for(int begin=0;begin<=end;begin++){ right = begin+i-1; for(left=begin;left<right;left++,right--){ if(s[left]!=s[right]) break; } if(left>=right){ right = begin+i-1; for(left=begin;left<=right;left++) cout << s[left]; cout << endl; } } } return 0; }
#include <iostream> #include <string> using namespace std; int p1,p2,p3; string src,dst=""; char tolower(char c){ if(c>='A' && c<='Z') return c + ('a'-'A'); return c; } char toupper(char c){ if(c>'a' && c<='z') return c + ('A'-'a'); return c; } bool isnumber(char c){ return (c>='0' && c<='9')?1:0; } bool ischar(char c){ return (c>='a' && c <='z' || c >='A' && c <='Z')?1:0; } int main(){ int p=0; cin >> p1 >> p2 >> p3; cin >> src; dst=src[0]; for(int i=1;i<src.size()-1;i++){ if(src[i]=='-'){ if(isnumber(src[i-1]) && isnumber(src[i+1]) || ischar(src[i-1])&&ischar(src[i+1])){ if(src[i+1]>src[i-1]){ if(p3==1){ //顺序 for(char j=src[i-1]+1;j<src[i+1];j++){ for(int k=1;k<=p2;k++){ switch(p1){ case 1: if(isnumber(j)) dst = dst + j; else dst = dst + tolower(j); break; case 2: if(isnumber(j)) dst = dst + j; else dst = dst + toupper(j); break; case 3: dst = dst + '*'; break; } } } }else{ //逆序 for(char j=src[i+1]-1;j>src[i-1];j--){ for(int k=1;k<=p2;k++){ switch(p1){ case 1: if(isnumber(j)) dst = dst + j; else dst = dst + tolower(j); break; case 2: if(isnumber(j)) dst = dst + j; else dst = dst + toupper(j); break; case 3: dst = dst + '*'; break; } } } } }else{ dst = dst + src[i]; } }else{ dst = dst + src[i]; } }else{ dst = dst + src[i]; } } dst = dst + src[src.length()-1]; cout << dst << endl; return 0; }