The basic algorithm of C language 29-the last three digits of an integer in any order (accuracy problem)

Source: Internet
Author: User

Accuracy problem!
/*
==================================================================
Topic:
Find the last three digits of any number of integers! The last three digits of the 72-time 347 are 241.
==================================================================
*/
#include <stdio.h>
#include <iomanip>
Main ()
{
int x,y,p,n;
printf ("Description: Base x, power Y, please enter: \ n");
printf ("x=");
scanf ("%d", &x);
printf ("y=");
scanf ("%d", &y);
P=1;
for (n=1;n<=y;n++)
{
p=p*x%1000; The key is to grasp the meaning of this sentence, the last three bits are always only the last three bits of the multiplier related, so only the last three bits, so as not to overflow!
printf ("%3d:p=%3d \ n", n,p);//If the output process is not required, this line of code is not required
}
printf ("%d of%d of the last three bits is:%3d\n", x,y,p);
}




/*
====================================================================
Evaluation:
or the application of random numbers, but this is used to deal with a relatively long number, the computer will use this method
Handle your own precision! is to keep only the precision-related parts at a time.
=====================================================================
*/





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The basic algorithm of C language 29-the last three digits of an integer in any order (accuracy problem)

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.