支持大数加、减、乘,其中减法支持负数
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
//大数加,把数字倒序先,然后正常加,进位给后面的位,最后倒序输出。判断下最高位是否被进位了就行
//倒序的原因是:如果不倒序,然后for倒着加会出现最高位进位的情况进到 数组下标-1 的情况,除非你一开始就预留一个0位
//OJ: http://acm.hdu.edu.cn/showproblem.php?pid=1002
void add(char *s1, char *s2) {
int len1 = strlen(s1), len2 = strlen(s2);
int maxLen = max(len1,len2);
int a1[maxLen + 1];
int a2[maxLen + 1];
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
for (int i = 0; i < len1; ++i) {
a1[i] = s1[ len1 - i - 1] - '0';
}
for (int i = 0; i < len2; ++i) {
a2[i] = s2[ len2 - i - 1] - '0';
}
for (int i = 0; i < maxLen; ++i) {
a1[i] += a2[i];
a1[i + 1] += a1[i] / 10;
a1[i] %= 10;
// if(a1[i] > 9) {
// a1[i + 1] ++;
// a1[i] -= 10;
// }
}
if( a1[maxLen] ) {
maxLen ++;
}
for (int i = maxLen - 1; i >= 0; --i) {
printf("%d", a1[i]);
}
printf("\n");
}
//大数减(在s1 > s2的情况可用,比如5-4=1,但不能用4-5=-1)
//跟加法一样,倒序相减,借位就+10借,最后处理前导0
void sub(char *s1, char *s2) {
int len1 = strlen(s1), len2 = strlen(s2);
int maxLen = max(len1,len2);
int a1[maxLen + 1];
int a2[maxLen + 1];
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
for (int i = 0; i < len1; ++i) {
a1[i] = s1[ len1 - i - 1] - '0';
}
for (int i = 0; i < len2; ++i) {
a2[i] = s2[ len2 - i - 1] - '0';
}
for (int i = 0; i < maxLen; ++i) {
if(a1[i] - a2[i] < 0) {
a1[i] = a1[i] + 10 - a2[i];
a1[i + 1] --;
} else {
a1[i] -= a2[i];
}
}
while(maxLen > 1 && a1[maxLen-1] == 0 ) { //去前导0
maxLen --;
}
for (int i = maxLen - 1; i >= 0; --i) {
printf("%d", a1[i]);
}
printf("\n");
}
//乘法,不倒序,直接正着乘 ,但进位需单独提出来处理,以及处理 0*103 这种去除前置0
// 12
// 34
//=
// 36
// 48
// 408
void mul(char *s1, char *s2) {
int len1 = strlen(s1), len2 = strlen(s2);
int maxLen = len1 + len2 -1; //长度最多为 len1 + len2 - 1
int ans[maxLen + 1];
memset(ans,0,sizeof(ans));
for (int i = 0; i < len1; ++i) {
for (int j = 0; j < len2; ++j) {
ans[ i+j ] += (s1[i]-'0') * (s2[j]-'0');
}
}
//分开出来倒序i处理,而非在上面 i+j 进位处理是为了避免 i+j-1被多次进位超10,但被跳过
for (int i = maxLen ; i >=1; i--) {
if(ans[i] > 9) {
ans[i-1] += ans[i]/10;
ans[i] %= 10;
}
}
//找第一个非0起点,处理0 * 103 = 000 的情况
int start = 0;
while(start != maxLen-1 && ans[start] == 0) {
start ++;
}
for (int i = start; i < maxLen; ++i) {
printf("%d", ans[i]);
}
printf("\n");
}
//比较两个正数的大小,用于减法
int compareSize(char *s1, char *s2) {
int len1 = strlen(s1);
int len2 = strlen(s2);
if(len1 > len2) return 1;
if(len1 < len2) return -1;
for(int i=0;i < len1;i++){
if(s1[i] > s2[i]){
return 1;
}
if(s1[i] < s2[i]){
return -1;
}
}
return 0;
}
//大数减复杂版,支持负数和 s1 < s2 的情况
void sub_complex(char *s1, char *s2) {
if( s1[0] == '-' && s2[0] == '-'){ //-1-(-2) = 2 - 1
sub(s2 + 1,s1 + 1);
return;
}
if( s1[0] == '-' && s2[0] != '-'){ //-1-2 = -(1+2)
printf("-");add(s1 + 1,s2);
return ;
}
if( s1[0] != '-' && s2[0] == '-'){ //1-(-2) = 1+2
add(s1,s2 + 1);
return ;
}
int diff = compareSize(s1,s2);
if(diff == 0){ //两个正数 相等为0
printf("0\n");return;
}
if( diff < 0 ){ //1-2 = -(2-1)
printf("-");
sub(s2,s1);
return;
}
sub(s1,s2); //正常的 2-1
}
int main() {
// duipai_mul();
// freopen("input.txt","r",stdin);
char s1[maxn], s2[maxn];
while( scanf("%s%s", s1, s2) != EOF){
// add(s1, s2);
// sub(s1, s2);
// mul(s1, s2);
// printf("%s - %s = ", s1, s2);
sub_complex(s1,s2);
}
return 0;
}
//*******************************************************/
//******************以下为测试 和 对拍代码 **************/
//*******************************************************/
// 1 ~ maxN 的普通数字乘法表
void normal_mul(int maxN){
freopen("output_1.txt","w",stdout);
for (int i = 0; i <= maxN; ++i) {
for (int j = 0; j <= maxN; ++j) {
printf("%d", i*j);
printf("\n");
}
}
exit(0);
}
// 1 ~ maxN 的大数乘法表
void my_mul(int maxN){
freopen("output_2.txt","w",stdout);
char s1[maxn], s2[maxn];
for (int i = 0; i <= maxN; ++i) {
for (int j = 0; j <= maxN; ++j) {
sprintf(s1, "%d", i);
sprintf(s2, "%d", j);
mul(s1, s2);
}
}
exit(0);
}
//乘法对拍
int duipai_mul() {
int maxN = 100;
// normal_mul(maxN);
my_mul(maxN);
}
// hdu 1002的输入格式
int main_hdu_1002() {
// freopen("input.txt","r",stdin);
char s1[maxn], s2[maxn];
int t;
scanf("%d", &t);
for (int te = 1; te <= t; ++te) {
scanf("%s%s", s1, s2);
printf("Case %d:\n", te );
printf("%s + %s = ", s1, s2);
add(s1, s2);
// printf("%s - %s = ", s1, s2);
// sub(s1, s2);
// printf("%s * %s = ", s1, s2);
// mul(s1, s2);
if (te != t) {
printf("\n");
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
void add(string s1, string s2) {
//在比较短的那个串 后面补0
while (s1.length()<s2.length())s1 = '0' + s1;
while (s2.length()<s1.length())s2 = '0' + s2;
//两个串首补0,预留给最高位进位
s1 = '0' + s1;
s2 = '0' + s2;
int maxLen = max(s1.length(),s2.length());
//倒序相加
for (int i = maxLen - 1; i >= 0; i--) {
int num1 = s1[i] - '0';
int num2 = s2[i] - '0';
if( num1 + num2 > 9) {
s1[i] = num1 + num2 - 10 + '0';
s1[i - 1] ++;
} else {
s1[i] = num1 + num2 + '0';
}
}
//如果预留的最高进位仍是0,说明没进位,删掉
if (s1[0] == '0') {
s1.erase(0, 1);
}
cout<<s1<<endl;
}
int main() {
// freopen("input.txt","r",stdin);
string s1,s2;
int t;
cin>>t;
for (int te = 1; te <= t; ++te) {
cin>>s1>>s2;
cout<<"Case "<<te<<":"<<endl;
cout<<s1<<" + "<<s2<<" = ";
add(s1, s2);
if (te != t) {
cout<<endl;
}
}
return 0;
}
1W以下阶乘:
#include<stdio.h>
#include<string.h>
#define MaxLen 40000 //1W的阶乘位数为35660
int a[MaxLen];
int main()
{
int n,i,j,s,carry;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
a[0]=1;
for(i=2;i<=n;i++)
{ //逐位相乘
carry=0;
for(j=0;j<MaxLen;j++)
{ //carry为进位
s = i * a[j] + carry;
carry = s /10;
a[j] = s % 10 ;
} //逆序存放:因为carry是上次的进位
}
for(i=MaxLen-1;i>=0;i--) //去前导0
if(a[i]!=0)
break;
for(;i>=0;i--)
printf("%d",a[i]);
putchar('\n');
}
return 0;
}
C++大数模板(不支持除法,据说是白书里的)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 1200;
struct bign{
int len, s[maxn];
bign() {
memset(s, 0, sizeof(s));
len = 1;
}
bign(int num) {
*this = num;
}
bign(const char* num) {
*this = num;
}
bign operator = (int num) {
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char* num) {
len = strlen(num);
for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
return *this;
}
string str() const {
string res = "";
for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
if(res == "") res = "0";
return res;
}
bign operator + (const bign& b) const{
bign c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
}
void clean() {
while(len > 1 && !s[len-1]) len--;
}
bign operator * (const bign& b) {
bign c; c.len = len + b.len;
for(int i = 0; i < len; i++)
for(int j = 0; j < b.len; j++)
c.s[i+j] += s[i] * b.s[j];
for(int i = 0; i < c.len-1; i++){
c.s[i+1] += c.s[i] / 10;
c.s[i] %= 10;
}
c.clean();
return c;
}
bign operator - (const bign& b) {
bign c; c.len = 0;
for(int i = 0, g = 0; i < len; i++) {
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= 0) g = 0;
else {
g = 1;
x += 10;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bool operator < (const bign& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
bool operator > (const bign& b) const{
return b < *this;
}
bool operator <= (const bign& b) {
return !(b > *this);
}
bool operator == (const bign& b) {
return !(b < *this) && !(*this < b);
}
bign operator += (const bign& b) {
*this = *this + b;
return *this;
}
};
istream& operator >> (istream &in, bign& x) {
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream &out, const bign& x) {
out << x.str();
return out;
}
int main() {
// freopen("in.txt","r",stdin);
int n;
bign a,b,c;
cin>>n;
for(int i=1;i<=n;i++)
{
cin >> a >> b;
c = a + b;
cout<<"Case "<<i<<":"<<endl;
cout << a << " + "<<b<<" = "<<c<<endl;
if(i!=n)
cout<<endl;
}
return 0;
}