PAT (Basic Level) Practice (中文) 1017 A除以B
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int MAXN=1001; 5 char a[MAXN]; 6 int b; 7 int q[MAXN],r[MAXN]; 8 void read(); 9 void deal(); 10 int main(){ 11 read(); 12 deal(); 13 return 0; 14 } 15 void read(){ 16 scanf("%s",a); 17 scanf("%d",&b); 18 q[0] = 0; 19 r[0] = 0; 20 } 21 void deal(){ 22 int temp=0,value; 23 for(int i=0;i<strlen(a);i++){ 24 temp = temp * 10 + (a[i]-'0'); 25 if(temp>=b){ 26 q[0]++; 27 q[q[0]] = temp / b; 28 temp = temp % b; 29 } 30 else{ 31 q[0]++; 32 q[q[0]] = 0; 33 } 34 } 35 int flag=0; 36 for(int i=1;i<=q[0];i++){ 37 if((q[i]!=0)||(q[i]==0&&flag==1)){ 38 printf("%d",q[i]); 39 } 40 if(q[i]==0&&flag==0); 41 else if(q[i]!=0) flag=1; 42 } 43 if(flag==0) printf("0"); 44 45 printf(" %d",temp); 46 }
注意商中的0,以及0 的输出。