题目:输入m和n,m和n为正整数,输出m/n,m%n的值。(1<=m<=10的100次方,1<=n<=10的9次方)
思路很简单 我直接复制粘贴啦
举个例子:>> 54367256 / 97, 54367256 % 97
我们这样模拟:
str1 - str2 = str1
54367256 - 9700000 = 44667256 (在str2末尾填充0,让str1,str2相差一位) ,相差 100000 倍
44667256 - 9700000 = 34967256 ,相差 100000 倍
34967256 - 9700000 = 25267256
25267256 - 9700000 = 15567256
15567256 - 9700000 = 5867256
5867256 - 970000 = 4897256 ,相差 10000 倍
4897256 - 970000 = 3927256
3927256 - 970000 = 2957256
2957256 - 970000 = 1987256
1987256 - 970000 = 1017256
1017256 - 970000 = 47256
47256 - 9700 = 37556 ,相差 100 倍
37556 - 9700 = 27856
27856 - 9700 = 18156
18156 - 9700 = 8456
8456 - 970 = 7486
7486 - 970 = 6516
6516 - 970 = 5546
5546 - 970 = 4576
4576 - 970 = 3606
3606 - 970 = 2636
2636 - 970 = 1666
1666 - 970 = 696
696 - 97 = 599 ,相差 1 倍
599 - 97 = 502
502 - 97 = 405
405 - 97 = 308
308 - 97 = 211
211 - 97 = 114 ,相差 1 倍
114 - 97 = 17 (此时:str1 < strt,循环结束)
把相差的倍数全部加起来,就是商
最后的那个就是余数。
接下来是代码
#include <iostream>
#include <cstring>
using namespace std;
void subb(char *str1, int i) //借位函数
{
if(str1[i-1] <= '0')
{
str1[i] += 10;
str1[i-1]--;
subb(str1, i-1);
}
else
{
str1[i] += 10;
str1[i-1]--;
}
}
void subtract(char *str1, char *str2) //减法函数
{
int i,j;
i = strlen(str1)-1;
for(j=strlen(str2)-1; j>=0; j--, i--)
{
if(str1[i] < str2[j])
{
subb(str1, i);
}
str1[i] = str1[i] - str2[j] + '0';
}
for(i=0; i<strlen(str1); i++)
{
if(str1[i] == '0')
;
else
break;
}
for(j=0; j < strlen(str1); j++)
{
str1[j] = str1[j+i];
}
if(strlen(str1)==0)
{
str1[0] = '0';
str1[1] = '\0';
}
}
void carry(char *str3, int i) //进位
{
if(str3[i] > '9')
{
str3[i] = str3[i] - 10;
str3[i-1] = str3[i-1] + 1;
carry(str3, i-1);
}
}
void addition(char *str3, char *str4) //加法函数
{
int i, j, gap;
char temp;
i = strlen(str3);
j = strlen(str4);
if(i<j)
gap = j - i + 1;
else
gap = 1;
for(i--; i>=0; i--)
str3[i+gap] = str3[i];
memset(str3, '0', gap);
i = strlen(str3);
j = strlen(str4);
for(j--,i--; j>=0; j--,i--)
{
str3[i] = str3[i] + str4[j] - '0';
if(str3[i] > '9')
{
// str3[i] = str3[i] - 10;
carry(str3, i);
}
}
if(str3[0] == '0')
{
for(i=1; str3[i] != '\0'; i++)
str3[i-1] = str3[i];
str3[i-1] = '\0';
}
}
int relation(char *str1, char *str2) //大小比较函数
{
int i,j,k;
i = strlen(str1);
j = strlen(str2);
if(i > j)
return 1;
else if(i == j)
{
for(k=0; k<i; k++)
{
if(str1[k] > str2[k])
return 1;
else if(str1[k] < str2[k])
return -1;
}
return 0;
}
else
return -1;
}
int main()
{
char str1[101], str2[101], str3[101], str4[101];
memset(str1, '\0', 101);
memset(str2, '\0', 101);
memset(str3, '\0', 101);
memset(str4, '\0', 101);
str3[0] = '0';
str3[1] = '\0';
str4[0] = '0';
str4[1] = '\0';
cin >> str1;
cin >> str2;
int i=strlen(str1),j= strlen(str2),k=strlen(str2),l=0;
while(relation(str1,str2) >= 0)
{
i=strlen(str1);
str4[0] = '1';
str4[1] = '\0';
if(i-1 > k)
{
for(j=k,l=1; j < i-1; j++,l++)
{
str2[j] = '0';
str4[l] = '0';
}
str4[l] = '\0';
str2[j] = '\0';
subtract(str1, str2);
str2[k] = '\0';
}
else
{
subtract(str1, str2);
}
addition(str3, str4);
}
cout << "商 = " << str3 << endl;
cout << "余 = " << str1 << endl;
return 0;
}