好老師(湖南省第九屆程序設計大賽原題)

好老師


題目不解釋,中國人都能看懂。


思路分析:

對所有的學生,首先分爲兩種:

<一>老師認識他,直接叫名字即可

<二>老師不認識他,由別的學生的名字和他與那個學生的相對位置關係來叫他


對於第一種學生,最好辦了,直接輸出名字就好了。

對於第二種學生,只能說,哎,老師都不認識你,真難爲我。

分三種情況:

(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 
****************************************************************/


發佈了150 篇原創文章 · 獲贊 35 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章