题解 | 小红的 gcd
小红的 gcd
https://www.nowcoder.com/practice/9a5ac1e1c2fb4f8d85595a52ff0a00fa
先对大整数取模,再进行辗转相除法
#include <iostream>
#include<string>
using namespace std;
long long mod(const string&s,long long b){
long long res=0;
for(char c:s){
res=(res*10+c-'0')%b;
}
return res;
}
long long gcd(long long a,long long b){
while(b!=0){
long long t=b;
b=a%b;
a=t;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a;
long long b;
cin>>a>>b;
long long a_mod_b=mod(a,b);
cout<<gcd(a_mod_b,b)<<endl;
}
// 64 位输出请用 printf("%lld")
三奇智元机器人科技有限公司公司福利 65人发布