NEFU 大一寒假训练一(二维数组及结构体)2019.12.31

Summary

很久以前的题目,突然发现竟然没发过。
趁大家没发现,赶紧补上,嘿嘿 ヾ(≧▽≦*)o

Information

二维数组:

Problem Title
952 二维矩阵对角线的和
1064 矩阵的外围
955 五人帮
1031 回转小矩阵
954 矩阵相乘

结构体:

Problem Title
1053 结构体应用
1637 身高问题
1638 成绩统计
1186 优秀学生
1147 谁不及格?

Part 1. 二维数组

Problem 952: 二维矩阵对角线和

Tips

简单(shui)题,!难度=1

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int m,a[10][10],tmp,sum;
	while(scanf("%d",&m)!=-1)
	{
		sum=0;
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>a[i][j];
			}
		}
		for(int i=0;i<m;i++)
		{
			sum+=a[i][i];
			sum+=a[m-i-1][i];
		}
		cout<<sum<<endl;
	}
	return 0;
}

Problem 1064: 矩阵的外围

Tips

最外圈加和输出,注意四个角不要重复。

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,m,a[10][10],sum;
	while(scanf("%d %d",&n,&m)!=-1)
	{
		sum=0;
		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+=a[0][i];
			sum+=a[m-1][i];
		}
		for(int i=1;i<m-1;i++)
		{
			sum+=a[i][0];
			sum+=a[i][m-1];
		}
		cout<<sum<<endl;
	}
	return 0;
}

Problem 955: 五人帮

Tips

注意不要越界访问

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,m,a[10][10],tmp,max=0;
	while(scanf("%d %d",&m,&n)!=-1)
	{
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>a[i][j];
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				tmp=a[i][j];
				if(i>0)tmp+=a[i-1][j];
				if(j>0)tmp+=a[i][j-1];
				if(j<n-1)tmp+=a[i][j+1];
				if(i<m-1)tmp+=a[i+1][j];
				if(tmp>max)max=tmp;
			}
		}
		cout<<max<<endl;
	}
	return 0;
}

Problem 1031: 回转小矩阵

Tips

回转小 火锅 矩阵不用真的转,输出方式改一下就行了

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,m,a[10][10],tmp,max=0;
	while(scanf("%d %d",&m,&n)!=-1)
	{
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>a[i][j];
			}
		}
		for(int i=n-1;i>=0;i--)
		{
			for(int j=0;j<m;j++)
			{
				cout<<a[j][i];
				if(j!=m-1)cout<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}

Problem 954: 矩阵相乘

Tips

这道题充分体现了学好线代的重要性

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int m,a[10][10],b[10][10],c[10][10],tmp,sum;
	while(scanf("%d",&m)!=-1)
	{
		sum=0;
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>a[i][j];
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>b[i][j];
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				c[i][j]=0;
				for(int k=0;k<m;k++)
				{
					c[i][j]+=a[i][k]*b[k][j];
				}
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				cout<<c[i][j];
				if(j!=m-1)cout<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}

Part 2. 结构体

Problem 1053: 结构体应用

Tips

注意要读取的名字中间有空格,输出之间需要2个空格。

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int m,tmp,sum;
	char xm[21];
	int xh;
	double grade;
	cin>>m;
	for(int i=0;i<m;i++)
	{
		cin>>xh;
		getchar();
		gets(xm);
		scanf("%lf",&grade);
		printf("%d  %s  %.2lf\n",xh,xm,grade);
	}	
	return 0;
}

Problem 1637: 身高问题

Tips

比较身高并同时保存身高和名字。

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,tall,xh;
	int tallest=0,txh=0;
	char name[100],tname[100];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>name>>tall>>xh;
		if(tall>tallest)
		{
			tallest=tall;
			txh=xh;
			strcpy(tname,name);
		}
		else if(tall==tallest&&xh<txh)
		{
			txh=xh;
			strcpy(tname,name);
		}
	}
	cout <<tname<<" "<<tallest<<" "<<txh;
	return 0;
}

Problem 1638: 成绩统计

Tips

好像没啥难度。。。

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,sum=0,grade,res=0,num=0;;
	char typ,tmp[10];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>typ;
		if(typ=='C')
		{
			gets(tmp);
			res++;
		}
		else
		{
			cin>>grade;
			sum+=grade;
			num++;
		}
	}
	cout <<res<<" "<<sum/num;
	return 0;
}

Problem 1186: 优秀学生

Tips

就不存储,一寸内存一寸金,嘿嘿 φ(゜▽゜*)♪

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,xh,cg,ans;
	char name[21];
	while(cin>>n)
	{
		ans=0;
		for(int i=0;i<n;i++)
		{
			cin>>xh>>name>>cg;
			if(cg>=90)
			{
				ans++;
				cout<<xh<<" "<<name<<" "<<cg<<endl;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Problem 1147: 谁不及格?

Tips

掉进了一个巨坑!!!

第一个k行 输出不及格学生姓名
第二个k行 输出学生学号
第三个k行 输出学生成绩

Code

#include <bits/stdc++.h>
using namespace std;

struct student
{
	char name[21];
	char xh[11];
	double g;
};

int main()
{
	int n,k;
	student s[10];
	while(cin>>n)
	{
		k=0;
		for(int i=0;i<n;i++)
		{
			getchar();
			gets(s[i].name);
			cin>>s[i].xh>>s[i].g;
			if(s[i].g<60)k++;
		}
		if(k)
		{
			cout<<k<<endl;
			for(int i=0;i<n;i++)
			{
				if(s[i].g<60)
				{
					cout<<s[i].name<<endl;
				}
			}
			for(int i=0;i<n;i++)
			{
				if(s[i].g<60)
				{
					cout<<s[i].xh<<endl;
				}
			}
			for(int i=0;i<n;i++)
			{
				if(s[i].g<60)
				{
					printf("%.2f\n",s[i].g);
				}
			}
		}
		else
		{
			cout<<"They are Great!!"<<endl;
		}
	}
	return 0;
}
发布了62 篇原创文章 · 获赞 202 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/csg999/article/details/104282885