好老师(湖南省第九届程序设计大赛原题)

好老师


题目不解释,中国人都能看懂。


思路分析:

对所有的学生,首先分为两种:

<一>老师认识他,直接叫名字即可

<二>老师不认识他,由别的学生的名字和他与那个学生的相对位置关系来叫他


对于第一种学生,最好办了,直接输出名字就好了。

对于第二种学生,只能说,哎,老师都不认识你,真难为我。

分三种情况:

(1)只有1个老师叫得出名字的同学离他最近,他在那个同学的右边

(2)只有1个老师叫得出名字的同学离他最近,他在那个同学的左边

(3)有2个老师叫叫得出名字的同学离他最近(一样近啦),他在这两个同学的中间


哈哈!这样一分析,题目就不难了。想当年金戈铁马!!!可是写了好长【代码二】,经过n次错误提交才AC的,我的时间!!!其实,纯属想多了,,,没那么复杂啦!!


解题思路:【代码一】

(1)开3个数组:

student[n][x]  n个学生的名字,名字最长有x个字符

lift[x]   第x个学生离他最近的老师叫得出名字的那个同学(第lift[x]个同学)的左边(不排除在他左边有一个离他一样近的,老师交的出名字的同学【中间】)

right[x]  第x个学生离他最近的老师叫得出名字的那个同学(第right[x]个同学)的右边(说明同上)

(2)初始化所有数据:

首先就是对输入的的数据,直接初始化student[][]数组、n、q,然后,把lift[]和right[]数组初始化为-1,表示,不是最近

(3)处理数据:

就是处理lift[]和right[]数组,根据能否叫出,名字和距离来确定【咱还是看代码吧!这里不好解释啊】

(4)回答问题:

对于题目给出的人的序号,输出老师该叫的方式,根据数据处理步骤得出的数据。

老师叫得出名字的,直接输出名字

老师叫不出名字的,要是lift[x]和right[x]都不为-1,那么,这个人在中间

 否则,值不是-1的数组的情况输出


【代码一】

#include<stdio.h>
#include<string.h>

int n,q; //the number of the students
char student[110][4]; //the name of each students
int right[110],lift[110];//the status of the student ,if it's no infromation it's initial by -1

void initial()//to initials the infromations by the input
{
    scanf("%d%d",&n);
    for(int i=0;i<n;i++)
    scanf("%s",student[i]);
    scanf("%d",&q);
    memset(right,-1,sizeof(right));
   // memset(middle,-1,sizeof(middle));
    memset(lift,-1,sizeof(lift));
}

void deal()//to deal with the questions the program will ask
{
    int i,j;
    for(i=0;i<n;i++)
    {
        if(student[i][0]=='?')
        {
            for(j=0;;j++)//the distence of the nearst student
            {
                if((i-j>=0&&student[i-j][0]!='?')&&(i+j<=n-1&&student[i+j][0]!='?'))
                {//in the middle of two student
                    right[i]=i-j;
                    lift[i]=i+j;
                    break;
                }
                else if(i-j>=0&&student[i-j][0]!='?')
                {//in the right of student i-j
                    right[i]=i-j;
                    break;
                }
                else if(i+j<=n-1&&student[i+j][0]!='?')
                {//in the lift of student i+j
                    lift[i]=i+j;
                    break;
                }
            }
        }
    }
}

void answer()//to answer the qustion by the infromations
{
    int x,i;
    scanf("%d",&x);
    x--;
    if(student[x][0]!='?')printf("%s\n",student[x]);
    else if(right[x]!=-1&&lift[x]!=-1)
        printf("middle of %s and %s\n",student[right[x]],student[lift[x]]);
    else if(right[x]!=-1)
    {
        for(i=x-right[x];i>0;i--)printf("right of ");
        printf("%s\n",student[right[x]]);
    }
    else
    {
        for(i=lift[x]-x;i>0;i--)printf("left of ");
        printf("%s\n",student[lift[x]]);
    }
}

int main()//it just the inter space
{
    initial();
    deal();
    while(q--)
        answer();
    return 0;
}


【代码二】

#include<stdio.h> 
#include<string.h> 
  
int n,q; //the number of the students 
char student[110][4]; //the name of every students 
int right[110],lift[110];//the status of the student ,if it's no infromation it's initial by -1 
  
void initial()//to initials the infromations by the input 
{ 
    scanf("%d%d",&n); 
    for(int i=0;i<n;i++) 
    scanf("%s",student[i]); 
    scanf("%d",&q); 
    memset(right,-1,sizeof(right)); 
   // memset(middle,-1,sizeof(middle)); 
    memset(lift,-1,sizeof(lift)); 
} 
  
void deal()//to deal with the questions the program will ask 
{ 
    int r=0,l=n-1;//for the border students 
    int i,j; 
    //for the liftist students 
    while(student[r][0]=='?')r++; 
    for(j=0;j<r;j++)lift[j]=r; 
    //for the rightist students 
    while(student[l][0]=='?')l--; 
    for(j=n-1;j>l;j--)right[j]=l; 
    //for the inside student 
    for(i=r+1;i<l;i++) 
    { 
        if(student[i][0]=='?') 
        { 
            for(j=0;;j++)//the distence of the nearst student 
            { 
                if((i-j>=0&&student[i-j][0]!='?')&&(i+j<=n-1&&student[i+j][0]!='?')) 
                {//in the middle of two student 
                    right[i]=i-j; 
                    lift[i]=i+j; 
                    break; 
                } 
                else if(i-j>=0&&student[i-j][0]!='?') 
                {//in the right of student i-j 
                    right[i]=i-j; 
                    break; 
                } 
                else if(i+j<=n-1&&student[i+j][0]!='?') 
                {//in the lift of student i+j 
                    lift[i]=i+j; 
                    break; 
                } 
            } 
        } 
    } 
} 
  
void answer()//to answer the qustion by the infromations 
{ 
    int x,i; 
    scanf("%d",&x); 
    x--; 
    if(student[x][0]!='?')printf("%s\n",student[x]); 
    else if(right[x]!=-1&&lift[x]!=-1) 
        printf("middle of %s and %s\n",student[right[x]],student[lift[x]]); 
    else if(right[x]!=-1) 
    { 
        for(i=x-right[x];i>0;i--)printf("right of "); 
        printf("%s\n",student[right[x]]); 
    } 
    else
    { 
        for(i=lift[x]-x;i>0;i--)printf("left of "); 
        printf("%s\n",student[lift[x]]); 
    } 
} 
  
int main()//it just the inter space 
{ 
    initial(); 
    deal(); 
    while(q--) 
        answer(); 
    return 0; 
} 
  
/************************************************************** 
    Problem: 1334 
    User: 20114045007 
    Language: C 
    Result: Accepted 
    Time:0 ms 
    Memory:964 kb 
****************************************************************/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值