码迷,mamicode.com
首页 > 其他好文 > 详细

UVA-568-数论

时间:2017-05-13 22:15:52      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:printf   uva   space   names   过程   个数   int end   pen   using   

题意

输入一个n,求n!最后一个不是0的数

2x5肯定是等于10的,先把所有不是2和5的数乘起来,保留最后一位

计算过程中计算出2和5的个数

因为2*5=10,而且2的个数比5的个数多,所以最后只要把剩下的2乘进去,保留最后一位

#include<iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;

int main()
{
	freopen("d:\\1.txt", "r", stdin);
	int n;
	while (cin >> n)
	{
		int end = 1;
		int n2 = 0, n5 = 0;
		for(int i = 2; i <= n; i++)
		{
			int j = i;
			while (j % 2==0)
			{
				j = j / 2;
				n2++;
			}
			while (j % 5==0)
			{
				j = j / 5;
				n5++;
			}
			end = (end * j) % 10;
		}
		for(int i = 0; i < n2 - n5; i++)
		{
			end = (end * 2) % 10;
		}
		printf("%5d -> %d\n", n, end);
	}
	return 0;
}

  

UVA-568-数论

标签:printf   uva   space   names   过程   个数   int end   pen   using   

原文地址:http://www.cnblogs.com/shuiyonglewodezzzzz/p/6850393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!