大整数的加减乘除:
大整数加法,openjudge 2981
void add(string &s1, string &s2, string &res)
{
if (s1.size() < s2.size()){
s1.insert(0, s2.size() - s1.size(), '0');
}
else {
s2.insert(0, s1.size() - s2.size(), '0');
}
string::reverse_iterator iter1 = s1.rbegin();
string::reverse_iterator iter2 = s2.rbegin();
int carry = 0;
while(iter1 != s1.rend()){
int sum = *iter1 - '0' + *iter2 - '0' + carry;
res.insert(0, 1, sum % 10 + '0');
carry = sum / 10;
iter1++;
iter2++;
}
if (carry > 0)
res.insert(0, 1, carry + '0');
while(res[0] == '0' && res.size() > 1){
res.erase(0, 1);
}
}
大整数减法 Openjudge 2736
void sub(string &s1, string &s2, string &res)
{
if (s1.size() < s2.size()){
s1.insert(0, s2.size() - s1.size(), '0');
}
else {
s2.insert(0, s1.size() - s2.size(), '0');
}
string::reverse_iterator iter1 = s1.rbegin();
string::reverse_iterator iter2 = s2.rbegin();
int borrow = 0;
while(iter1 != s1.rend()){
int tmp = (*iter1 - '0') - (*iter2 - '0') - borrow;
if (tmp < 0) {
tmp += 10;
borrow = 1;
}
else {
borrow = 0;
}
res.insert(0, 1, tmp + '0');
iter1++;
iter2++;
}
while(res[0] == '0' && res.size() > 1){
res.erase(0, 1);
}
}
大整数乘法 openjudge 2980
void mul(string &s1, string &s2, string &res)
{
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
res.insert(0, s1.size() + s2.size(), '0');
int i, j;
for(i=0;i<s1.size();i++){
int carry = 0;
for(j=0;j<s2.size();j++){
int tmp = res[i+j] - '0' + (s1[i] - '0')*(s2[j] - '0') + carry;
res[i+j] = tmp % 10 + '0';
carry = tmp / 10;
}
if(carry){
while(carry){
int tmp = res[i+j] - '0' + carry;
res[i+j] = tmp % 10 + '0';
carry = tmp / 10;
j++;
}
}
}
reverse(res.begin(),res.end());
while(res[0] == '0' && res.size() > 1){
res.erase(0, 1);
}
}
大整数除法 2737
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_LEN 110
void sub(char *s1, char *s2, char *res)
{
char *p1 = s1 + strlen(s1) - 1;
char *p2 = s2 + strlen(s2) - 1;
char ans[MAX_LEN];
int i = 0;
int borrow = 0;
while(p1 >= s1 && p2 >= s2){
int tmp = (*p1 - '0') - (*p2 - '0') - borrow;
if (tmp < 0) {
tmp += 10;
borrow = 1;
}
else {
borrow = 0;
}
ans[i++] = tmp + '0';
p1--;
p2--;
}
while(p1 >= s1){
int tmp = (*p1 - '0') - borrow;
if (tmp < 0) {
tmp += 10;
borrow = 1;
}
else {
borrow = 0;
}
ans[i++] = tmp + '0';
p1--;
}
ans[i] = '\0';
for(char *p = ans + i - 1;*p == '0';p--)
*p = '\0';
reverse(ans, ans+strlen(ans));
strcpy(res, ans);
}
void add(char *s1, char *s2, char *res)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
char *p1, *p2;
char temp[MAX_LEN] = "";
char ans[MAX_LEN];
int i = 0;
if (len1 < len2){
memset(temp, '0', len2 - len1);
strcat(temp, s1);
p1 = temp + strlen(temp) - 1;
p2 = s2 + strlen(s2) - 1;
}
else {
memset(temp, '0', len1 - len2);
strcat(temp, s2);
p1 = temp + strlen(temp) - 1;
p2 = s1 + strlen(s1) - 1;
}
int carry = 0;
while(p1 >= temp){
int sum = *p1 - '0' + *p2 - '0' + carry;
ans[i++] = sum % 10 + '0';
carry = sum / 10;
p1--;
p2--;
}
if (carry > 0)
ans[i++] = carry + '0';
ans[i] = '\0';
reverse(ans, ans + strlen(ans));
strcpy(res, ans);
}
bool greaterThanOrEqualTo(char *a, char *b)
{
size_t lena = strlen(a);
size_t lenb = strlen(b);
if(lena > lenb || (lena == lenb && strcmp(a, b) >= 0))
return true;
return false;
}
int main()
{
int n;
scanf("%d", &n);
while(n--){
char res[MAX_LEN] = "0";
char a[MAX_LEN], b[MAX_LEN];
int i;
scanf("%s%s", a, b);
while(a[0] != '0' && greaterThanOrEqualTo(a, b)){
if (strlen(a) == strlen(b)){
while(greaterThanOrEqualTo(a, b)){
add(res, "1", res);
sub(a, b, a);
}
}
else {
char tb[MAX_LEN];
strcpy(tb, b);
char pw[MAX_LEN] = "1";
size_t len;
if (strncmp(a, b, strlen(b)) < 0)
len = strlen(a) - strlen(b) - 1;
else
len = strlen(a) - strlen(b);
for(i=1;i<=len;i++){
pw[i] = '0';
}
pw[i] = '\0';
for(i=0;i<len;i++){
tb[strlen(b) + i] = '0';
}
tb[strlen(b) + i] = '\0';
while(greaterThanOrEqualTo(a, tb)){
add(res, pw, res);
sub(a, tb, a);
}
}
}
printf("%s\n", res);
}
}