& Quot; interesting integer & quot; Class Exercises, Interview Questions (article 1), and exercises

Source: Internet
Author: User

"Interesting integer" exercises, Interview Questions (article 1), and exercises

Question 1: Quantity

If a number is equal to the sum of its factors, this number is called the complete number. For example, 6 = 1 + 2 + 3. Write a program and obtain the number of all completions in less than 10000.

#include <stdio.h>#define MAXN 50000int main(void){    int divisor[MAXN];              //Use to save factors    int p = 0, count = 0, num = 0;    int numsave = 0, divisortest = 0, i = 0;    for (num = 1; num < 10000; num++)    {        p = 0;        numsave = num;        for (divisortest = 1; divisortest < num / 2 + 1; divisortest++)        {            if (num % divisortest == 0)            {                divisor[p++] = divisortest;                numsave -= divisortest;            }        }        if (numsave == 0)        {            printf("%d:%4d = ", ++count, num);            for (i = 0; i < p - 1; i++)            {                printf("%d + ", divisor[i]);            }            printf("%d\n", divisor[p - 1]);        }    }  return 0;}
Resolution: although the result shows that MAXN only needs to take 15, if you do so, it will inevitably lead to a segment error. When num = 9720, it needs 47 array units to store its factors, and the accessed array is out of bounds.

Question 2: daffodils

The value is equal to the sum of the three power of each digit. For example, 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3 is called the daffodils number. Program to find all the daffodils.
#include <stdio.h>int main(void){    int i, j, k, num;    for (num = 100; num < 1000; num++)    {        i = num / 100;        k = num % 10;        j = num % 100 / 10;        if (i * i * i + j * j * j + k * k * k == num)            printf("%d\n", num);    }    return 0;}

Question 3: intimacy

Assume that there are two numbers a and B. If the sum of all the factors of a is equal to B, the sum of all the factors of B is equal to a (the factor includes 1 and does not include itself), and a is not equal to B, a and B are a pair of intimacy numbers. For example, the sum of 220 and 284,284 is 220, so 220 and 284 are a pair of intimacy. Number of intimacy requests within 5000.

#include <stdio.h>#define MAXN 5000int main(void){    int i, a, b, tempa, tempb;    int fact_a[MAXN] = {0}, pa;    int fact_b[MAXN] = {0}, pb;    for (a = 1; a < 5000; a++)      //a is bigger    {        tempa = 0;        tempb = 0;        pa = 0;        pb = 0;        for (i = 1; i < (a / 2 + 1); i++)        {            if (a % i == 0)            {                fact_a[pa++] = i;                tempa += i;            }        }        if (tempa > a)                      //to ensure b > a        {            for (i = 1; i < (tempa / 2 + 1); i++)            {                if (tempa % i == 0)                {                    fact_b[pb++] = i;                    tempb += i;                }            }        }        if (tempb == a)        {            printf("%d AND %d:\n", a, tempa);            printf("%d = ", a);            for (i = 0; i < pa - 1; i++)                printf("%d + ", fact_a[i]);            printf("%d\n", fact_a[i]);            printf("%d = ", tempa);            for (i = 0; i < pb - 1; i++)                printf("%d + ", fact_b[i]);            printf("%d\n", fact_b[i]);        }    }    return 0;}
Resolution: 220 and 284 are a pair of intimacy numbers. After the order is changed, 284 and 220 cannot be counted as intimacy numbers.

4 questions: Self-defense

A self-defense number refers to a natural number in which the last few digits of a square result are equal to the number itself. For example, the square of 6 is equal to 36, and the ending number is 6, so 6 is the self-defense number. The square value of 25 is equal to 625, And the ending value is 25. Therefore, 25 is the number of guards. Program to find the number of self-defense in the specified range. The analysis is as follows:

(1) The natural idea is to calculate the square of the number n, and then compare the corresponding number of digits with n. If the number is equal, the self-defense number is found. Due to the limited storage range of computer variables, this idea cannot be calculated for excessively large integers.

(2) In order to calculate a larger self-defense number, the following ideas should be adopted. For example, if the value is 625*625, only the last three digits of the product are concerned.

1) for a single digit that is multiplied by a multiplier, multiply it by a factor of 625 and a single digit of 5;

2) In the product of ten digits multiplied by the multiplier, use the last two digits of the multiplier to multiply the ten digits 20 of the multiplier;

3) in the unit where the number of BITs is multiplied by the number of BITs, multiply the value of the last bits 5 with the number of BITs 600.

Add the accumulation of all of the above, and then take the last three digits.
#include <stdio.h>int main(void){    int a, b, t_a;    long n, m, tempm, sum, x , y;    scanf("%ld", &n);    for (m = 1; m < n; m++)    {        tempm = m;        a = 1;        b = 10;        sum = 0;        while (tempm > 0)        {            tempm /= 10;            a *= 10;        }        t_a = a;        while (t_a > 10)        {            x = m % t_a;            y = m % b - m % (b / 10);            sum = (sum + (x * y)) % a;            t_a /= 10;            b *= 10;        }        if (sum == m)            printf("%ld ", m);    }    printf("\n");    return 0;}

Question 5: Prime Number

Except 1 and itself, there is no other number of factors. Two ideas: (1) common law; (2) Eratosthenes law.

// Common law int IsPrime (int m) {int I, flag = 1; for (I = 2; I * I <m + 1; I ++) {if (m % I = 0) {flag = 0; break ;}} return flag ;}

// Merge method # include <stdio. h> # define MAXN 1000int main (void) {int Prime [MAXN + 1], I, j; for (I = 2; I <MAXN + 1; I ++) prime [I] = 1; for (I = 2; I * I <MAXN + 1; I ++) {if (Prime [I] = 1) {for (j = 2 * I; j <MAXN + 1; j ++) {if (j % I = 0) Prime [j] = 0 ;}}} for (I = 2; I <MAXN + 1; I ++) {if (Prime [I]) printf ("% d", I );} printf ("\ n"); return 0 ;}
Resolution: the common law is applicable to determining whether a number is a prime number. The limit method is applicable to finding a prime number within a specified range.


An interesting interview question (with answers ):

This question is to bring two irrelevant parts together. In fact, it should not be like that,

Interesting questions, such as top 500 interesting interview questions, where are there?

I thought about it, three. (Only personal opinions can be expressed)
Black color: 0 White: 1
Three color sensors correspond to A B C
A and B are placed in A straight line, and B is placed on the right side of the disc (anything that does not overlap with A and B, of course, on the left) the color sensor reads the data once when the color value of each sensor changes.
If the disk turns clockwise: The read data (A, C, B, note that the order is A-> C-> B) is:
100 (that is, A first captures white C as black and B as black)
110 (that is, C gets the value of white, and A and C are less than 180 degrees, it is the same as C, at this time, only B is black)
011 (in this case, the color of A changes (black), and the color of B also changes (white). C and B are white in the same color.) and so on, next, we should:
001
100 back to a cycle
At this time, take any two sets of color values, and judge the second set of color value of C and the color value of the last person who is the same, then you can determine the direction of the disk.
For example, if the color value of the second group C in 011,001 is 0, which is the same as that of the first group A, the rotation direction is A-> C, that is, clockwise.
Similarly, if the value is counter-clockwise, the data obtained is also A, C, and B.
110
100
001
011
110
Similarly, take two adjacent groups of data. For example, 110,100 determines that the value 0 of the second group of C in C and 100 is the same as the value of B in the previous group of data, that is, the rotation direction of B --> C.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.