ECNU-2018计算机学院考研机试

ECNU 2018计算机考研上机测试

1.庙会

不需要一轮一轮的模拟,直接取余输出即可。代码如下:

#include<cstdio>
#include<iostream>

using namespace std;

int main(){
 int n,m,round;
 cin >> n >> m >> round;
 for(int i= 1;i<= round;i++){
  if(i%n==0) cout << n;
  else cout << (i%n);
  
  if(i%m==0) cout <<" "<< m;
  else cout << " "<<(i%m);
  cout << endl;
 }
 return 0;
}

2.热河路

  • step1.找出序列中1出现的位置,可知1出现的下标为1,2,4,7,11……。
  • step2.按照这个顺序生成109范围内的一个序列
  • step3.针对输入数据测试是否在生成的序列中,如果在,则输出1,否则输出0.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int maxn=1e9+5;

int N;
bool a[maxn]={false};
void begin()
{
	//memset(a,0,sizeof(a));
	int cnt=0;
	for(int i=1;i<maxn;i+=cnt)
	{
		a[i]=true;
		cnt++;
	}
}

int main()
{
	begin();
	while(scanf("%d",&N)!=EOF)
	{
		for(int i=0;i<N;i++)
		{
			int x;
			scanf("%d",&x);
		    printf("%d\n",a[x]);
		}
	}
	return 0;
}

有一点需要注意。

  • 1.可以使用bool型数组,因为bool型数组只占一个字节。而不能使用其他类型的(例如int型的数组)

还有一种解法,但是我不大理解其原理,【也是可以通过AC的】代码如下:

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;

int main(){
 int N;
 scanf("%d",&N);
 while(N--){
  int num;
  scanf("%d",&num);
  if(num==1){
   printf("1\n");
   continue;
  }
  num--;
  int i=sqrt(num<< 1);
  if((i*(i+1))>> 1== num){
   printf("1\n");
  }
  else{
   printf("0\n");
  }
 }
 return 0;
}

这里面貌似涉及到了数学的知识,不是很理解 以后再看吧 。【其实就是针对该数值利用方程的知识看是否能够反解出一个正整数,如果可以,则输出1,否则输出0.】

3.定西

#include<cstdio>
#include<iostream>

using namespace std;
const int maxn= 105;
bool arr[maxn];
int count = 0;
int n;

void go(int cur,int step){//count代表当前所在台阶,step代表每次的步长
 cur += step;
 if(!arr[cur]){return;}//说明此阶台阶不可上
 if(cur > n ) return;
 if(cur == n ) {//说明已经上完了所有台阶
  count++; 
  return ;
 }
 else {
  go(cur,1);
  go(cur,2);
  go(cur,3);
 }
}


int main(){
 fill(arr,arr+100,true);
 int k;
 cin >> n >> k;
// cout << n <<" " << k;

 int num;
 for(int i = 1;i<=k ;i++){
  cin >> num;
  arr[num] = false;
 }
 go(0,0);
 cout << count<<endl;
 return 0;
}

4.和你在一起

按照字符串的顺序sort即可

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int cmp(string a,string b){
 if(a<b) return 0;
 else return 1;
}

int main(){
 int n;
 cin >> n;
 string str[20];
 for(int i = 0;i< n;i++){
  cin >> str[i];
  
 }
 sort(str,str+n,cmp);
 for(int i = 0;i< n;i++)
  cout << str[i];
 cout << endl;
 return 0;
}

其实这么写是不完全正确的,对于

2
2713 27

这种测试用例,本应该输出272713,而不是271327。但是oj的测试用例好像没有考虑到这一点。如果要是修改成完美的程序,只需要将cmp函数的比较过程修改一下即可。

5.梵高先生

  • 用二维数组存储(注意到n最大值为20)
  • 当前的数=与其肩膀上(正上方的数与正上方左边的数)的和相等。
    根据上述内容,可确定整个二维述祖
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int maxn=21;

int main(){
 int n;
 cin >> n;
 int arr[maxn][maxn];
 fill(arr[0],arr[0]+maxn*maxn,0);//赋初值0
 arr[1][1] = 1; 
 for(int i = 2;i <= n ;i++){
  for(int j = 1;j <=i ;j++){//第i行只有i个数
   arr[i][j] = arr[i-1][j-1] + arr[i-1][j];//肩膀上的两个的数值
  }
 }

//输出整个序列
 for(int i = 1;i <= n ;i++){
  for(int j = 1;j <=i ;j++){//第i行只有i个数
    if(arr[i][j]!=0) cout << arr[i][j];
    if(j!=i) cout <<" ";
  }
  cout << endl;
 }
}

6. 西班牙馅饼

题目很简单,不再赘述。

#include<cstdio>
#include<iostream>

using namespace std;
const int maxn = 105;

int main(){
 int n,m;
 cin >> n >> m;
 int arr[maxn][maxn];
 int sum = 0;
 for(int i = 0;i< n;i++){
  int max = 0;
  for(int j = 0;j<m;j++){
   cin >> arr[i][j];
   if(arr[i][j]> max) max = arr[i][j];
  }
  sum+=max;
 }
 cout << sum<< endl;
 return 0;
}
发布了954 篇原创文章 · 获赞 307 · 访问量 112万+

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/103823470