高精度三连击(HDU 1002+POJ 1503+HDU 2178)

来源:互联网 发布:数学天赋知乎 编辑:程序博客网 时间:2024/05/12 16:45

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 305199    Accepted Submission(s): 58972


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 

Sample Input
21 2112233445566778899 998877665544332211
 

Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
 

Author
Ignatius.L
 

Recommend


题意:高精度输出A+B





#include<string>    #include<iostream>    #include<iosfwd>    #include<cmath>    #include<cstring>    #include<stdlib.h>    #include<stdio.h>    #include<cstring>    #define MAX_L 2005 //大整数的位数  using namespace std;class bign{public:int len, s[MAX_L];bign();bign(const char*);bign(int);bool sign;string toStr() const;friend istream& operator>>(istream &, bign &);friend ostream& operator<<(ostream &, bign &);bign operator=(const char*);bign operator=(int);bign operator=(const string);bool operator>(const bign &) const;bool operator>=(const bign &) const;bool operator<(const bign &) const;bool operator<=(const bign &) const;bool operator==(const bign &) const;bool operator!=(const bign &) const;bign operator+(const bign &) const;bign operator++();bign operator++(int);bign operator+=(const bign&);bign operator-(const bign &) const;bign operator--();bign operator--(int);bign operator-=(const bign&);bign operator*(const bign &)const;bign operator*(const int num)const;bign operator*=(const bign&);bign operator/(const bign&)const;bign operator/=(const bign&);bign operator%(const bign&)const;bign factorial()const;bign Sqrt()const;bign pow(const bign&)const;void clean();~bign();};#define max(a,b) a>b ? a : b    #define min(a,b) a<b ? a : b    bign::bign(){memset(s, 0, sizeof(s));len = 1;sign = 1;}bign::bign(const char *num){*this = num;}bign::bign(int num){*this = num;}string bign::toStr() const{string res;res = "";for (int i = 0; i < len; i++)res = (char)(s[i] + '0') + res;if (res == "")res = "0";if (!sign&&res != "0")res = "-" + res;return res;}istream &operator>>(istream &in, bign &num){string str;in >> str;num = str;return in;}ostream &operator<<(ostream &out, bign &num){out << num.toStr();return out;}bign bign::operator=(const char *num){memset(s, 0, sizeof(s));char a[MAX_L] = "";if (num[0] != '-')strcpy(a, num);elsefor (int i = 1; i < strlen(num); i++)a[i - 1] = num[i];sign = !(num[0] == '-');len = strlen(a);for (int i = 0; i < strlen(a); i++)s[i] = a[len - i - 1] - 48;return *this;}bign bign::operator=(int num){if (num < 0)sign = 0, num = -num;elsesign = 1;char temp[MAX_L];sprintf(temp, "%d", num);*this = temp;return *this;}bign bign::operator=(const string num){const char *tmp;tmp = num.c_str();*this = tmp;return *this;}bool bign::operator<(const bign &num) const{if (sign^num.sign)return num.sign;if (len != num.len)return len < num.len;for (int i = len - 1; i >= 0; i--)if (s[i] != num.s[i])return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));return !sign;}bool bign::operator>(const bign&num)const{return num < *this;}bool bign::operator<=(const bign&num)const{return !(*this>num);}bool bign::operator>=(const bign&num)const{return !(*this<num);}bool bign::operator!=(const bign&num)const{return *this > num || *this < num;}bool bign::operator==(const bign&num)const{return !(num != *this);}bign bign::operator+(const bign &num) const{if (sign^num.sign){bign tmp = sign ? num : *this;tmp.sign = 1;return sign ? *this - tmp : num - tmp;}bign result;result.len = 0;int temp = 0;for (int i = 0; temp || i < (max(len, num.len)); i++){int t = s[i] + num.s[i] + temp;result.s[result.len++] = t % 10;temp = t / 10;}result.sign = sign;return result;}bign bign::operator++(){*this = *this + 1;return *this;}bign bign::operator++(int){bign old = *this;++(*this);return old;}bign bign::operator+=(const bign &num){*this = *this + num;return *this;}bign bign::operator-(const bign &num) const{bign b = num, a = *this;if (!num.sign && !sign){b.sign = 1;a.sign = 1;return b - a;}if (!b.sign){b.sign = 1;return a + b;}if (!a.sign){a.sign = 1;b = bign(0) - (a + b);return b;}if (a<b){bign c = (b - a);c.sign = false;return c;}bign result;result.len = 0;for (int i = 0, g = 0; i < a.len; i++){int x = a.s[i] - g;if (i < b.len) x -= b.s[i];if (x >= 0) g = 0;else{g = 1;x += 10;}result.s[result.len++] = x;}result.clean();return result;}bign bign::operator * (const bign &num)const{bign result;result.len = len + num.len;for (int i = 0; i < len; i++)for (int j = 0; j < num.len; j++)result.s[i + j] += s[i] * num.s[j];for (int i = 0; i < result.len; i++){result.s[i + 1] += result.s[i] / 10;result.s[i] %= 10;}result.clean();result.sign = !(sign^num.sign);return result;}bign bign::operator*(const int num)const{bign x = num;bign z = *this;return x*z;}bign bign::operator*=(const bign&num){*this = *this * num;return *this;}bign bign::operator /(const bign&num)const{bign ans;ans.len = len - num.len + 1;if (ans.len < 0){ans.len = 1;return ans;}bign divisor = *this, divid = num;divisor.sign = divid.sign = 1;int k = ans.len - 1;int j = len - 1;while (k >= 0){while (divisor.s[j] == 0) j--;if (k > j) k = j;char z[MAX_L];memset(z, 0, sizeof(z));for (int i = j; i >= k; i--)z[j - i] = divisor.s[i] + '0';bign dividend = z;if (dividend < divid) { k--; continue; }int key = 0;while (divid*key <= dividend) key++;key--;ans.s[k] = key;bign temp = divid*key;for (int i = 0; i < k; i++)temp = temp * 10;divisor = divisor - temp;k--;}ans.clean();ans.sign = !(sign^num.sign);return ans;}bign bign::operator/=(const bign&num){*this = *this / num;return *this;}bign bign::operator%(const bign& num)const{bign a = *this, b = num;a.sign = b.sign = 1;bign result, temp = a / b*b;result = a - temp;result.sign = sign;return result;}bign bign::pow(const bign& num)const{bign result = 1;for (bign i = 0; i < num; i++)result = result*(*this);return result;}bign bign::factorial()const{bign result = 1;for (bign i = 1; i <= *this; i++)result *= i;return result;}void bign::clean(){if (len == 0) len++;while (len > 1 && s[len - 1] == '\0')len--;}bign bign::Sqrt()const{if (*this<0)return -1;if (*this <= 1)return *this;bign l = 0, r = *this, mid;while (r - l>1){mid = (l + r) / 2;if (mid*mid>*this)r = mid;elsel = mid;}return l;}bign::~bign(){}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);#endifchar aa[MAX_L], bb[MAX_L];bign a, b;int t,cas=0;scanf("%d", &t);while (t--){scanf("%s%s", aa, bb);a = aa;b = bb;if (cas)puts("");printf("Case %d:\n%s + %s = %s\n",++cas,aa,bb ,(a + b).toStr().c_str());}return 0;}




猜数字

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5691    Accepted Submission(s): 3871


Problem Description
A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" 。 
问B猜n次可以猜到的最大数。 
 

Input
第1行是整数T,表示有T组数据,下面有T行 
每行一个整数n (1 ≤ n ≤ 30) 
 

Output
猜n次可以猜到的最大数
 

Sample Input
213
 

Sample Output
17
 

Author
Zhousc
 

Source
ECJTU 2008 Summer Contest
 

Recommend
lcy
 



这题不是高精度。。。。。。直接输出2^n-1即可


#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(1e5)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;char s[N];int main(){    int t;scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        printf("%d\n",(1<<n)-1);    }    return 0;}



Integer Inquiry
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 32571 Accepted: 12749

Description

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output

370370367037037036703703703670

Source

East Central North America 1996


题意:数字累加,0结束输入


#include<string>    #include<iostream>    #include<iosfwd>    #include<cmath>    #include<cstring>    #include<stdlib.h>    #include<stdio.h>    #include<cstring>    #define MAX_L 2005 //大整数的位数  using namespace std;class bign{public:int len, s[MAX_L];bign();bign(const char*);bign(int);bool sign;string toStr() const;friend istream& operator>>(istream &, bign &);friend ostream& operator<<(ostream &, bign &);bign operator=(const char*);bign operator=(int);bign operator=(const string);bool operator>(const bign &) const;bool operator>=(const bign &) const;bool operator<(const bign &) const;bool operator<=(const bign &) const;bool operator==(const bign &) const;bool operator!=(const bign &) const;bign operator+(const bign &) const;bign operator++();bign operator++(int);bign operator+=(const bign&);bign operator-(const bign &) const;bign operator--();bign operator--(int);bign operator-=(const bign&);bign operator*(const bign &)const;bign operator*(const int num)const;bign operator*=(const bign&);bign operator/(const bign&)const;bign operator/=(const bign&);bign operator%(const bign&)const;bign factorial()const;bign Sqrt()const;bign pow(const bign&)const;void clean();~bign();};#define max(a,b) a>b ? a : b    #define min(a,b) a<b ? a : b    bign::bign(){memset(s, 0, sizeof(s));len = 1;sign = 1;}bign::bign(const char *num){*this = num;}bign::bign(int num){*this = num;}string bign::toStr() const{string res;res = "";for (int i = 0; i < len; i++)res = (char)(s[i] + '0') + res;if (res == "")res = "0";if (!sign&&res != "0")res = "-" + res;return res;}istream &operator>>(istream &in, bign &num){string str;in >> str;num = str;return in;}ostream &operator<<(ostream &out, bign &num){out << num.toStr();return out;}bign bign::operator=(const char *num){memset(s, 0, sizeof(s));char a[MAX_L] = "";if (num[0] != '-')strcpy(a, num);elsefor (int i = 1; i < strlen(num); i++)a[i - 1] = num[i];sign = !(num[0] == '-');len = strlen(a);for (int i = 0; i < strlen(a); i++)s[i] = a[len - i - 1] - 48;return *this;}bign bign::operator=(int num){if (num < 0)sign = 0, num = -num;elsesign = 1;char temp[MAX_L];sprintf(temp, "%d", num);*this = temp;return *this;}bign bign::operator=(const string num){const char *tmp;tmp = num.c_str();*this = tmp;return *this;}bool bign::operator<(const bign &num) const{if (sign^num.sign)return num.sign;if (len != num.len)return len < num.len;for (int i = len - 1; i >= 0; i--)if (s[i] != num.s[i])return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));return !sign;}bool bign::operator>(const bign&num)const{return num < *this;}bool bign::operator<=(const bign&num)const{return !(*this>num);}bool bign::operator>=(const bign&num)const{return !(*this<num);}bool bign::operator!=(const bign&num)const{return *this > num || *this < num;}bool bign::operator==(const bign&num)const{return !(num != *this);}bign bign::operator+(const bign &num) const{if (sign^num.sign){bign tmp = sign ? num : *this;tmp.sign = 1;return sign ? *this - tmp : num - tmp;}bign result;result.len = 0;int temp = 0;for (int i = 0; temp || i < (max(len, num.len)); i++){int t = s[i] + num.s[i] + temp;result.s[result.len++] = t % 10;temp = t / 10;}result.sign = sign;return result;}bign bign::operator++(){*this = *this + 1;return *this;}bign bign::operator++(int){bign old = *this;++(*this);return old;}bign bign::operator+=(const bign &num){*this = *this + num;return *this;}bign bign::operator-(const bign &num) const{bign b = num, a = *this;if (!num.sign && !sign){b.sign = 1;a.sign = 1;return b - a;}if (!b.sign){b.sign = 1;return a + b;}if (!a.sign){a.sign = 1;b = bign(0) - (a + b);return b;}if (a<b){bign c = (b - a);c.sign = false;return c;}bign result;result.len = 0;for (int i = 0, g = 0; i < a.len; i++){int x = a.s[i] - g;if (i < b.len) x -= b.s[i];if (x >= 0) g = 0;else{g = 1;x += 10;}result.s[result.len++] = x;}result.clean();return result;}bign bign::operator * (const bign &num)const{bign result;result.len = len + num.len;for (int i = 0; i < len; i++)for (int j = 0; j < num.len; j++)result.s[i + j] += s[i] * num.s[j];for (int i = 0; i < result.len; i++){result.s[i + 1] += result.s[i] / 10;result.s[i] %= 10;}result.clean();result.sign = !(sign^num.sign);return result;}bign bign::operator*(const int num)const{bign x = num;bign z = *this;return x*z;}bign bign::operator*=(const bign&num){*this = *this * num;return *this;}bign bign::operator /(const bign&num)const{bign ans;ans.len = len - num.len + 1;if (ans.len < 0){ans.len = 1;return ans;}bign divisor = *this, divid = num;divisor.sign = divid.sign = 1;int k = ans.len - 1;int j = len - 1;while (k >= 0){while (divisor.s[j] == 0) j--;if (k > j) k = j;char z[MAX_L];memset(z, 0, sizeof(z));for (int i = j; i >= k; i--)z[j - i] = divisor.s[i] + '0';bign dividend = z;if (dividend < divid) { k--; continue; }int key = 0;while (divid*key <= dividend) key++;key--;ans.s[k] = key;bign temp = divid*key;for (int i = 0; i < k; i++)temp = temp * 10;divisor = divisor - temp;k--;}ans.clean();ans.sign = !(sign^num.sign);return ans;}bign bign::operator/=(const bign&num){*this = *this / num;return *this;}bign bign::operator%(const bign& num)const{bign a = *this, b = num;a.sign = b.sign = 1;bign result, temp = a / b*b;result = a - temp;result.sign = sign;return result;}bign bign::pow(const bign& num)const{bign result = 1;for (bign i = 0; i < num; i++)result = result*(*this);return result;}bign bign::factorial()const{bign result = 1;for (bign i = 1; i <= *this; i++)result *= i;return result;}void bign::clean(){if (len == 0) len++;while (len > 1 && s[len - 1] == '\0')len--;}bign bign::Sqrt()const{if (*this<0)return -1;if (*this <= 1)return *this;bign l = 0, r = *this, mid;while (r - l>1){mid = (l + r) / 2;if (mid*mid>*this)r = mid;elsel = mid;}return l;}bign::~bign(){}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);#endifchar aa[MAX_L], bb[MAX_L];bign a, b="0";while (~scanf("%s", aa)){if (aa[0] == '0'&&strlen(aa) == 1)break;a = aa;b += a;}printf("%s\n", b.toStr().c_str());return 0;}




0 0
原创粉丝点击