Advanced Language Course Design Report Second report: Optimization of enumerations

Source: Internet
Author: User

Internship Topics

Second report: Optimization of enumerations

L Reading Group Document "program design guidance and online practice"8 Zhang Yi 8.1,8.2,8.4.

L for 8.2 question, can you continue to improve? Try devc++ to write an improved version of the program, describe your improved algorithm, and post code and comments.

L Completion of ACM Club Assignment:2015cup Internship 2 Enumeration Optimization

L Describe your algorithm and annotate your program.

L Note Do not copy: The system automatically determine plagiarism, once copied,0 points.

First, internship purpose: in-depth learning enumeration optimization

second, the need to describe the problem-solving algorithm of 8.2,8.4 to your inspiration, 8.2 Examples of the algorithm can not be improved? Describe your improved algorithm and paste in the code and comments.

Inspiration: To minimize the amount of computation, increase the span of each cycle, thus reducing the number of cycles; using mathematical formula derivation, the relation of known quantity is more direct, and the repetition calculation of each time is avoided.

Iii. for each of your topics :

1. Algorithm description.

Number of Right triangle: through the depth of the right triangle, the range of its edges can be further reduced, thus reducing the amount of computation, such as the longest edge length is between One-second and One-third of the perimeter, greatly reducing the computational amount, and the second-longest edge is necessarily greater than its and the minimum edge of One-second, and less than the maximum edge, Reduce the amount of computation.

0~9 permutation: Directly through the repeat for if, one by one than the amount of computation is not too large, directly through.

2. Your code and comments.

8.2 Optimization:

#include <stdio.h>

int main ()

{

int p,e,i,d,j,no=1;

scanf ("%d%d%d", &p, &e, &i, &d);

while (p!=-1 && e!=-1 && i!=-1 && d!=-1)

{

J= (33-(d+1-i)%33) +d+1; /* Do not loop, reduce the loop, and move 33 before the first operation */

for (; j<21252; j=j+33)

if ((J-E)%28==0)

Break

for (; j<21252; j=j+33*28)/*33*28 value is relatively large */

if ((j-p)%23 = = 0)

Break

printf ("Case%d", no);

printf (": The next triple peak occurs in%d days.\n", j-d);

scanf ("%d%d%d%d", &p, &e, &i, &d);

no++;

}

}

number of Right triangle :

#include <stdio.h>

int main ()

{

int t,a,a,b,c,n,i; A is perimeter

scanf ("%d", &t);

GetChar ();

while (t)//test count

{

n=0;

scanf ("%d", &a);

For (c= (A/2);c> (A/3); c--)//MAX Edge limit

{

For (b= ((a-c)/2); b<c;b++)//second edge limit

{

A=a-b-c;

if (b*b+a*a==c*c&&a<b)//judgment

{

n++;

}

}

}

printf ("%d\n", N);

t--;

}

return 0;

}

0~9 permutations Evenly :

#include <stdio.h>

int main ()

{

int a,b,c,d,e,f,g,h,i,j,n=0;

for (a=0;a<10;a++)//each starting from zero for

{

for (b=0;b<10;b++)

{

if (b==a)//compare with previous one

Continue

for (c=0;c<=9;c++)

{

if (c==a| | C==B)

Continue

for (d=0;d<=9;d++)

{

if (d==a| | d==b| | D==C)

Continue

for (e=0;e<=9;e++)

{

if (e==a| | e==b| | e==c| | E==d)

Continue

for (f=0;f<=9;f++)

{

if (f==a| | f==b| | f==c| | f==d| | F==E)

Continue

for (g=0;g<=9;g++)

{

if (g==a| | g==b| | g==c| | g==d| | g==e| | G==F)

Continue

for (h=0;h<=9;h++)

{

if (h==a| | h==b| | h==c| | h==d| | h==e| | h==f| | H==G)

Continue

for (i=0;i<=9;i++)

{

if (i==a| | i==b| | i==c| | i==d| | i==e| | i==f| | i==g| | I==H)

Continue

for (j=0;j<=9;j++)

{

if (j==a| | j==b| | j==c| | j==d| | j==e| | j==f| | j==g| | j==h| | J==i)

Continue

if ((a*10000+b*1000+c*100+d*10+e)% (f*10000+g*1000+h*100+i*10+j) ==0)//judgment is divisible

n++;

}

}

}

}

}

}

}

}

}

}

printf ("%d", n);

return 0;

}

3. the design and debugging process encountered problems and solutions.

Number of right triangle :

do not understand the main optimization plan, I think, always think not how to, but have to refer to other people's ideas, see a few are not very understanding, and even do not understand why to do so, after looking at a few ideas to learn a good understanding of the several, choose the most easy to write.

0~9 permutations Evenly :

no difficulty, follow the idea of one-time writing success.

    1. Experience Experience and self-evaluation of the program.

number of Right triangle :

feel their IQ catch nasty ah, perhaps is not familiar with, perhaps just can't think of that method, but I feel also need to accumulate experience, see more, will be more. The program is not difficult, is the algorithm bad thinking.

0~9 permutations Evenly :

There is nothing to say that can be programmed in this way in the future.

Advanced Language Course Design Report Second report: Optimization of enumerations

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.