您的位置:首页 > 其它

uva 568(数学)

2016-02-09 12:39 246 查看
题解:从1開始乘到n,由于结果仅仅要最后一位。所以每乘完一次,仅仅要保留后5位(少了值会不准确,刚開始仅仅保留了一位。结果到15就错了,保留多了int会溢出,比方3125就会出错) 和下一个数相乘,接着保留5位,注意5位没有后导零,最后取5位中最后一个不是零的就能够了。

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
int n;
long int temp;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
printf("%5d -> 1\n", n);
continue;
}
int a = 1;
long int temp1;
for (int i = 1; i <= n; i++) {
temp = (i * a) % 10;
if (temp == 0) {
temp1 = i * a;
while (temp == 0) {
temp1 = temp1 / 10;
temp = temp1 % 10;
}
a = temp1 % 100000;
}
else
a = (i * a) % 100000;
}
printf("%5d -> %ld\n", n, temp);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: