2013/08/09

SPOJ: Бодлогын ангилал


http://www.spoj.com/CSMS/problems/TOP0005/


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

#define true 1
#define false 0

typedef int bool;

int main()
{
 char str[51];
 int length;
 int sum = 0;
 int words = 0;
 int i;
 bool flag = true;
 bool isWord = true;
 bool dot = false;
 int wordlength = 0;

 gets(str);
 length = strlen(str);

 for(i = 0; i <= length; i++)
 {
  switch(str[i])
  {
  case '\0':
  case ' ':
   if(!dot && isWord && wordlength > 0)
   {
    //printf("%c\n", str[i - 1]);
    sum += wordlength;
    words++;
   }
   wordlength = 0;
   isWord = true;
   dot = false;
   break;
  case '.':
   if(i == 0 || str[i - 1] == ' ')
    isWord = false;
   if(dot)
    isWord = false;
   else if(str[i + 1] == ' ' || str[i + 1] == '\0')
   {
    wordlength++;
    flag = false;
   }
   if(flag)
   dot = true;
   flag = true;
   break;
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
   isWord = false;
   break;
  default:
   wordlength++;
   break;
  }
 }
 //printf("%d %d\n", sum, words);
 if(words == 0)
  printf("250");
 else
  switch(sum / words)
  {
  case 1:
  case 2:
  case 3: printf("250"); break;
  case 4:
  case 5: printf("500"); break;
  default: printf("1000"); break;
  }
 return 0;
}

No comments:

Post a Comment