【算法题】算法题之复试常考篇

复试上机题 [C语言描述]

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

2. 对输入的 n 个数进行排序并输出。

题目描述

求整数 a,b 的和。

假如输入

/*
输入的第一行包括一个整数 n(1<=n<=100)。接下来的一行包括 n 个整数。 
*/
4 
1 4 3 2

应当输出

/*
可能有多组测试数据,对于每组数据,将排序后的 n 个整数输出,每个数后
面都有一个空格。每组测试数据的结果占一行。 
*/
1 2 3 4
#include<stdio.h>
//冒泡排序
 void bublesort(int a[],int n){
     int temp=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n-1-i;j++){
            if(a[j]>a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }

    for(int i=0;i<n;i++){
        printf("%d ",a[i]);
    }
 }

 int main(){
    int n=0;
    int a[101];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        bublesort(a,n);
    }
 }


/*-----------------------------------------------------------------------*/
//第二种方法
#include<stdio.h>
#include<algorithm>
using namespace std;

bool cmp(int a,int b){
	return a<b
}
 void arraysort(int a[],int n){
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        printf("%d ",a[i]);
    }

 }

 int main(){
    int n=0;
    int a[101];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        arraysort(a,n);
    }
 }


3. 成绩排序

题目描述

有 N 个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名
字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出 N
个学生排序后的信息。

假如输入

/*
测试数据有多组,每组输入第一行有一个整数 N(N<=1000),接下来的 N
行包括 N 个学生的数据。每个学生的数据包括姓名(长度不超过 100 的字符串)、
年龄(整形数)、成绩(小于等于 100 的正数)。
*/
3 
abc 20 99 
bcd 19 97 
bed 20 97

应当输出

/*
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。然后
输出学生信息,按照如下格式:姓名 年龄 成绩
提示:学生姓名的字母序区分字母的大小写,如 A 要比 a 的字母序靠前(因为 A 的
ASC 码比 a 的 ASC 码要小)。 
*/
bcd 19 97 
bed 20 97 
abc 20 99
#include<stdio.h>
#include<algorithm>
#include<string.h>

using namespace std; 

struct E{
	char name[101];
	int age;
	int score;
}buf[100];

bool cmp(E a,E b){
	if(a.score!=b.score)
		return a.score<b.score;
	int tmp = strcmp(a.name,b.name);
	if(tmp!=0) return tmp<0;
	else
		return a.age<b.age;	
}

int main()
{
	int n=0;
	while(scanf("%d",&n)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%s%d%d",&buf[i].name,&buf[i].age,&buf[i].score);
		}
		sort(buf,buf+n,cmp);
		for(int i=0;i<n;i++)
			printf("%s %d %d 
",buf[i].name,buf[i].age,buf[i].score);
		
	}
	return 0;
}

4. 日期差值

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们
之间的天数为两天

假如输入

/*
测试数据有多组,每组输入第一行有一个整数 N(N<=1000),接下来的 N
行包括 N 个学生的数据。每个学生的数据包括姓名(长度不超过 100 的字符串)、
年龄(整形数)、成绩(小于等于 100 的正数)。 
*/
20110412 
20110422 

应当输出

/*
有多组数据,每组数据有两行,分别表示两个日期,形式为 YYYYMMDD
*/
11
#include<stdio.h>
#define IsYear(x) x%100!=0&&x%4==0 || x%400==0?1:0

int dayofMonth[13][2]={
	0,0,
	31,31,
	29,28,
	31,31,
	30,30,
	31,31,
	30,30,
	31,31,
	31,31,
	30,30,
	31,31,
	30,30,
	31,31,
};

struct Data{
	int year;
	int month;
	int day;
	void nextDay(){
		day++;
		if(day>dayofMonth[month][IsYear(year)]){
			day=1;
			month++;
		}
		if(month>12){
			month=1;
			year++;
		}
	}
};

int buf[5001][13][32];
int abs(int x)
{
	return x>=0?x:-x; 
}


int main(){
	Data temp;
	temp.year=0;
	temp.month = 1;
	temp.day = 1;
	int count=0;
	while(temp.year<5001){
		buf[temp.year][temp.month][temp.day] =count;
		temp.nextDay();
		count++;
	}
	int y1,y2;
	int d1,d2;
	int m1,m2;
	int dis;
	while(scanf("%4d%2d%2d",&y1,&m1,&d1)!=EOF){
		scanf("%4d%2d%2d",&y2,&m2,&d2);
		dis = abs(buf[y1][m1][d1]-buf[y2][m2][d2])+1;
		printf("%d
",dis);
	}
	return 0;
}

5. 统计同成绩学生人数

题目描述

读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。

假如输入

/*
测试输入包含若干测试用例,每个测试用例的格式为 
第 1 行:N 
第 2 行:N 名学生的成绩,相邻两数字用一个空格间隔。 
第 3 行:给定分数 
当读到 N=0 时输入结束。其中 N 不超过 1000,成绩分数为(包含)0 到 100
之间的一个整数。 
*/
3 
80 60 90 
60 
2 
85 66 
0 
5 
60 75 90 55 75 
75 
0

应当输出

/*
 对每个测试用例,将获得给定分数的学生人数输出。
*/
1 
0 
2
#include<stdio.h>
int main(){
	int n=0;
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		int hash[101]={
			0,
		};
		int x;
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			hash[x]++;
		}
		scanf("%d",&x);
		printf("%d",hash[x]);	
	}
	return 0;
}

6. Sort

题目描述

给你 n 个整数,请按从大到小的顺序输出其中前 m 大的数。

假如输入

/*
每组测试数据有两行,第一行有两个数 n,m(0<n,m<1000000),第二行包含 n
个各不相同,且都处于区间[-500000,500000]的整数。 
*/
5 3 
3 -35 92 213 -644

应当输出

/*
 对每组测试数据按从大到小的顺序输出前 m 大的数。
*/
213 92 3

7. 输出梯形

题目描述

输入一个高度 h,输出一个高为 h,上底边为 h 的梯形。

假如输入

/*
一个整数 h(1<=h<=1000)。
*/
4

应当输出

/*
 h 所对应的梯形。
*/
      **** 
    ****** 
  ******** 
********** 

8. 叠筐

题目描述

把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个
工作现在要让计算机来完成,得看你的了。

假如输入

/*
输入是一个个的三元组,分别是,外筐尺寸 n(n 为满足 0<n<80 的奇整数),
中心花色字符,外筐花色字符,后二者都为 ASCII 可见字符;
*/
11 B A 
5 @ W

应当输出

/*
 输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐
相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。
*/
 AAAAAAAAA 
ABBBBBBBBBA 
ABAAAAAAABA 
ABABBBBBABA 
ABABAAABABA 
ABABABABABA 
ABABAAABABA 
ABABBBBBABA 
ABAAAAAAABA
ABBBBBBBBBA 
 AAAAAAAAA 

 

 @@@ 
@WWW@ 
@W@W@ 
@WWW@ 
 @@@ 

9. 找 x

题目描述

输入一个数 n,然后输入 n 个数值各不相同,再输入一个值 x,输出这个值在这个数组中的下标(从 0 开始,若不在数组中则输出-1)。

假如输入

/*
测试数据有多组,输入 n(1<=n<=200),接着输入 n 个数,然后输入 x。
*/
2 
1 3 
0

应当输出

/*
 对于每组输入,请输出结果。
*/
-1

10. 查找学生信息

题目描述

输入 N 个学生的信息,然后进行查询。

假如输入

/*
输入的第一行为 N,即学生的个数(N<=1000) 
接下来的 N 行包括 N 个学生的信息,信息格式如下: 
01 李江 男 21 
02 刘唐 男 23 
03 张军 男 19 
04 王娜 女 19 
然后输入一个 M(M<=10000),接下来会有 M 行,代表 M 次查询,每行输入
一个学号,格式如下: 
02 
03 
01 
04
*/
4 
01 李江 男 21 
02 刘唐 男 23 
03 张军 男 19 
04 王娜 女 19 
5 
02 
03 
01 
04 
03

应当输出

/*
 输出 M 行,每行包括一个对应于查询的学生的信息。 
如果没有对应的学生信息,则输出“No Answer!” 
*/
02 刘唐 男 23 
03 张军 男 19 
01 李江 男 21 
04 王娜 女 19 
03 张军 男 19

11. 字符串链接函数MyStrcat

题目描述

不用strcat 函数,自己编写一个字符串链接函数MyStrcat(char dstStr[],charsrcStr[])

假如输入

/*
两个字符串,字符串由小写字母组成。
*/
hello world
good morning

应当输出

/*
链接后的字符串 
*/
helloworld
goodmorning
#include<stdio.h>
#include<string.h>
void MyStrcat(char dstStr[],char srcStr[]){
    int len1,len2;
    len1 = strlen(dstStr);
    len2 = strlen(srcStr);
    for(int i=0;i<len2;i++){
        dstStr[len1++] = srcStr[i];
    }
    dstStr[len1] = '';
}

 int main(){
    char s1[101],s2[101];
    while(scanf("%s%s",s1,s2)!=EOF){
        MyStrcat(s1,s2);
        printf("%s
",s1);
    }
    return 0;
 }

12. 换钱计划

题目描述

一个百万富翁遇到一个陌生人,陌生人找他谈了一个换钱的计划。该计划如下:我每天给你10 万元,你第一天给我1 分钱,第二天2 分钱,
第三天4 分钱……
这样交换 30 天后,百万富翁交出了多少钱?陌生人交出了多少钱?(注意一个是万元,一个是分)

假如输入

/*
该题没有输入
*/

应当输出

/*
输出两个整数,分别代表百万富翁交出的钱和陌生人交出的钱,富翁交出的钱以万元作单位,陌生人交出的钱以分作单位。
*/
300 1073741823
#include<stdio.h>
#include<math.h>

void exchangemoney(){
    int a=0,b=0;
    for(int i=0;i<30;i++){
        a+=10;
        b+=pow(2,i);
    }
    printf("%d %d
",a,b);

}

 int main(){
    exchangemoney();
    return 0;
 }

13. 逆置字符串

题目描述

输入一个字符串,长度小于等于200,然后将数组逆置输出。

假如输入

/*
测试数据有多组,每组输入一个字符串。
*/
hdssg

应当输出

/*
对于每组输入,请输出逆置后的结果。 
*/
gssdh
#include<stdio.h>
#include<string.h>

void reversestring(char s[]){
    char *p;
    p = s+strlen(s)-1;
    for(int i=0;i<strlen(s);i++)
        printf("%c",*(p--));
    printf("
");
}

 int main(){
     char str[201];
     while(scanf("%s",str)!=EOF){
        reversestring(str);
     }
    return 0;
 }

14. 大数加和

题目描述

给定a和n,计算a+aa+aaa+a...a(n个a)的和。

假如输入

/*
测试数据有多组,输入a,n(1<=a<=9,1<=n<=100)。
*/
1 10

应当输出

/*
对于每组输入,请输出结果。 
*/
1234567900
/*
这段程序在输入 1 11时出错;错误原因是a超出了int型的最大范围。
要找另外的解决办法。
*/
#include<stdio.h>
#include<string.h>
//1+11+111
int calsum(int a,int n){
    int sum=0;
    int temp = a;
    for(int i=0;i<n;i++){
        sum += a;
        a = a*10+temp;
        //printf("%d",a);
    }
    return sum;
}

 int main(){
     int a=0,n=0;
     int sum=0;
    while(scanf("%d%d",&a,&n)!=EOF){
        sum = calsum(a,n);
        printf("%d
",sum);
    }
    return 0;
 }

15. 字符串排序

题目描述

输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。

假如输入

/*
测试数据有多组,输入字符串。
*/
bacd

应当输出

/*
对于每组输入,输出处理后的结果。
*/
abcd
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

void strsort(char s[]){
    int len = strlen(s);
    sort(s,s+len);
    printf("%s
",s);
}

 int main(){
    char s1[201],s2[201];
    while(scanf("%s",s1)!=EOF){
        strsort(s1);
    }
    return 0;
 }

16. 10个数里的最大值

题目描述

测试数据有多组,每组10个整数。

假如输入

/*
测试数据有多组,每组10个整数。
*/
10 22 23 152 65 79 85 96 32 1

应当输出

/*
对于每组输入,请输出其最大值(有回车)。 
*/
max=152
#include<stdio.h>
void findmax(int a[]){
    int max = a[0];
    for(int i=0;i<10;i++)
    {
        if(a[i]>max)
            max = a[i];
    }
    printf("max=%d
",max);
}

 int main(){
    int a[10];
    for(int i=0;i<10;i++){
        scanf("%d",&a[i]);
    }
    findmax(a);
    return 0;
 }

17. 去掉s中所有的c字符

题目描述

输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。。

假如输入

/*
测试数据有多组,每组输入字符串s和字符c。
*/
heallo
a

应当输出

/*
对于每组输入,输出去除c字符后的结果。 
*/
hello
#include<stdio.h>
#include<string.h>

void handlestr(char s[],char x){
    int len = strlen(s);
    int j = 0;
    char p[101];
    for(int i=0;i<len;i++){
        if(s[i]!=x)
            p[j++] = s[i];
    }
    printf("%s
",p);
}

 int main(){
    char s[101];
    char c;
    while(scanf("%s %c",s,&c)!=EOF){
        handlestr(s,c);
    }
    return 0;
 }

18. 指定矩阵相乘

题目描述

输入为两个矩阵,其中一个为2x3的矩阵,另一个为3x2的矩阵

假如输入

/*
输入为两个矩阵,其中一个为2*3的矩阵,另一个为3*2的矩阵
*/
1 2 3
3 4 5
6 7
8 9
10 11

应当输出

/*
一个2*2的矩阵(每一个数字后都跟一个空格) 
*/
52 58
100 112
#include<stdio.h>
void matrix_muti(int a[][3],int b[][2]){
    int c[2][2]={0};
    for(int i=0; i<2;i++)
        for(int j=0; j<2;j++)
            for(int k=0; k<3;k++)
                c[i][j]+=a[i][k]*b[k][j];

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++)
            printf("%d ",c[i][j]);
        printf("
");
    }
}

 int main(){
    int a[2][3],b[3][2];
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            scanf("%d",&a[i][j]);
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++)
            scanf("%d",&b[i][j]);
    matrix_muti(a,b);
    return 0;
 }
/*———————————————————————————————————————————————————————————————————————————————————*/
/*
上题为固定大小的矩阵相乘,条件给出了具体的维度,代码的扩展性不够。
以下代码为通用的矩阵相乘代码
*/

19. 计算三角形的边

题目描述

给出三个正整数,计算最小的数加上次小的数与最大的数之差。

假如输入

/*
每一行包括三个数据a, b, c,并且都是正整数,均小于10000。
*/
1 2 3
6 5 4
10 20 15
1 1 100
0 0 0

应当输出

/*
对于输入的每一行,在单独一行内输出结果s。s=min(a,b,c)+mid(a,b,c)-max(a,b,c)。上式中,min为最小值,mid为中间值,max为最大值。 
*/
0
3
5
-98
#include<stdio.h>
#include<algorithm>
using namespace std;

int handdata(int a,int b,int c){
    int x[3] = {a,b,c};
    sort(x,x+3);
    int result = x[0]+x[1]-x[2];
    return result;
}
int main()
{
    int a,b,c;
    int result=0;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF){
        result = handdata(a,b,c);
        printf("%d
",result);
    }
    return 0;
}

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

1. 计算 A+B

题目描述

求整数 a,b 的和。

假如输入

/*
测试案例有多行,每行为 a,b 的值,a,b 为 int 范围。
输出多行,对应 a+b 的结果。
*/
1 2 
4 5 
6 9

应当输出

/*
 
*/
3 
9 
15

原文地址:https://www.cnblogs.com/lwp-nicol/p/14260611.html