2014 The fifth session of the Blue Bridge Cup test-C + + program Design Group B-Sfeng Calculator

Source: Internet
Author: User
Tags strcmp

Title Description


The revolutionary contribution of the Sfeng speed algorithm is to predict rounding from a high position. 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

Similarly, 2/7, 3/7, ... 6/7 are similar repeating decimal, multi-digit more than N/7, will enter n

The following program simulates the process of multiplying the Sfeng speed algorithm by 7.

The single-digit rule multiplied by 7 is: even times 2, odd times 2, plus 5, all take only one digit.

The rounding rule multiplied by 7 is:
Full 142857 ... In 1,
Full 285714 ... In 2,
Full 428571 ... In 3,
Full 571428 ... In 4,
Full 714285 ... In 5,
Full 857142 ... In 6

Please analyze the program flow and fill in the missing code for the underlined part.

Program code:

  

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

Calculate Bits
int Ge_wei (int a)
{
if (a% 2 = = 0)
Return (A * 2)% 10;
Else
Return (A * 2 + 5)% 10;
}

Calculate rounding
int Jin_wei (char* p)
{
char* level[] = {
"142857",
"285714",
"428571",
"571428",
"714285",
"857142"
};

Char buf[7];
BUF[6] = ' + ';
strncpy (buf,p,6);

int i;
for (i=5; i>=0; i--) {
int r = strcmp (Level[i], buf);
if (r<0) return i+1;
while (r==0) {
p + = 6;
strncpy (buf,p,6);
R = strcmp (Level[i], buf);
if (r<0) return i+1;
      if (r>0) return i; Blanks
}
}

return 0;
}

Multiply multiple digits by 7
void F (char* s)
{
int head = Jin_wei (s);
if (Head > 0) printf ("%d", head);

char* p = s;
while (*p) {
int a = (*p-' 0 ');
int x = (Ge_wei (a) + Jin_wei (p+1))% 10;
printf ("%d", x);
p++;
}

printf ("\ n");
}

int main ()
{
F ("428571428571");
F ("34553834937543");
return 0;
}

2014 The fifth session of the Blue Bridge Cup test-C + + program Design Group B-Sfeng Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.