PAT乙级 A除以B (20)

来源:互联网 发布:ios完整项目源码 编辑:程序博客网 时间:2024/06/05 16:10

相当于一个大整数除法的一个简化,其实这种大整数的题就应该使用python或者java来编写,简单粗暴.

但是我想还是锻炼一下吧,其中出过一次错就是char类型溢出,因为我会把上一次没有除尽的余数加到下一位上面.

思路就是从第一位开始除,余数则乘十累加到下一位.将结果存到另外一个数组中,最后循环输出,去掉最前面的0.

#include "iostream"#include "string"#include "string.h"#include "stdlib.h"using namespace std;int main(){string num;char result[2000];long chushu;long temp;long i;long yushu;long next;cin>>num;cin>>chushu;long len = num.size();next = num[0];//next变量就是防止溢出,因为char类型最大就能存127.for(i = 0;i < len ;i++){temp = next - '0';result[i] = temp/chushu + '0';yushu = temp%chushu;if(i!=(len-1))next = num[i+1] + ((yushu)*10);}for(i = 0;i<len;i++){if(result[i]!='0')break;}for(;i<len;i++){cout<<result[i];}cout<<" "<<yushu<<endl;return 0;}


0 0
原创粉丝点击