Example 2-1 AABB 2-2 3n + 1

Source: Internet
Author: User

Example 2-1 AABB

Output all the four-digit full records in the form of AABB (that is, the first two digits are equal, and the last two digits are also equal)

#include <stdio.h>#include <stdlib.h>#include <math.h> int main(int argc, char *argv[]){  int i, j, n;  double m;  for(i = 1; i <= 9; i++)     for(j = 0; j <= 9; j++)     {        n = i*1100 + j*11;        //n = (i*10+i)*100 + j*10 + j;        m = sqrt(n);        if(floor(m+0.5) == m) printf("%d\n", n);     }  system("PAUSE");  return 0;}int main(int argc, char *argv[]){  int x, y;  for(x = 33; x*x <= 9999; x++)  {     y = x*x;     if(y/1000 == y/100%10 && y/10%10 == y%10)        printf("%d\n", y);  }  system("PAUSE");  return 0;}
Conclusion: one group of Reverse Thinking solves the same problem

2. Use the variable n = A * 1100 + B * 11 to store four digits.

3. There will be errors in floating point operations. When the floating point error is performed, the floating point error such as floor (m + 0.5) = m should be taken into account.

Example 2-2 3n + 1 Conjecture: For any natural number N greater than 1, if n is an odd number, convert n to 3N + 1, otherwise, after several such transformations, the value of N must be changed to 1. for example, 3-> 10-> 5-> 16-> 8-> 4-> 2-> 1, input N, and output the number of transformations. N ≤ 10 ^ 9. sample input: 3 sample output: 7
#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){  unsigned  n, count = 0;  scanf("%d", &n);  while(n > 1)  {     if(n % 2 == 1)  { n = n + (n+1)/2; count += 2; continue;}     else n >>= 1 ;     count++;  }  printf("%d\n", count);    system("PAUSE");  return 0;}
Conclusion: 1 3N + 1 will overflow
2. A temporary solution is: Because N is an odd number, 3 * n + 1 must be an even number. Next, divide it by 2. if two operations are performed together, this problem can be mitigated to some extent. If you are interested, try it. Program features should be good at discovery.
3 divided by 2, right shift operation

4 (3N + 1)/2 is better than writing N + (n + 1)/2 to reduce the possibility of overflow.

Example 2-3 factorial
Input N, calculate S = 1! + 2! + 3! + ...... + N! The last 6 bits (excluding leading 0), N ≤ 10 ^ 6

#include <cstdlib>#include <stdio.h>int main(){    const int MOD = 1000000;    int n;    long long sum = 0, tem = 1;    scanf("%d",&n);        for (int i=1; i<=n; i++)    {        tem = tem*i%MOD ;        sum = (sum+tem)%MOD;    }    printf("%d\n",sum);           system("PAUSE");    return EXIT_SUCCESS;}



Conclusion: 1. Arithmetic overflow is solved by MoD division in each step.
2. Characteristics of this question. When n> 25, the results will remain unchanged. Good at discovering the characteristics of this question

 

Example 2-1 AABB 2-2 3n + 1

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.