当前位置:
首页 >
信息学奥赛一本通 1175:除以13 | OpenJudge NOI 1.13 27:除以13
发布时间:2025/3/17
54
豆豆
生活随笔
收集整理的这篇文章主要介绍了
信息学奥赛一本通 1175:除以13 | OpenJudge NOI 1.13 27:除以13
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
【题目链接】
ybt 1175:除以13
OpenJudge NOI 1.13 27:除以13
【题目考点】
1. 高精度
考察:高精除低精 高精模低精
高精度计算讲解
【题解代码】
解法1:使用函数与数组
#include <bits/stdc++.h> using namespace std; #define N 105 void toNum(char s[], int a[]) {a[0] = strlen(s);for(int i = 1; i <= a[0]; ++i)a[i] = s[a[0] - i] - '0'; } void showNum(int a[]) {for(int i = a[0]; i >= 1; --i)cout << a[i]; } void setLen(int a[], int i) {while(a[i] == 0 && i > 1)i--;a[0] = i; } //高精度数字除以低精度数字 a为被除数 b为除数 r为商,返回值是余数 int Divide(int a[], int b, int r[]) {int x = 0;//x:是上一次进行除法的余数,以及作为下一次的被减数for(int i = a[0]; i >= 1; --i){r[i] = (x * 10 + a[i]) / b;x = (x * 10 + a[i]) % b;}setLen(r, a[0]); return x; } int main() {int a[N] = {}, r[N] = {}, m;char s[N];cin >> s;toNum(s, a);m = Divide(a, 13, r);showNum(r);cout << endl << m;return 0; }解法2:类中重载运算符
#include<bits/stdc++.h> using namespace std; #define N 105 struct HPN {int a[N];HPN(){memset(a, 0, sizeof(a));}HPN(char s[]){memset(a, 0, sizeof(a));a[0] = strlen(s);for(int i = 1; i <= a[0]; ++i)a[i] = s[a[0] - i] - '0';}int& operator [] (int i){return a[i];}void setLen(int i)//确定数字位数{while(a[i] == 0 && i > 1)i--;a[0] = i;}void show(){for(int i = a[0]; i >= 1; --i)cout << a[i];}HPN operator / (int b) //高精除低精{int x = 0;HPN r;for(int i = a[0]; i >= 1; --i){x = x * 10 + a[i];r[i] = x / b;x %= b;}r.setLen(a[0]);return r;}int operator % (int b) //高精模低精{int x = 0;for(int i = a[0]; i >= 1; --i)x = (x * 10 + a[i]) % b;return x;} }; int main() {char s[N];cin >> s;HPN a(s), r;int m;r = a / 13;//高精除低精 m = a % 13;//高精模低精r.show();cout << endl << m; return 0; }总结
以上是生活随笔为你收集整理的信息学奥赛一本通 1175:除以13 | OpenJudge NOI 1.13 27:除以13的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: php计时器每过24小时结果加一倍,单片
- 下一篇: mipi和isp处理_图像信号处理 (I