Beginner trained Jedi Series Beginners

Source: Internet
Author: User
Tags greatest common divisor cmath

FAQs for Beginners:

1. about the input character is missing

When you enter a numeric data, you must pay attention when you lose the characters. Because when the carriage return is swapped, the compiler treats the carriage return as a character. The correct approach is to :

scanf ("%d", &a);

GetChar ();// absorbing newline characters

scanf ("%c", &c);

2. about playing the table:

Table is used to deal with the problem of a large number of operations, and the common practice will time out of the situation, for some data is large and can be used in arrays to store the results of the problem, the table is a good choice.

The application of common table for beginners is the sub-table, the Prime number table, etc.

Problem Link:http://acm.hdu.edu.cn/showproblem.php?pid=1215(Tanabata festival)

http://acm.hdu.edu.cn/showproblem.php?pid=4548(US prime)

3. About header Files

in the suffix name is <cstdio <STDIO.H> same <CSTRING> Span style= "font-family: Arial" to replace <STRING.H>

<cmath> replace <CMATH> this is c++ Keep the original c Some of the functions of the practice. Use <CSTDIO> or With <STDIO.H> purely to see a person's liking.

The new person in the reference code may see the <iostream> using namespace std; CIN >> cout << Endl These are all things in C + + , and there's no need to delve into them.

4. About vc6.0 black border issues at run time

This problem generally occurs because the array is too large, and it is necessary to place the array outside the main function when defining the array .

5. See this believe you have been very patient, now is some novice practical templates:


Judge a function to determine whether it is a prime number

<span style= "FONT-SIZE:14PX;" >//Prime number judge int is_prime (int x)//is the prime number true, otherwise false {     if (x<2) return 0;    for (int i=2; i*i<=x; i++)    {        if (x%i==0) return 0;    }    return 1;} </span>


greatest common divisor recursive notation
<span style= "FONT-SIZE:14PX;" >//greatest common Divisor int gcd (int a,int b)//Ask for A, B greatest common Divisor {    if (a<b) return gcd (b,a);    Return!B?A:GCD (b,a%b);} </span>

List of prime numbers
<span style= "FONT-SIZE:14PX;" >//decimal table int a[1000002]; int table () {    int i,j;    For (i=2, i*i<=1000002; i++)    if (!a[i]) for    (j=i*2; j<=1000002; j+=i)    a[j]=1;} After you finish the table, you can open an array of record primes </span>

Tanabata Festival Code:

#include <cstdio>int p[500002]={0};void table ()//dozen tables to find the sub {    int i,j;    For (I=1, i<=500002/2; i++)    {for        (j=i*2; j<=500002; j+=i)        {                 p[j]+=i;        }}    } int main () {    int t,n;    Table ();    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        printf ("%d\n", P[n]);    }    return 0;}

US Prime code:

/* Problem Solving ideas: The prime number of the table, and then use the table thinking will be the numbers of the United States also hit the table */#include <cstdio>int a[1000002]={1,1,0},b[1000002]={0};//open outside will not explode int table () {    int i,j;    For (i=2, i*i<=1000002; i++)    if (!a[i]) for    (j=i*2; j<=1000002; j+=i)    a[j]=1;} int sum (int x) {    return x%10+x/10%10+x/100%10+x/1000%10+x/10000%10+x/100000%10+x/1000000%10;} int main () {    table ();    int i,j,n,x,y;    scanf ("%d", &n);     for (I=1; i<=1000002; i++)    {            if (!a[i]&&!a[sum (i)])                b[i]=b[i-1]+1;            else b[i]=b[i-1];    }    for (j=1; j<=n; j + +)    {         scanf ("%d%d", &x,&y);        printf ("Case #%d:%d\n", j,b[y]-b[x-1]);    }    return 0;}




Beginner trained Jedi Series Beginners

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.