2017 NPU computer test questions (C language)

1. Enter two sets of time (hours h, minutes m, seconds s) and calculate the average time. The difference between the two sets of times shall not exceed 1 hour, and h is between 0-11.

Input sample
1 20 30 1 30 30
0 20 30 11 30 30
Output
1 25 30
11 55 30

Reference code:
Method 1:

#include<stdio.h>
int main()
{
    
    
	int a[2][2];
	int i,j;
	int h,m,s;
	for(i=0;i<2;i++)
	{
    
    
		for(j=0;j<2;j++)
		{
    
    
			scanf("%d%d%d",&h,&m,&s);
			if(h==0)
				h=12;
			a[i][j]=h*3600+m*60+s; 
		}
	}
	int sum[2];
	sum[0]=(a[0][0]+a[0][1])/2;
	sum[1]=(a[1][0]+a[1][1])/2;
	for(i=0;i<2;i++)
	{
    
    
		printf("%d %d %d",sum[i]/3600,sum[i]%3600/60,sum[i]%60);
		printf("\n");
	}
}

Method Two:

#include <stdio.h>
int main()
{
    
    
    int h1,h2,m1,m2,s1,s2;
    int h,m,s,sum1,sum2,sum;
    while(~scanf("%d%d%d%d%d%d",&h1,&m1,&s1,&h2,&m2,&s2))
    {
    
    
    	if(h1==0) h1=12;
    	if(h2==0) h2=12;
    	sum1=h1*60*60+m1*60+s1;
    	sum2=h2*60*60+m2*60+s2;
    	sum=sum1+sum2;
    	sum=sum/2;
    	h=sum/3600%12;
    	m=(sum-h*3600)/60;
    	s=sum-h*3600-m*60;
    	printf("%d %d %d",h,m,s);
    	printf("\n");
	}   
}

2. Sort: input n, there are n groups of test numbers, each group has a variable length, and the output is sorted from small to large.

Input example:
2
1 4 7 2 5
8 9 4 3
Output:
1 2 4 5 7
3 4 8 9

Reference Code:

#include<stdio.h>
#include<string.h>
sort(int a[],int n)
{
    
    
	int i,j,k;
	for(i=0;i<n-1;i++)
	{
    
    
		for(j=0;j<n-i-1;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    
				k=a[j];
				a[j]=a[j+1];
				a[j+1]=k;
			}
		}
	}
}
int main()
{
    
    
	int i,n,len,a[100];
	char ch;
	scanf("%d%*c",&n);
	while(n--)
	{
    
    
		len=0;
		do
		{
    
    
			scanf("%d",&a[len]);
			len++;
		}while((ch=getchar())!='\n');
		sort(a,len);
		for(i=0;i<len;i++)
			printf("%d ",a[i]);
		printf("\n");
	}
}

3. Enter the number of rows, and then enter an expression in each row to get the result.

Input example:
3
1+1
2.2/3
1+2*3
Output:
2
0.7
7

Reference Code:

#include<stdio.h>
#define StackSize 100
#define QueueSize 100

//队的DS定义
typedef struct
{
    
    
    char data[100];
    int front,rear;  //front -- 头指针    rear -- 尾指针
}SeqQueue;

//队的初始化
void InitQueue(SeqQueue *Q)
{
    
    
    Q->front = 0;
    Q->rear = 0;
}

//队的判空 1-空 0-没空
int QueueEmpty(SeqQueue Q)
{
    
    
	if(Q.front==Q.rear)
		return 1;
	else
		return 0;
}

//入队
int EnQueue(SeqQueue *Q,char x)
{
    
    
    if((Q->rear+1) % QueueSize == Q->front)
        return 0;
    else
    {
    
    
        Q->data[Q->rear] = x;
        Q->rear = (Q->rear+1) % QueueSize;
    }
}

//出队
char DeQueue(SeqQueue *Q)
{
    
    
    char x;
    if(Q->rear == Q->front)
        return 0;
    else
    {
    
    
        x = Q->data[Q->front];
        Q->front = (Q->front + 1) % QueueSize;
        return x;
    }
}

//栈的DS定义
typedef struct
{
    
    
    char data[100];
    int top;
}SeqStack;


//栈的初始化
void InitStack(SeqStack *S)
{
    
    
    S->top = -1;
}

//入栈
int Push(SeqStack *S,char x)
{
    
    
    if(S->top == StackSize - 1)
    	return 0;
    else
    {
    
    
        S->top++;
        S->data[S->top] = x;
    }
}

//出栈
char Pop(SeqStack *S)
{
    
    
    char x;
    if(S->top == -1)
        return 0;
    else
    {
    
    
        x = S->data[S->top];
        S->top--;
        return x;
    }
}

//取栈顶元素
char GetTop(SeqStack S)
{
    
    
    char x;
    x = S.data[S.top];
    return x;
}

//判断栈空 1-空 0-没空
int EmptyStack(SeqStack S)
{
    
    
    if(S.top != -1)
        return 0;
    else
        return 1;
}

//优先级函数
int Priority(char op)
{
    
    
    switch(op)
    {
    
    
    case '(':
    case '#':return 0;
    case '+':
    case '-':return 1;
    case '*':
    case '/':return 2;
    }
    return -1;
}

//中缀表达式 转 后缀表达式
void CTpostExp(SeqQueue *Q)
{
    
    
    char c,t;
    SeqStack S;
    InitStack(&S);
    do
    {
    
    
        c = getchar();
        switch(c)
        {
    
    
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':EnQueue(Q,c); break;
        case '(':Push(&S,c); break;
        case ')':
              do
              {
    
    
                  t = Pop(&S);
                  EnQueue(Q,t);
              }while(t != '(');
              break;
        case '+':
        case '-':
        case '*':
        case '/':
            if(S.top == -1) Push(&S,c);
            else
            {
    
    
                while(Priority(c) <= Priority(GetTop(S)))
                {
    
    
                    t = Pop(&S);
                    EnQueue(Q,t);
                }
                Push(&S,c);
            }
            break;
        }
    }while(c != '\n');
    while(!EmptyStack(S))
    {
    
    
        t = Pop(&S);
        EnQueue(Q,t);
    }
}

//根据后缀表达式求值
void CaculatePostExp(SeqQueue Q)
{
    
    
    char temp;
    char num;  //用于保存输入数据
    int x,y,result;
    SeqStack S;
    InitStack(&S);
    while(!QueueEmpty(Q))
    {
    
    
        temp = DeQueue(&Q);
        switch(temp)
        {
    
    
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':Push(&S,temp); break;
        case '+':
            y = Pop(&S) - '0';
            x = Pop(&S) - '0';
            Push(&S,(char)(x + y + '0'));
            break;
        case '-':
            y = Pop(&S) - '0';
            x = Pop(&S) - '0';
            Push(&S,(char)(x - y + '0'));
            break;
        case '*':
            y = Pop(&S) - '0';
            x = Pop(&S) - '0';
            Push(&S,(char)(x * y + '0'));
            break;
        case '/':
            y = Pop(&S) - '0';
            x = Pop(&S) - '0';
            Push(&S,(char)(x / y + '0'));
            break;
        }
    }
    temp = Pop(&S);
    result = temp - '0';
    printf("%d\n",result);
}
int main()
{
    
    
    int n;
    char temp;
    SeqQueue Q;
    scanf("%d%*c",&n);
    while(n--)
    {
    
    
        InitQueue(&Q);
        CTpostExp(&Q);
        CaculatePostExp(Q);
    }
}

4. Bracket matching, input the number of tests n, and then input n sets of samples in n lines. If they match, output yes, otherwise output no.

Input example:
2
([3])
([[[)
Output:
yes
no

Reference Code:

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int n,i,len,flag,big,mid,small;
	char a[100];
	scanf("%d%*c",&n);
	while(n--)
	{
    
    
		flag=1;
		big=0;
		mid=0;
		small=0;
		gets(a);
		len=strlen(a);
		for(i=0;i<len;i++)
		{
    
    
			switch(a[i])
			{
    
    
				case'{':
					big++;
					break;
				case'[':
					mid++;
					break;
				case'(':
					small++;
					break;
				case'}':
					if(big>0) big--;
					else flag=0;
					break;
				case']':
					if(mid>0) mid--;
					else flag=0;
					break;
				case')':
					if(small>0) small--;
					else flag=0;
					break;
			}
		}
		if(big!=0||mid!=0||small!=0)
			flag=0;
		if(flag==1)
			printf("yes\n");
		if(flag==0)
			printf("no\n");
	}
}

Guess you like

Origin blog.csdn.net/Wxy971122/article/details/105361539