"Introduction to the algorithm contest" on the computer Practice--Chapter II _c

Source: Internet
Author: User
Tags bitwise mathematical functions

Exercise 2-1 digits (digit)

Enter a positive integer that does not exceed the 10^9, outputting its number of digits. For example, 12735 digits are 5. Do not use any mathematical functions, only arithmetic and circular statements are implemented.

#include <stdio.h>

int main ()
{
    int x,num=0;
    scanf ("%d", &x);
    while (x)
    {
    	x/=10;
    	num++;
    }
    printf ("%d\n", num);
	
	return 0;



Exercise 2-2 Narcissus number (daffodil)

Output the number of daffodils in the 100~999. If the 3-digit ABC satisfies abc=a^3+b^3+c^3, it is called Narcissus number. such as 153=1^3+5^3+3^3, so 153 is the number of daffodils.

#include <stdio.h>

int main ()
{
    int i,a,b,c;
    For (i=100 i<=999; i++)
    {
    	a = i%10;     /* Single-digit
    	/b = i/10%10;  * * 10 bits
    	/c = i/100;    /* Hundred */
    	if (i = = a*a*a + b*b*b + c*c*c)
    	    printf ("%d\n", I);
    }
	
	return 0;
Results: The number of three daffodils are: 153,370,371,407



Exercise 2-3 Han Soldiers of (hanxin)

It is rumored that Han Xin talent, never directly counted the number of their own troops, as long as the soldiers to three people a row, five people a row, seven people in a row to transform formation, and he only glanced at the team's Shang to know the total number. Enter 3 nonnegative integer a,b,c, representing the number of Shang per formation (a<3,b<5,c<7), the minimum number of outputs (or no solution reported). The total number of known is not less than 10, not more than 100.

Sample input: 2 1 6

Sample output: 41

Sample input: 2 1 3

Sample output: No answer

#include <stdio.h>

int main ()
{
    int i,a,b,c;
    scanf ("%d%d%d", &a,&b,&c);
    For (i=10 i<=100; i++)
    {
    	if (i%3==a && i%5==b && i%7==c) 
    	{
    		printf ("%d\n", I);
    		break;
    	}
    }
    if (i = =)
        printf ("No answer\n");
	
	return 0;


Exercise 2-4 Inverted triangle (triangle)

Enter a positive integer n<=20, outputting an n-tier inverted triangle. For example, the output of n=5 is as follows:

#########
 #######
  #####
   ###
    #

#include <stdio.h>

int main ()
{
    int n,i,j;
    scanf ("%d", &n);
    for (i=n;i>=1;i--)
    {for
    	(j=1;j<=n-i;j++)
    	    printf ("");
		for (j=1;j<=2*i-1;j++)
    	    printf ("#");
    	printf ("\ n");
    }
	
	return 0;


Exercise 2-5 Statistics (STAT)

Enter a positive integer n, then read n positive integer a1,a2,...,an, and finally read a positive integer m. The number of statistics A1,a2,...,an is less than M. Hint: If redirects and fopen can be used, which is more convenient.


This way ... Welcome advice.



Exercise 2-6 Harmonic series (harmony)

Enter positive integer n, output H (n) = 1 + 1/2 + 1/3 +...+ 1/n value, retain 3 decimal places. For example N=3, the answer is 1.833.

#include <stdio.h>

int main ()
{
    int n,i;
    Double H = 0;
    scanf ("%d", &n);
    for (I=1; i<=n; i++)
    	H + = 1.0/i;
    printf ("%.3lf\n", H);
	
	return 0;


Exercise 2-7 Approximate calculation (approximation)

Compute PI/4 = 1-1/3 + 1/5-1/7 + ... until the last item is less than 10^ (-6).

#include <stdio.h>

int main ()
{
    int i=1,flag=1;
    Double sum=0,item=1.0;
    
    while (item>=1.0/1000000)
	{
		sum = Flag*item;
		Flag *=-1;
		i + 2;
		item = 1.0/i;
    }
    
    printf ("PI =%lf\n", sum*4); 
	
	return 0;


Exercise 2-8 the and (subsequence) of the subsequence

Enter two positive integer n<m<10^6, output 1/n^2 + 1/(n+1) ^2 +...+ 1/m^2, and retain 5 decimal places. For example, when n=2,m=4 the answer is 0.42361;n=65536,m=655360, the answer is 0.00001. Note: There is a trap.

#include <stdio.h>

int main ()
{
    int i,n,m;
    Double sum=0;
    scanf ("%d%d", &n,&m);
    
    for (i=n;i<=m;i++)
    	sum + = 1.0/i/i;
    
    printf ("%.5lf\n", sum); 
	
	return 0;
The trap: When N or M is larger, I * I will overflow, cannot use 1.0/(I * i), should use 1.0/i/I. Or use a long long data type instead.



Exercise 2-9 Fractional Decimal (decimal)

Enter a positive integer a,b,c, output A/b decimal form, accurate to the decimal point C bit. A,b<=10^6,c<=100. For example, a=1,b=6,c=4 should output 0.1667.

#include <stdio.h>

int main ()
{
    int a,b,c;
    scanf ("%d%d%d", &a,&b,&c);
    printf ("%.*lf\n", c,1.0*a/b); /* New knowledge: In the format controller, * can be replaced by the variable behind. * * return
	
	0;

The problem with this approach is that when the C is large, the fractional part after the more than 10 decimal digits shows only zero, and I don't know why.

The following is a bitwise decimal method, which is completely fine. (This code is not what I wrote, the comments I added, the source of code to see the end of the blog.) )

#include <stdio.h>
 
int main (void) 
{ 
    int a,b,c,mod,re,i,m,x,y;
    while (scanf ("%d%d%d", &a,&b,&c) ==3)
    {
        printf ("%d", A/b);   /* Output Integer part * 
        /mod=a%b;
        if (c>0)
        {
            printf (".");
            for (i=1;i<c;i++)/  * Bitwise calculation OUTPUT c-1 decimal * *
                m=mod*10;
                re=m/b;
                printf ("%d", re);
                mod=m%b;
            }
            m=mod*10;
            x=m/b;
            mod=m%b;    /* More than one, rounded to the last one we need * * *
            m=mod*10;
            y=m/b;
            if (y>=5)
            x + +;
            printf ("%d\n", x);  /* Output Last decimal *
    /}} return
    0; 
}


Exercise 2-10 Arrangement (permutation)

With 1,2,3,..., 9 consists of 3 three-digit abc,def and GHI, each number is used exactly once, requiring Abc:def:ghi = 1:2: 3. Outputs all the solutions. Tip: Don't use your brains.

#include <stdio.h> int arr[10] = {0};/* array arr to record whether 1~9 each number appears, corresponds to subscript one by one, appears as 1, otherwise 0*/int main () {int a,b,c,d,e,f,g,h,i
	, Abc,def,ghi,t,sum; for (a=1;a<=3;a++)/*a Max is 3*/for (b=1;b<=9;b++) for (c=1;c<=9;c++) for (d=2;d<=6;d++)/*d min 2, max for 6* /for (e=1;e<=9;e++) for (f=1;f<=9;f++) for (g=3;g<=9;g++)/*g min 3*/for (h=1;h<=9;h
										    
						+ +) for (i=1;i<=9;i++) {for (t=1;t<=9;t++) arr[t] = 0;
						Arr[a] = 1;arr[b] = 1;arr[c] = 1;
					    ARR[D] = 1;arr[e] = 1;arr[f] = 1;
										
						ARR[G] = 1;arr[h] = 1;arr[i] = 1;
	                    sum = 0;
	                    for (t=1;t<=9;t++)/* If the array is cumulative and is 9, which means that all 1~9 numbers appear * * * sum+=arr[t];
	                        if (sum==9) {abc = A*100+B*10+C;
	                        def = d*100+e*10+f;
			                Ghi = G*100+h*10+i; if (abc*2==def && Abc*3==ghi) printf ("%d:%d:%d = 1:2:3\n", Abc,def,ghi);
} return 0; }

Results:

192:384:576 = 1:2:3

219:438:657 = 1:2:3

273:546:819 = 1:2:3

327:654:981 = 1:2:3


There is another online method, a certain degree of reverse thinking, exquisite, running faster than my multi-layer for loop nesting more quickly, my program cycle number of 3*9*9*5*9*9*7*9*9 = 55,801,305 times, and the following procedures only 333-100 = 233 times.

#include <stdio.h>

int main (void)
{
	int x, y, z, a[10] = {0};
	for (x = n < 333 x + +)
	{
		y = 2*x;
		z = 3*x;
		Number of a[appearing] = 1
		a[x/100] = a[x/10%10] = a[x%10] = 1;
		A[Y/100] = a[y/10%10] = a[y%10] = 1;
		A[Z/100] = a[z/10%10] = a[z%10] = 1;
		int I, s = 0;
		for (i = 1; i < i++)
			s + = a[i];
		if (s = = 9)
			printf ("%d\t%d\t%d\n", X, Y, z);
		for (i = 1; i < i++)	//re-assign to 0
			a[i] = 0;
	}
	return 0;
}



If there are errors, please note that the discussion is welcome.

Note: This article is a partial reference to this blog: http://blog.csdn.net/litiouslove/article/details/7891700

Related Article

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.