北航上机试题2011(题目+代码)

https://apriljia.com/2018/07/16/%E5%8C%97%E8%88%AA%E4%B8%8A%E6%9C%BA%E8%AF%95%E9%A2%982011%EF%BC%88%E9%A2%98%E7%9B%AE%E4%BB%A3%E7%A0%81%EF%BC%89/

1. 孪生数定义: 如果 A 的约数(因数,包含1,但不包含A本身)之和等于 B , B 的约数(因数)之和等于 A , A 和 B 称为孪生数(A和B不相等)。试找出正整数 M 和 N 之间的孪生数。


输入:
从控制台输入两个正整数M和N(1<=M<N<=20000),中间用一个空格分隔。

输出:

在标准输出上输出符合题目描述的M和N之间的全部孪生数对(包括M和N)。每行输出一对孪生数,用一个空格隔开,小的先输出;各行孪生数按照第一个数从小到大的顺序输出,一对孪生数只输出一次。 如果没有符合要求的孪生数对,则输出字符串“NONE”。

输入样例
20 300

200 250

输出样例
220 284

NONE

#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

int yueshuhe(int n){
	int sum=0;
	for(int i=1;i<n;i++){
		if(n%i==0) sum+=i;
	}
	return sum;
}
int main(int argc, char* argv[])
{	 
	int start,end;
	int i,j;
	int ysh;
	int flag=0;
	scanf("%d %d",&start,&end);
	for(i=start;i<=end;i++){
		ysh=yueshuhe(i);
		if(ysh>i&&ysh<=end&&yueshuhe(ysh)==i){
			printf("%d %d",i,ysh);
			flag=1;
		}
	}
	if(flag==0) printf("NONE");
	return 0;
}
2. 先输入两个矩阵A和B,然后输入替换位置(左上角),编写程序将矩阵A中从替换位置开始的子矩阵(与B同样大小)替换为B,并输出替换后的矩阵。


【输入形式】


从控制台先输入矩阵A的行数和列数(行数和列数均大于等于1,小于等于20),然后在新的行上输入矩阵A的各行数字(以一个空格分隔的整数)。再以同样的方式输入矩阵B。最后输入替换位置(用一个空格分隔的两个整数表示,行数和列数都从1开始计数,因此两个整数都大于等于1)。若替换位置超出了矩阵A的行数或列数,则原样输出矩阵A。


【输出形式】


在标准输出上分行输出替换后的矩阵,每行中各数字之间以一个空格分隔。


【输入样例1】

5 6

10 2 34 -1 800 90

2 76 56 -200 23 1

35 0 0 98 8 3000

2000 100 -1 1 2 0

8 7 85 963 496 8

2 3

9 9 9


9 9 9

3 3

【输出样例1】

10 2 34 -1 800 90

2 76 56 -200 23 1

35 0 9 9 9 3000

2000 100 9 9 9 0

8 7 85 963 496 8

#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
	int m1[40][40];
	int m2[40][40];
	int l1,h1;
	int l2,h2;
	int i,j;
	int t1,t2;

	scanf("%d %d",&l1,&h1);
	for(i=0;i<l1;i++)
		for(j=0;j<h1;j++)
			scanf("%d",&m1[i][j]);
	
	scanf("%d %d",&l2,&h2);
	for(i=0;i<l2;i++)
		for(j=0;j<h2;j++)
			scanf("%d",&m2[i][j]);


	scanf("%d %d",&t1,&t2);
	t1--;t2--;
	for(i=0;i<l2;i++){
		for(j=0;j<h2;j++){
			m1[i+t1][j+t2]=m2[i][j];
		}
	}

	for(i=0;i<l1;i++){
		for(j=0;j<h1;j++){
			printf("%d ",m1[i][j]);
		}
		printf("\n");
	}
	return 0;
}
3. 从键盘输入包含扩展符'-'的字符串,将其扩展为等价的完整字符,例如将a-d扩展为abcd,并输出扩展后的字符串。

要求:只处理[a-z]、[A-Z]、[0-9]范围内的字符扩展,即只有当扩展符前后的字符同时是小写字母、大写字母或数字时才进行扩展,其它情况不进行扩展,原样输出。例如:a-R、D-e、0-b、4-B等字符串都不进行扩展。

【输入形式】

从键盘输入包含扩展符的字符串

【输出形式】

输出扩展后的字符串

【输入样例1】

ADEa-g-m02

【输出样例1】

ADEabcdefghijklm02

【输入样例2】

cdeT-bcd

【输出样例2】

cdeT-bcd

【样例说明】

将样例1的输入ADEa-g-m02扩展为:ADEabcdefghijklm02;样例2的输入cdeT-bcd中,扩展符前的字符为大写字母,扩展符后的字符为小写字母,不在同一范围内,所以不进行扩展。

#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
	char str1[200];
	char str2[200];
	int i,j;
	int index=0;

	gets(str1);
	int len=strlen(str1);
	str2[index++]=str1[0];
	for(i=1;i<len-1;i++){
		if(str1[i]=='-'){
			if(isdigit(str1[i-1])&&isdigit(str1[i+1])&&str1[i-1]<str1[i+1]){
				for(j=str1[i-1]+1;j<str1[i+1];j++){
					str2[index++]=j;
				}
			}	
		else if(islower(str1[i-1])&&islower(str1[i+1])&&str1[i-1]<str1[i+1]){
				for(j=str1[i-1]+1;j<str1[i+1];j++){
					str2[index++]=j;
				}
			}
		else if(isupper(str1[i-1])&&isupper(str1[i+1])&&str1[i-1]<str1[i+1]){
				for(j=str1[i-1]+1;j<str1[i+1];j++){
					str2[index++]=j;
				}
		}else{

			str2[index++]=str1[i];

		}


		}else{
			str2[index++]=str1[i];
		}
	}
	//if(i<len-1)
		str2[index++]=str1[len-1];
	str2[index]=0;

	cout<<str2;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mingzhiqing/article/details/81063050
今日推荐