Thursday, May 17, 2012

[UVa] 12414 - Calculating Yuan Fen


Method: Simulate it, nothing special. But don't handle with strings. TLE for sure then.
#include <stdio.h>

int y(char *s, int st) {
 int val[100], stor[100], v, i, j, k=0;
 for (i=0 ; s[i] ; i++) {
  v = s[i]+st-'A';
  for (j=0 ; v ; j++, v/=10) {
   stor[j] = v%10;
  }
  for (--j ; j>=0 ; j--, k++) {
   val[k] = stor[j];
   
  }
 }
 
 
 while (k>2) {
  for (i=0, j=0 ; j<k ; j++, i++) {
   val[i] = (val[j]+val[j+1])%10;
  }
  if (k==4) {
   if (val[0]==1 && val[1]==0 && val[2]==0) {
    return 100;
   }
  }
  k--;
 }
 return 0;
}

int main( void ) {
 char input[20];
 int i, found, result;
 while ( gets(input) ) {
  
  for (i=1, found=0 ; i<=10000 && found==0 ; i++) {
   result = y(input, i);
   if (result == 100) {
    found = 1;
   }
  }
  if (found == 1) {
   printf("%d\n",i-1);
  } else {
   printf(":(\n");
  }
 }
 return 0; 
}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...