/*Title: The revolutionary contribution of the Sfeng calculator Sfeng speed algorithm is to predict rounding from a high. Do not need 99 tables, completely subvert the traditional hand count! The core foundation of the calculator is the multiplication of 1-bit numbers multiplied by multiple digits. Where multiplying by 7 is the most complex, take it for example. Because, 1/7 is a repeating decimal: 0.142857 ..., if the number of digits more than 142857 ..., will go 1 the same, 2/7, 3/7, ... 6/7 are similar repeating decimal, multi-digit more than N/7, it is necessary to enter n the program simulates the Sfeng speed algorithm multiplied by 7 of the operation process. The single-digit rule multiplied by 7 is: even times 2, odd times 2, plus 5, all take only one digit. The Carry rule multiplied by 7 is: Full 142857 ... Enter 1, full 285714 ... Enter 2, full 428571 ... Enter 3, full 571428 ... Enter 4, full 714285 ... Enter 5, full 857142 ... In 6*/#include<stdio.h>#include<string.h>//Calculate BitsintGe_wei (inta) { ifA2==0) returnA2) %Ten; Else returnA2+5) %Ten; }//Calculate RoundingintJin_wei (Char*p) { Char* level[] = { "142857", "285714", "428571", "571428", "714285", "857142" }; Charbuf[7]; buf[6] =' /'; strncpy (Buf,p,6); inti; for(i=5; i>=0; i--){ intR =strcmp (Level[i], buf); if(r<0)returni+1; while(r==0) {p+=6; strncpy (Buf,p,6); R=strcmp (Level[i], buf); if(r<0)returni+1; if(r>0)returnI//Fill in } } return 0;}//multiply multiple digits by 7voidFChar*s) {intHead =Jin_wei (s); if(Head >0) printf ("%d", head); Char* p =s; while(*p) { intA = (*p-'0'); intx = (Ge_wei (a) + Jin_wei (p+1)) %Ten; printf ("%d", x); P++; } printf ("\ n");}intMain () {f ("428571428571"); F ("34553834937543"); return 0;}
Code Appreciation--Sfeng Calculator