Algorithmic Getting Started note------------Day1

Source: Internet
Author: User

The 1.C language uses%d to display the float value, does not convert the float value to an approximate int value, but instead displays the garbage value, uses%f to display the int value, and does not convert the int value to a floating-point value

2. Three-digit reversal: Enter a three-digit number, separate its hundred, 10-bit and single-digit, invert output

#include <stdio.h>int main (void) {        int A;        scanf ("%d", &a);        printf ("%d%d%d\n", a%10,a/10%10,a/100);        return 0;} Consider the case of 250, which is sufficient to output 52 or 052#include<stdio.h>int main (void) {        int A;        scanf ("%d", &a);        a=a%10*100+a/10%10*10+a/100;        printf ("%03d\n", a);        return 0;}

3. Two-digit exchange

Variable Exchange #include<stdio.h>int main (void) {        int t,a,b;        scanf ("%d%d", &a,&b);        T=a;        a=b;        b=t;        printf ("%d%d", A, b);        return 0;} The two-variable method #include<stdio.h>int main (void) {int A a        , A;        scanf ("%d%d", &a,&b);        A=a+b;        B=a-b;        A=a-b;        printf ("%d%d\n" A, b);} Different or thought, be cautious with # define swap (A, b) a^=b^=a^=b Note here that the two modifications to the same variable cannot be placed in an expression, #include <stdio.h> #define SWAP (A, b) a^=b^= A^=bint Main (void) {int A=1,b=2;int c[]={1,2};swap (A, b), swap (c[0],c[1]);p rintf ("a=%d,b=%d\n", A, b);p rintf ("c[0]=%d, C[1]=%d\n ", c[0],c[1]); return 0;} Running results under GCC: a=2,b=1c[0]=0,c[1]=1 running results in VC6.0 environment: a=2,b=1c[0]=2,c[1]=1

43 integers

#include <stdio.h>int main (void) {        int a,b,c,t;        scanf ("%d%d%d", &a,&b,&c);        if (a>b)  {            t=a;a=b;b=t;        }        if (a>c) {            t=a;a=c;c=t;        }        if (b>c) {            t=b;b=c;c=t;        }        From small to large arrange        printf ("%d%d%d\n", a,b,c);        return 0;} Note that if this is a, B, and then compare B,c, the last a,c, here it is possible that the median was modified two times #include<stdio.h>int main (void) {        int a,b,c,x,y,z;        scanf ("%d%d%d", &a,&b,&c);        X=a;if (b<x)  x=b;if (c<x)  x=c;        Z=a;if (b>z)   z=b;if (c>z)   z=c;        Y=a+b+c-x-z;        printf ("%d%d%d\n", X, Y, z);        return 0;}

Trigonometric functions in 5.C language use radians instead of angles

6. Leap Year judgment

if (year%4==0&&year%100!=0) | | (year%400==0)
return 1;
Else
return 0;

7. Four-bit complete square number with output shape such as Aabb

#include <stdio.h> #include <math.h>int main (void) {        int a,b,s;        for (a=1;a<=9;a++) for          (b=0;b<=9;b++)            {                    s=1100*a+11*b;                    if (Floor (sqrt (s) +0.5) ==sqrt (s))   //floor is the integer portion of the return x, in order to reduce the error by rounding floor (x+0.5)                        printf ("%d\n", s);            }        return 0;} #include <stdio.h>int main (void) {        int x,n,hi,ho;        for (x=1;; x + +)        {            n=x*x;            if (n<1000)  continue;            if (n>9999) break  ;            hi=n/100;            ho=n%100;            if (hi/10==hi%10&&ho/10==ho%10)   printf ("%d\n", n);        }        return 0;}

8.3n+1 problems

#include <stdio.h>int main (void) {        int n,num=0;        scanf ("%d", &n);        while (n>1)        {            if (n%2==1)      n=3*n+1; Big data, multiplication can overflow            else        n=n/2;            num++;        }        printf ("%d\n", num);        return 0;} When the output 987654321 error, it is the multiplication overflow//In order to solve this problem, there is a good way to solve, that is, when n is odd, the 3n+1 must be even, when immediately divided by 2, that is (3n+1)/2= (2n+n+1)/2=n+ (n +1)/2; #include <stdio.h>int main (void) {        int n,num=0;        scanf ("%d", &n);        while (n>1)        {            if (n%2==1)      {                n=n+ (n+1)/2;    Modify here                num++;            }            else        N=N/2;            num++;        }        printf ("%d\n", num);        return 0;}

9. The sum of factorial

#include <stdio.h>int main (void) {        int i,j,n;        int s=0;        scanf ("%d", &n);        for (i=1;i<=n;i++)        {                int fac=1;                for (j=1;j<=i;j++)                    fac*=j;                S+=FAC;        }        printf ("%d\n", s%1000000);        return 0;} This may overflow, because the factorial data is very large, so the use of each step to the N-redundancy, the result is not changed, this is a knowledge point of number theory #include<stdio.h> #include <time.h>int main (void) {        int i,j,n;        int s=0;        const int mod=1000000;        scanf ("%d", &n);        for (i=1;i<=n;i++)        {                int fac=1;                for (j=1;j<=i;j++)                    fac= (fac*j%mod);               S= (S+FAC)%mod;        }        printf ("%d\n", s);        printf ("Time used=%.2lf\n", (double) clock ()/clocks_per_sec); Learn the usage of the time header file        return 0;}

10. File operation

#define Local#include<stdio.h> #define INF 1000000000int Main (void) {#ifdef LOCAL freopen ("data.in"            , "R", stdin);        Freopen ("Data.out", "w", stdout);        #endif//LOCAL;        int x=0,n,min=inf,max=-inf,s=0;                while (scanf ("%d", &n) ==1) {if (N>max) max=n;                if (n<min) min=n;                S+=n;        x + +;        } printf ("%d%d%.3lf\n", Min,max, (double) s/x); return 0;}        File input/output operation, not using redirection #include<stdio.h> #define INF 10000000int Main (void) {file *fin,*fout;        Fin=fopen ("data.in", "RB");        Fout=fopen ("Data.out", "WB");        int x=0,n,s=0,min=inf,max=-inf;            while (FSCANF (Fin, "%d", &n) ==1) {if (N>max) max=n;            if (n<min) min=n;            S+=n;        x + +;        } fprintf (Fout, "%d%d%.3lf\n", Min,max, (double) s/x);        Fclose (Fin);        Fclose (Fout); return 0;}

Exercises

Enter an integer that does not exceed 10 9, and the number of bits to output it #include<stdio.h>int main (void) {int n;        scanf ("%d", &n);        int num=1;                while (n>9) {N=N/10;        num++; } printf ("%d\n", num);}        Narcissus number #include<stdio.h>int main (void) {int a,b,c;            for (int i=100;i<=999;i++) {a=i/100;            b=i/10%10;            c=i%10;        if (a*a*a+b*b*b+c*c*c==i) printf ("%d\n", I); } return 0;}        Han Xin Soldiers of #include<stdio.h>int main (void) {int a,b,c,i;        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        } else continue;        } if (i>100) printf ("No answer\n"); return 0;}        Inverted triangle #include<stdio.h>int Main (void) {int n;        scanf ("%d", &n); for (int i=5;i>=1;i--) {for (int j=1;j<=2*i-1;j++) printf ("#");                printf ("\ n");        for (int k=5;k>=i;k--) printf (""); } return 0;}        Fractional decimal #include<stdio.h>int main (void) {int a,b,c;        scanf ("%d%d%d", &a,&b,&c);        printf ("%.*lf\n", C, (double) A/b); return 0;}        Approximate calculation, a bit of a problem #include<stdio.h> #include <math.h>int main (void) {int t=-1;        Double a=1.0,sum=1.0;                while (Fabs (a) >=0.01) {a= (1.0)/(a+2);                A=a*t;                Sum=sum+a;        T=t*-1;        } printf ("%.9lf\n", sum); return 0;}

  

Algorithmic Getting Started note------------Day1

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.