HDU1042(N!)题解

HDU1042(N!)题解

以防万一,题目原文和链接均附在文末。那么先是题目分析:

【一句话题意】

计算N的阶乘并输出。

【题目分析】

题给范围上限是10000,那么毫无疑问是大数题。之前我整理过各种大数的代码并且改了改然后所谓封装了一下,于是这道题就直接用了个类似的代码修改了一下,然后。。。交上去就对了(喂。。)。
但是作为题解,怎么着也得把做法解释清楚吧。。

以下为c语言代码(虽然是cpp提交的,也带了一堆cpp头文件),也附带了代码解释。代码很好懂,看看解释应该看得明白。还有...如果你是写java代码的。。现成的BigInteger再见慢走不送~

【算法流程】

大数乘小数:
  这里的小数指的是int范围内的小数。不管你知道不知道,我们假设你知道,大数应当用一个数组来存储,下标对应每一位的数字(下标和位数的关系可能会是或者说经常是相反的)。而大数运算则是相当于模拟小学的列式计算。那么,我们只需要按照小学那样,让大数的每一位和小数相乘,然后记录进位并加在下一位即可。
  为了方便计算,我们用int数组a存储大数,常整i存储小数(这个变量名称改改看着就舒坦了,这里不改是因为...因为....因为爱情XD),为了方便,我们采取下标从小到大对应个,十,百,千,万...的存数方式。为了方便管理位数和输出,我们另外使用一个变量digit存储管理大数现有位数。我们用变量j作为迭代器来扫位数进行模拟每一位的乘法,并用carry存储进位。(注:temp类型是int。)
于是,顺应上面的解释,我们有了这样的代码:

        for(carry = 0, j = 1; j <= digit; ++j) {
            temp = a[j - 1] * i + carry;
            a[j - 1] = temp % 10;
            carry = temp / 10;
        }
        while(carry) { //处理完毕仍需进位时,需要注意变动digit
            a[++digit - 1] = carry % 10;
            carry /= 10;
        }

我们可以这样测试上面的代码
  int a[455000] = {5,4,3,2,1};//初始化为12345,想改自己改= =
  digit = 5;//如上,五位
  i = n;//n为你要乘的数
自己封装个函数测试吧。。。。。。当然测试结果应该很科学。
修改代码:
  大数乘小数有了,那么我们循环从1乘到你要的数(最大1w)就是了。明显,我们需要:

  int a[455000] = {1};//初始化为1
  digit = 1;//1位(废话
  for(i = 2; i <= n; i++) {
    //这里放大数乘小数的代码,i为小数,a数组为大数。n为要阶乘到的数
    //由于是循环,于是每次乘的结果就是大数*2*3*4*...*n了
  }

这下明白了吧。。
上代码。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 #include <math.h>
 7
 8 using namespace std;
 9
10 void echoFactorial(int n);
11
12 int main() {
13
14     int n;
15     while(scanf("%d",&n) != EOF) {
16         echoFactorial(n);
17     }
18     return 0;
19 }
20
21 void echoFactorial(int n){
22     int carry, j;
23     int digit;
24     int temp, i;
25     int a[455000] = {1};//the express code of hometown
26     digit = 1;
27     for(i = 2; i <= n; i++) {
28         for(carry = 0, j = 1; j <= digit; ++j) {
29             temp = a[j - 1] * i + carry;
30             a[j - 1] = temp % 10;
31             carry = temp / 10;
32         }
33         while(carry) {
34             a[++digit - 1] = carry % 10;
35             carry /= 10;
36         }
37     }
38
39     for(int k = digit; k >= 1; --k)
40         printf("%d", a[k - 1]);
41     printf("\n");
42
43 }
44 /*
45 TestData(IN)
46 1
47 2
48 3
49 TestData(OUT)
50 1
51 2
52 6
53 */


这里是附带的题目信息:

题目链接:(HDU 1042)N!
题目属性:大数,模拟(竟然有说这道题要枚举的,醉,怎么可能。好吧你要枚举我一点也不介意~)
相关题目:1002、1042、1133、1250、1297、1715、1753、1865、2100
题目原文:
【Desc】Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
【In】One N in one line, process to the end of file.
【Out】For each N, output N! in one line.
【SampIn/Out】参见代码下方的注释。

时间: 2024-08-26 00:42:20

HDU1042(N!)题解的相关文章

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种

8.8联考题解

今天的T1让我怀疑我是不是在做奥赛题--这考的是什么知识点啊这个,会不会用绝对值函数? Evensgn 的债务 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Evensgn 有一群好朋友,他们经常互相借钱.假如说有三个好朋友A,B,C.A 欠 B 20 元,B 欠 C 20 元,总债务规模为 20+20=40 元.Evensgn 是个追求简约的人,他觉得这样的债务太繁杂了.他认为,上面的债务可以完全等价为 A 欠C20 元,B 既不欠别人,别人也不欠他.这样总债务规模就压缩到了 

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

2017ZZUACM省赛选拔试题部分题解----谨以纪念我这卡线滚粗的美好经历

写在前面: 其实心里有些小小的不爽又有点小小的舒畅,为啥捏?不爽当然是因为没被选拔上啦,舒畅捏则是因为没被选拔上反而让自己警醒,学长也提点很多很多."沉下去,然后一战成名"学长如是对我说,我很开心.其实这完全算不算是题解,只是我个人的一些小想法而已.而且到现在还有一题不会...让自己长点记性吧. 题目 A :聪明的田鼠 Time Limit: 1 Sec Memory Limit: 128 MB Description 田鼠MIUMIU来到了一片农田,农田可以看成是一个M*N个方格的矩

LeetCode-001题解

此题目摘自LeetCode001 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来.偶尔想起来的时候,就写出来. public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length<=1) return; nextPermutationHelp( nums,0,nums.length-1); } public void nextPermutationHelp(int []nu

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H