顯示具有 Nice Licence Plates 標籤的文章。 顯示所有文章
顯示具有 Nice Licence Plates 標籤的文章。 顯示所有文章

2015年7月27日 星期一

[C][瘋狂程設][CPE考題20140923] 14E3.UVA12602:Nice Licence Plates

Nice Licence Plates

Alberta licence plates currently have a format of ABC-0123 (three letters followed by four digits).
We say that the licence plate is "nice" if the absolute difference between the value of the first part and the value of the second part is at most 100.
The value of the first part is calculated as the value of base-26 number (where digits are in [A..Z]). For instance, if the first part is "ABC", its value is 28 (0*26^2 + 1*26^1 + 2*26^0). So, the plate "ABC-0123" is nice, because |28-123|<=100.
Given the list of licence plate numbers, your program should determine if the plate is nice or not.

input

First line of the input contains an integer N (1<=N<=100), the number of licence plate numbers. Then follow N lines, each containing a licence plate in the format LLL-DDDD.
2
ABC-0123
AAA-9999

output

For each licence plate print on a line "nice" or "not nice" (without quotes) depending on the plate number being nice as described in the probem statement.
nice
not nice

Notes:


1.利用sscanf()分離出前三個字母與後四個數字

  scanf("%s",input);
  sscanf(input,"%[^-]-%d",letter,&digits);

2.計算前三個字母轉化成26進位當中所表示的數字大小

letter[0]-'A')*BASE*BASE+(letter[1]-'A')*BASE+(letter[2]-'A'

3.計算整數的絕對值: abs()

abs((letter[0]-'A')*BASE*BASE+(letter[1]-'A')*BASE+(letter[2]-'A')-digits);

Code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define BASE 26 
#define MAX 100


int main(void){

 int num,i=0,digits,result;;
 char *input,letter[3];

 scanf("%d",&num);
 
 while(i<num){
  scanf("%s",input);
  sscanf(input,"%[^-]-%d",letter,&digits);
  result = abs((letter[0]-'A')*BASE*BASE+(letter[1]-'A')*BASE+(letter[2]-'A')-digits);
  printf("%s\n",abs(result)<=MAX?"nice":"not nice");
  i++;
 }
 return 0;
}