- #include <stdio.h>
- #include <string.h>
- #include <list>
- using namespace std;
- void convert(char *a,list<int> &a1)
- {/*转换函数*/
- a1.clear();
- int len=strlen(a);
- int i;
- for(i=0;i<len;i++)
- a1.push_back(a[i]-'0');
- while(*a1.begin()==0) a1.pop_front();
- }
- void print(list<int> &a)
- { /*打印函数*/
- list<int>::iterator it;
- it=a.begin();
- if(a.empty()) {printf("0\n"); return;}
- for(it;it!=a.end();it++)
- printf("%d",*it);
- printf("\n");
- }
- void sub(list<int> &a,list<int> &b, list<int> &c)
- { /*大整数的减法*/
- c.clear();
- list<int>::reverse_iterator rita=a.rbegin();
- list<int>::reverse_iterator ritb=b.rbegin();
- short carry=0;
- int temp;
- while(rita!=a.rend()&&ritb!=b.rend())
- {
- temp=*rita-*ritb+carry;
- if(temp<0)
- {
- temp+=10; carry=-1;
- }
- else carry=0;
- c.push_front(temp);
- rita++;
- ritb++;
- }
- while(rita!=a.rend())
- {
- temp=*rita+carry;
- if(temp<0) { temp+=10; carry=-1;}
- else carry=0;
- c.push_front(temp);
- rita++;
- }
- while(*c.begin()==0) c.pop_front();
-
- }
- bool compare(list<int> &a,list<int> &b)
- { /*大整数的比较*/
- if(a.size()>b.size()) return true;
- else if(a.size()<b.size()) return false;
- else
- {
- list<int>::iterator ita=a.begin(),itb=b.begin();
- while(ita!=a.end())
- {
- if(*ita>*itb) return true;
- else if(*ita<*itb) return false;
- else { ita++; itb++;}
- }
- return true;
- }
- }
- void divid(list<int> &a,list<int> &b, list<int> &c)
- { /* 大整数的除法*/
- c.clear();
- list<int> row,temp;
- list<int>::iterator ita,itc=c.begin();
- int count;
- for(ita=a.begin();ita!=a.end();ita++)
- {
- count=0;
- while(*row.begin()==0) row.pop_front();
- row.push_back(*ita);
- while(compare(row,b))
- {
- count++;
- sub(row,b,temp);
- row=temp;
- }
- c.push_back(count);
- }
- while(*c.begin()==0) c.pop_front();
- }
-
- int main()
- {
- char sa[901],sb[901];
- list<int> a,b,c;
- while(scanf("%s%s",sa,sb)==2)
- {
- convert(sa,a);
- convert(sb,b);
- divid(a,b,c);
- print(c);
- }
- return 0;
|