题解(记得点赞+收藏)
2025-11-15 12:02:47
发布于:四川
9阅读
0回复
0点赞
法1:
#include<bits/stdc++.h>
using namespace std;
int a[50010],b,c[50010];
int main()
{
string s;
cin>>s>>b;
int len=s.size();
for(int i=0;i<len;i++){
a[i]=s[i]-'0';
}long long r=0,cur=0;
for(int i=0;i<len;i++){
cur=r*10+a[i];
c[i]=cur/b;
r=cur%b;
}int x=0;
for(int i=0;i<len;i++){
if(c[i]!=0){
x=i;
break;
}
}
for(int i=x;i<len;i++)cout<<c[i];
cout<<"\n"<<r;
return 0;
}
法2:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
pair<string, long long> divideStrings(string dividend, long long divisor) {
if (divisor == 0) {
return {"", 0};
}
string quotient;
long long remainder = 0;
for (char digit : dividend) {
remainder = remainder * 10 + (digit - '0');
quotient.push_back((remainder / divisor) + '0');
remainder %= divisor;
}
quotient.erase(0, quotient.find_first_not_of('0'));
if (quotient.empty()) quotient = "0";
return {quotient, remainder};
}
int main() {
string a;
long long b;
cin >> a >> b;
auto result = divideStrings(a, b);
cout << result.first << "\n" << result.second << endl;
return 0;
}
运行时间&内存占用:
| 方法\运行时间&内存占用 | 运行时间 | 内存占用 |
|---|---|---|
| 法1 | ||
| 法2 |
这里空空如也







有帮助,赞一个