N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大整数与int的乘法,一个50000的数组完全可以解决,看来分析问题的能力还是比较弱呀,希望能够提升分析问题的全局能力!
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
int a[50000];
while(cin>>n)
{
memset(a,0,sizeof(a));
a[0]=1;
int flag=0;
for(int i=2;i<=n;i++)
{
for(int j=0;j<=flag;j++)
a[j]*=i;
for(int j=0;j<=flag;j++)
{
if(a[j]>=10)
{
a[j+1]+=a[j]/10;
a[j]%=10;
}
}
if(a[flag+1])
{
while(a[++flag]>=10)
{
a[flag+1]+=a[flag]/10;
a[flag]%=10;
}
}
}
for(int i=flag;i>=0;i--)
cout<<a[i];
cout<<endl;
}
return 0;
}
大整数类的简单输入输出,暂时就理解这些了,希望后续能够加深搞出大整数的加减乘除!
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct Big
{
static const int BASE=;
static const int WIDTH=;
vector<int > s;
Big operator =(const string &str)
{
s.clear();
int x,len=(str.length()-)/WIDTH+;
for(int i=;i<len;i++)
{
int end=str.length()-i*WIDTH;
int start=max(,end-WIDTH);
sscanf(str.substr(start,end-start).c_str(),"%d",&x);
s.push_back(x); }
return *this; } };
istream &operator >>(istream &in,Big &b)
{string x;
in>>x;
b=x;
return in;
} ostream &operator << (ostream &out, Big &x)
{
out<<x.s.back();//防止高位不足八位
for(int i=x.s.size()-;i>=;i--)
{ char buf[];
sprintf(buf,"%08d",x.s[i]);
for(int j=;j<strlen(buf);j++)
out<<buf[j];
}
return out; }
int main()
{
Big a,b;
cin>>a>>b;
cout<<a<<" "<<b; return ; }
N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)的更多相关文章
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
- C++高精度计算(大整数类)
Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- 用Java的大整数类BigInteger来实现大整数的一些运算
关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...
- [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换
[原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...
- C++ BigInteger 大整数类模板(转)
#include <deque> #include <vector> #include <iostream> #include <string> #in ...
- hdu 1250 简单大整数加法
#include<stdio.h> #include<string.h> #define N 3100 int a[N],b[N],c[N],d[N],e[N]; int ma ...
- C++大整数类模板
参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...
- Ural 1158. Censored! 有限状态自动机+DP+大整数
Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...
随机推荐
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
- 【leetcode刷题笔记】Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 简要总结ajax工作原理及优缺点
虽然在实际的项目中使用多种ajax请求,但就其工作原理,优缺点尚未深入总结, 参考:http://www.cnblogs.com/SanMaoSpace/archive/2013/06/15/3137 ...
- 《机器学习实战第7章:利用AdaBoost元算法提高分类性能》
import numpy as np import matplotlib.pyplot as plt def loadSimpData(): dataMat = np.matrix([[1., 2.1 ...
- 通过公钥解密密文思路(256bits RSA)
256bit RSA公钥安全系数极低,只需要几分钟即可破解密文,本文综合其他文章记录了一次解密256bits RSA加密的密文的过程,仅作为备忘. 1.分解公钥,分解出n与e: 1.1使用openss ...
- etcd 安装部署
etcd 是coreos团队开发的分布式服务发现键值存储仓库. github地址: https://github.com/coreos/etcd 安装: 1.下载etcd最新版本 https://gi ...
- Linux查看文件编码格式及文件编码转换<转>
如果你需要在Linux 中操作windows下的文件 ,那么你可能会经常遇到文件 编码 转换的问题.Windows中默认的文件 格式是GBK(gb2312),而Linux 一般都是UTF-8.下面介绍 ...
- JavaWeb -- Jsp 自定义标签的使用
Jsp中不要有一行Java代码, 需要的Java代码都要封到自定义标签中. 自定义标签的作用: a. 自定义标签除了可以移除jsp页面java代码外,它也可以实现以上功能. b. 控制jsp页面某 ...
- 有若干个箱子,假设每个箱子的最大承重为 MaxW 。将货物分配装箱
今天在博客园中看到一个博问,就写了下实现代码. 问题: 有若干个箱子,假设每个箱子的最大承重为 MaxW .有一批物品,它们的重量分别为w1.w2...Wn,假设每个物品的重量都不超过箱子承重.写个算 ...
- matplotlib柱状图
import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random ...