其他分享
首页 > 其他分享> > 6kyu—Prize Draw

6kyu—Prize Draw

作者:互联网

To participate in a prize draw each one gives his/her firstname.

Each letter of a firstname has a value which is its rank in the English alphabet. A and a have rank 1, B and b rank 2 and so on.

The length of the firstname is added to the sum of these ranks hence a number som.

An array of random weights is linked to the firstnames and each som is multiplied by its corresponding weight to get what they call a winning number.

Example:

names: “COLIN,AMANDBA,AMANDAB,CAROL,PauL,JOSEPH”
weights: [1, 4, 4, 5, 2, 1]

PauL -> som = length of firstname + 16 + 1 + 21 + 12 = 4 + 50 -> 54
The weight associated with PauL is 2 so PauL’s winning number is 54 * 2 = 108.
Now one can sort the firstnames in decreasing order of the winning numbers. When two people have the same winning number sort them alphabetically by their firstnames.

Task:

parameters: st a string of firstnames, we an array of weights, n a rank

return: the firstname of the participant whose rank is n (ranks are numbered from 1)

Example:

names: “COLIN,AMANDBA,AMANDAB,CAROL,PauL,JOSEPH”
weights: [1, 4, 4, 5, 2, 1]
n: 4

The function should return: “PauL”
Notes:

The weight array is at least as long as the number of names, it may be longer.

If st is empty return “No participants”.

If n is greater than the number of participants then return “Not enough participants”.

See Examples Test Cases for more examples.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct win
{
char fname[50];
int Wnumber;
};
void prize(struct win *st,int we);//负责计算Wnumber
void sort(struct win **st,int len);

char* nthRank(char* st, int* we, int n) {
  // your code
 if(*st== '\0')
return "No participants";
// 特殊情况判断

  int j=0,i=0,word=0;
 struct win**winner=(struct win **)malloc(20*sizeof(struct win*));
 for(i=0;i<20;i++)
   {
      winner[i]=(struct win *)malloc(sizeof(struct win));
   }//分配一个结构数组

 i=0;
 do{    //将字符串中的名字copy到结构数组中
    if(st[j]==',')
      {
        winner[word++]->fname[i]='\0';
        i=0; j++;
      }
    else 
       winner[word]->fname[i++]=st[j++];
   
  }while(st[j]!='\0');
    
      if(n>word+1)  //如果参与人数不足...
        return "Not enough participants";
      
     for(i=0;i<word+1;i++)
    {
      prize(winner[i], we[i]);//计算Wnumber
    }

    sort(winner,word+1);//排序

  if(winner[n-1]->Wnumber == winner[n]->Wnumber &&
    strcmp(winner[n-1]->fname,winner[n]->fname) > 0 )
        return winner[n]->fname;
    else 
        return winner[n-1]->fname;
}
void prize(struct win *st,int we)
{
   int len = (int)strlen(st->fname);
    st->Wnumber += len;

  for(int i=0;i<len;i++)
      st->Wnumber += toupper( (int)st->fname[i] ) - 64 ; 
      
  st->Wnumber *= we;
}
void sort(struct win **st,int len)
{
struct win *t;
for(int i=0;i<len;i++)
  {
  for(int j=i;j<len;j++)
    {
      if(st[i]->Wnumber<st[j]->Wnumber)
        {
           t=st[i];
           st[i]=st[j];
           st[j]=t;
        }
    }
  }

}	

标签:Draw,return,struct,int,win,st,fname,6kyu,Prize
来源: https://blog.csdn.net/weixin_53054309/article/details/115263297