• noi寒假刷题之旅_ 1.8编程基础之多维数组(25题)


    »1.8编程基础之多维数组(25题)

    上次编辑的时候忘记保存了,前面几题就算了趴懒得

    08:矩阵加法

    #include<iostream>
    #define MAX 105
    using namespace std;
    int table[MAX][MAX];
    int main()
    {
    	int n,m;
    	cin>>n>>m;
    	for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>table[i][j];
    	int t;
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<m;++j)
    		{
    			cin>>t;
    			if(j==0)
    			{
    				cout<<table[i][j]+t;
    			}
    			else
    			{
    				cout<<' '<<table[i][j]+t;	
    			}
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    09:矩阵乘法

    #include<iostream>
    #define MAX 105
    using namespace std;
    int table1[MAX][MAX];
    int table2[MAX][MAX];
    int table3[MAX][MAX];
    int main()
    {
    	int n,m,k;
    	cin>>n>>m>>k;
    	for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>table1[i][j];
    	for(int i=0;i<m;++i)for(int j=0;j<k;++j)cin>>table2[i][j];
    	int t;
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<k;++j)
    		{
    			for(int z=0;z<m;++z)table3[i][j] += table1[i][z]*table2[z][j];
    		}
    	}
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<k;++j)
    		{
    			if(j!=0)
    			{
    				cout<<" "<<table3[i][j];
    			}
    			else
    			{
    				cout<<table3[i][j];
    			}		
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    10:矩阵转置

    #include<iostream>
    #define MAX 105
    using namespace std;
    int table1[MAX][MAX];
    int main()
    {
    	int n,m,k;
    	cin>>n>>m;
    	for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>table1[i][j];
    	for(int i=0;i<m;++i)
    	{
    		for(int j=0;j<n;++j)
    		{
    			if(j!=0)
    			{
    				cout<<" "<<table1[j][i];
    			}
    			else
    			{
    				cout<<table1[j][i];
    			}		
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    11:图像旋转

    #include<iostream>
    #define MAX 105
    using namespace std;
    int table1[MAX][MAX];
    int main()
    {
    	int n,m,k;
    	cin>>n>>m;
    	for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>table1[i][j];
    	for(int i=0;i<m;++i)
    	{
    		for(int j=n-1;j>=0;--j)
    		{
    			if(j!=n-1)
    			{
    				cout<<" "<<table1[j][i];
    			}
    			else
    			{
    				cout<<table1[j][i];
    			}		
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    12:变幻的矩阵

    #include<iostream>
    #include<string>
    #define MAX 105
    using namespace std;
    string table1[MAX][MAX];
    string table2[MAX][MAX];
    int main()
    {
    	int n;
    	cin>>n;
    	for(int i=0;i<n;++i)for(int j=0;j<n;++j)cin>>table1[i][j];
    	for(int i=0;i<n;++i)for(int j=0;j<n;++j)cin>>table2[i][j];
    	int flag=0;
    	for(int i=0,ii=0;i<n;++i,++ii)
    	{
    		for(int j=n-1,jj=0;j>=0;--j,++jj)
    		{
    			if(table1[j][i]!=table2[ii][jj])
    			{
    				flag=1;
    			}	
    		}
    	}
    	if(flag)
    	{
    		flag=0;
    		for(int i=n-1,ii=0;i>=0;--i,++ii)
    		{
    			for(int j=0,jj=0;j<n;++j,++jj)
    			{
    				if(table1[j][i]!=table2[ii][jj])
    				{
    					flag=1;
    				}	
    			}
    		}
    		if(flag)
    		{
    			flag=0;
    			for(int i=0;i<n;++i)
    			{
    				for(int j=0;j<n;++j)
    				{
    					if(table1[i][j]!=table2[i][j])
    					{
    						flag=1;
    					}	
    				}
    			}
    			if(flag)
    			{
    				flag=0;
    				for(int i=n-1,ii=0;i>=0;--i,++ii)
    				{
    					for(int j=n-1,jj=0;j>=0;--j,++jj)
    					{
    						if(table1[i][j]!=table2[ii][jj])
    						{
    							flag=1;
    						}	
    					}
    				}
    				if(flag)
    				{
    					cout<<5;
    				}
    				else
    				{
    					cout<<3;
    				}
    			}
    			else
    			{
    				cout<<4;
    			}
    		}
    		else
    		{
    			cout<<2;
    		}
    	}
    	else
    	{
    		cout<<1;
    	}
    	return 0;	
    }
    

    13:图像模糊处理【四舍五入取上整】

    #include<iostream>
    #include<cmath>
    #define MAX 105
    using namespace std;
    double table1[MAX][MAX];
    double table2[MAX][MAX];
    int main()
    {
    	int n,m;
    	cin>>n>>m;
    	for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>table1[i][j];
    	int flag=0;
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<m;++j)
    		{
    			if(!(i==0||j==0||i==n-1||j==m-1))
    			{
    				table2[i][j]=((table1[i][j]+table1[i-1][j]+table1[i+1][j]+table1[i][j+1]+table1[i][j-1])/5);
    			}	
    			else
    			{
    				table2[i][j] = table1[i][j];
    			}
    		}
    	
    	}
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<m;++j)
    		{
    			if(j==0)printf("%.0f",table2[i][j]);
    			else printf(" %.0f",table2[i][j]);
    		}
    		cout<<endl;
    	}
    	return 0;	
    }

    两种四舍五入的方式

    #include<iostream>
    using namespace std;
    int main()
    {
    	double a=5.12;
    	double b=4.55;
    	/*
    	printf()函数会根据格式要求(%.nf)自动对float类型小数进行四舍五入,输出到小数点后面的第n位,第n+1位进行“四舍五入”
    	*/
    	printf("1:四舍:%.0f,五入:%.0f
    ",a,b);
    	printf("2:四舍:%d,五入:%d
    ",int(a+0.5),int(b+0.5));
    }

    14:扫雷游戏地雷数计算

    #include<iostream>
    #include<string> 
    #include<cmath>
    #define MAX 105
    using namespace std;
    char table1[MAX][MAX]={};
    int table2[MAX][MAX]={};
    int main()
    {
    	int n,m;
    	cin>>n>>m;	
    	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)cin>>table1[i][j];
    
    	int flag=0;
    	for(int i=1;i<=n;++i)
    	{
    		for(int j=1;j<=m;++j)
    		{
    			if(table1[i][j]=='*')
    			{
    				table2[i][j]=-1;
    			}
    			else
    			{	
    				table2[i][j]=(table1[i-1][j]=='*')+(table1[i+1][j]=='*')+(table1[i][j-1]=='*')+(table1[i][j+1]=='*')+(table1[i-1][j-1]=='*')+(table1[i+1][j+1]=='*')+(table1[i-1][j+1]=='*')+(table1[i+1][j-1]=='*');
    			}
    		}
    	}
    	for(int i=1;i<=n;++i)
    	{
    		for(int j=1;j<=m;++j)
    		{
    			if(table2[i][j]==-1)cout<<"*";
    			else  cout<<table2[i][j];
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    15:细菌的繁殖与扩散

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #define MAX 105
    using namespace std;
    int table[MAX][MAX]={};
    int table2[MAX][MAX]={};
    int dx[]={1,1,1,0,0,-1,-1,-1}; //从上到下 
    int dy[]={-1,0,1,-1,1,-1,0,1}; //从上到下
    int main()
    {
    	int n,m;
    	cin>>table[5][5]>>m;
    
    	for(int i=1;i<=m;++i)
    	{
    		for(int j=1;j<=9;++j)
    		{
    			for(int k=1;k<=9;++k)
    			{	
    				table2[j][k]+=table[j][k]*2;	
    				for(int s=0;s<8;++s)
    				{	
    					int x=j+dx[s],y=k+dy[s];
    					table2[x][y]+=table[j][k];
    				}
    			}
    		}
    		memcpy(table,table2,sizeof(table2)); 
    		memset(table2,0,sizeof(table2)); 
    	}
    	for(int j=1;j<=9;++j)
    	{
    		for(int k=1;k<=9;++k)
    		{
    			if(k==1) cout<<table[j][k];
    			else  cout<<" "<<table[j][k];
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    16:矩阵剪刀石头布

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #define MAX 105
    using namespace std;
    char table[MAX][MAX];
    char table2[MAX][MAX];
    int dx[]={-1,1,0,0}; //从上到下 
    int dy[]={0,0,-1,1}; //从上到下
    char f(char a,char b)
    {
    	if(a=='R'&&b=='S'||a=='S'&&b=='P'||a=='P'&&b=='R') return a;
    	else return 'N';
    }
    int main()
    {
    	int r,c,n;
    	cin>>r>>c>>n;
    	for(int i=1;i<=r;++i)for(int j=1;j<=c;++j)cin>>table[i][j];
    	memcpy(table2,table,sizeof(table));
    	for(int i=0;i<n;++i)
    	{
    		for(int j=1;j<=r;++j)
    		{
    			for(int k=1;k<=c;++k)
    			{
    				for(int s=0;s<4;++s)
    				{
    					int x=j+dx[s],y=k+dy[s];
    					char r=f(table[j][k],table[x][y]);
    					if(r!='N') table2[x][y]=r;				
    				}		
    			}
    		}
    		memcpy(table,table2,sizeof(table2));
    	} 
    	for(int i=1;i<=r;++i)
    	{
    		for(int j=1;j<=c;++j)
    		{
    			cout<<table[i][j];
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    17:最好的草

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #define MAX 105
    using namespace std;
    char table[MAX][MAX];
    int dx[]={-1,1,0,0}; //从上到下 
    int dy[]={0,0,-1,1}; //从上到下
    int main()
    {
    	int r,c,n,conut=0;
    	cin>>r>>c;
    	for(int i=1;i<=r;++i)for(int j=1;j<=c;++j) cin>>table[i][j];
    	for(int j=1;j<=r;++j)
    		for(int k=1;k<=c;++k)
    		{
    			if(table[j][k]=='#')
    			{
    				conut++;
    				for(int s=0;s<4;++s)
    				{
    					int x=j+dx[s],y=k+dy[s];
    					if(table[x][y]=='#') table[x][y]='.';
    				}	
    			}
    		}
    	cout<<conut;
    	return 0;	
    }
    

    18:肿瘤面积【算面积,还差两个点老是wa,所以贴一份dalao的ac代码,写的相当清晰】

    #include <iostream>
     
    using namespace std;
     
    int main()
    {
        int n, low, high,x1 ,y1,x2,y2,sum;
        cin>>n;
        int i=0;
        while(i<n*n){
            int num;
            cin>>num;
            if(num==0){
                low=i;
                i++;
                break;
            }
            i++;
        }
        while(i<n*n){
            int num;
            cin>>num;
            if(num==0){
                high=i;
            }
            i++;
        }
     
        x1=low%n;y1=(low+n)/n;
        x2=high%n;y2=(high+n)/n;
        sum=(y2-y1-1)*(x2-x1-1);
        cout<<sum<<endl;
        return 0;
    }
    

    19:肿瘤检测

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #define MAX 1004
    using namespace std;
    int table[MAX][MAX];
    int n;
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,-1,1};
    int main()
    {
    	int s=0,c=0;
    	cin>>n;
    	for(int i=1;i<=n;++i)
    		for(int j=1;j<=n;++j)
    		{
    			cin>>table[i][j];
    			if(table[i][j]<=50)s++; 
    		 } 
    	for(int j=1;j<=n;++j)
    	{
    		for(int k=1;k<=n;++k)
    		{
    			if(table[j][k]<=50)
    			{
    				if(j==1||k==1||j==n||k==n)
    				{
    					c++;	
    				}
    				else
    				{
    					for(int i=0;i<4;++i)
    					{
    						int x=dx[i]+j,y=dy[i]+k;
    						if(table[x][y]>50)
    						{
    							c++;
    							break;
    						 } 
    					}
    				}
    
    			}
    		}
    	}	
    	cout<<s<<" "<<c;	
    	return 0;	
    }
    

    20:反反复复

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #define MAX 1004
    using namespace std;
    char table[MAX][MAX];
    int n;
    int main()
    {
    	int s=0,c=0,ii=0;
    	string str="";
    	cin>>n>>str;
    	int len=str.length();
    	len = len/n;
    	for(int i=0;i<len;++i)
    	{
    		if(i%2==1)
    		{
    			for(int j=n-1;j>=0;--j)
    			{
    				table[i][j] = str[ii++];
    			}
    		}
    		else
    		{
    			for(int j=0;j<n;++j)
    			{
    				table[i][j] = str[ii++];
    			}	
    		}
    	}
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<len;++j)
    		{
    			cout<<table[j][i];
    		}
    	}
    	return 0;	
    }
    

     

    21:二维数组右上左下遍历

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #include<algorithm>
    #define MAX 1004
    using namespace std;
    int table[MAX][MAX];
    int n;
    int main()
    {
    	int row,col,solt=0;
    	cin>>row>>col;
    	for(int i=0;i<row;++i)
    		for(int j=0;j<col;++j)
    			cin>>table[i][j];
    	for(int k=0;k<=row+col;++k)
    		for(int i=0;i<row;++i)
    			for(int j=0;j<col;++j)
    			{
    				if(i+j==k)cout<<table[i][j]<<endl;
    			 } 
    	return 0;	
    }
    

    22:神奇的幻方

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #include<algorithm>
    #define MAX 1004
    using namespace std;
    int t[MAX][MAX];
    int main()
    {
    	int n;	cin>>n;
    	int m=(2*n-1);
    	int x=1,y=n;
    	t[x][y]=1; //第一个数字写在第一行的中间
    	for(int i=2;i<=m*m;++i)
    	{
    		if(x==1&&y==m||t[x-1][y+1]) x+=1;//c
    		else if(x==1) x=m,y+=1;//a
    		else if(y==m) x-=1,y=1;//b
    		else x-=1,y+=1;//下一个数字,都写在上一个数字的右上方
    		t[x][y]=i;		
    	}
    	for(int i=1;i<=m;++i)
    	{
    		for(int j=1;j<=m;++j)
    		{
    			cout<<t[i][j]<<" ";
    		} 
    		cout<<endl;
    	}
    	return 0;	
    }
    

    23:二维数组回形遍历

    【没什么技术含量就是麻烦的要死,辛辛苦苦写了个递归然后爆掉了,果断该循环】

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #include<algorithm>
    #define MAX 1004
    using namespace std;
    int t[MAX][MAX];
    int v[MAX][MAX];
    int main()
    {
    	int srow=0,scol=0,row,col;cin>>row>>col;
    	for(int i=0;i<row;++i)for(int j=0;j<col;++j)cin>>t[i][j];
    	int si=0,sj=0;
    	for(int i=1;i<=row*col;++i,++srow,++scol,--row,--col)
    	{
    		for(int i=scol;i<col&&!v[srow][i];++i)
    		{
    			cout<<t[srow][i]<<endl;
    			v[srow][i]=1;
    		}
    		for(int i=srow+1;i<row&&!v[i][col-1];++i)
    		{
    			cout<<t[i][col-1]<<endl;
    			v[i][col-1]=1;
    		}
    		for(int i=col-2;i>scol&&!v[row-1][i];--i)
    		{
    			cout<<t[row-1][i]<<endl;
    			v[row-1][i]=1;
    		}
    		for(int i=row-1;i>srow&&!v[i][scol];--i)
    		{
    			cout<<t[i][scol]<<endl;
    			v[i][scol]=1;
    		}
    	}
    	return 0;	
    }
    

     测试数据

    4 5
    1 2 3 4 11
    12 13 14 5 111
    11 16 15 6 1111
    10 9 8 7 11111
    
    5 4
    1 2 3 4
    12 13 14 5
    11 16 15 6
    10 9 8 7
    17 18 19 20
    
    
    1 2
    1 2
    
    4 4
    1 2 3 4 
    12 13 14 5 
    11 16 15 6
    10 9 8 7
    
    1 1
    1
    View Code

    24:蛇形填充数组

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #include<algorithm>
    #define MAX 1004
    using namespace std;
    int t[MAX][MAX];
    int v[MAX][MAX];
    int main()
    {
    	int n,num=1;cin>>n;
    	for(int k=0;k<=2*n;++k)
    	{
    		if(k%2)
    		{
    			for(int i=0;i<n;++i)
    			{
    				for(int j=0;j<n;++j)
    				{
    					if(i+j==k) t[i][j]=num++;
    				}
    			}
    		}
    		else
    		{
    			for(int i=n-1;i>=0;--i)
    			{
    				for(int j=0;j<n;++j)
    				{
    					if(i+j==k) t[i][j]=num++;
    				}
    			}	
    		}
    	}
    	for(int i=0;i<n;++i)
    	{
    		for(int j=0;j<n;++j)
    		{
    			cout<<t[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    	return 0;	
    }
    

    25:螺旋加密

    #include<iostream>
    #include<string> 
    #include<cmath>
    #include<cstring> 
    #include<algorithm>
    #define MAX 1004
    using namespace std;
    int t[MAX][MAX];
    int v[MAX][MAX];
    string table[27]={"00000","00001","00010","00011","00100",
        "00101","00110","00111","01000","01001","01010","01011",
        "01100","01101","01110","01111","10000","10001","10010",
        "10011","10100","10101","10110","10111","11000","11001","11010"};
    string MakeCode(string str,int len)
    {
    	string re="";
    	for(int i=0;i<str.length();++i)
    	{
    		for(int j=0;j<27;++j)
    		{
    			if(str[i]==' ') 
    			{
    				re+=table[0];break;
    			}
    			else
    			{
    				re+=table[str[i]-'A'+1]; break;
    			} 
    		}
    	}
    	return re;
    }
    int main()
    {
    	string str;
    	int srow=0,scol=0,row,col,ii=0;
    	cin>>row>>col;getchar();getline(cin,str);
    	int r=row,c=col;
    	string num=MakeCode(str,row*col);
    
    	for(int ii=0;ii<num.length();++srow,++scol,--row,--col)
    	{
    		for(int i=scol;i<=col-1&&!v[srow][i]&&ii<num.length();++i,++ii)
    		{
    		    t[srow][i]=num[ii]-'0';
    			v[srow][i]=1;
    		}
    		for(int i=srow+1;i<=row-1&&!v[i][col-1]&&ii<num.length();++i,++ii)
    		{
    			t[i][col-1]=num[ii]-'0';
    			v[i][col-1]=1;
    		}
    		for(int i=col-2;i>=scol&&!v[row-1][i]&&ii<num.length();--i,++ii)
    		{
    			t[row-1][i]=num[ii]-'0';
    			v[row-1][i]=1;
    		}
    		for(int i=row-2;i>=srow+1&&!v[i][scol]&&ii<num.length();--i,++ii)
    		{
    			t[i][scol]=num[ii]-'0';
    			v[i][scol]=1;
    		}
    	}
    	for(int i=0;i<r;++i)
    		for(int j=0;j<c;++j)
    			cout<<t[i][j]; 
    	return 0;	
    }
  • 相关阅读:
    == 和equals方法
    ObjectInputStream 与ObjectOutputStream
    IOS基础:ObjectiveC 数组处理
    学习笔记:自定义方法的两种实现方式
    DatePicker 获取时间的时区问题
    IOS基础:tableview中cell
    IOS基础:窗口切换的几种方法
    IOS基础:ObjectiveC 字符串处理
    使用 Notifications
    学习笔记:Tab Bar 控件使用详解
  • 原文地址:https://www.cnblogs.com/chrysanthemum/p/12246399.html
Copyright © 2020-2023  润新知