題目信息
本題要求計算A/B擒抛,其中A是不超過1000位的正整數(shù)仁讨,B是1位正整數(shù)。你需要輸出商數(shù)Q和余數(shù)R笛匙,使得A = B * Q + R成立侨把。
輸入格式:
輸入在1行中依次給出A和B,中間以1空格分隔妹孙。
輸出格式:
在1行中依次輸出Q和R秋柄,中間以1空格分隔。
輸入樣例:
123456789050987654321 7
輸出樣例:
17636684150141093474 3
分析
此題我的思路就是模擬平時除法的做法蠢正。
代碼
#include<stdio.h>
int main(){
char a[1000],q[1000];int b,r,cnt=0,temp=0;//cnt是商的位數(shù)
scanf("%s %d",a,&b);
for(int i=0;i<strlen(a);i++){
temp+=a[i]-'0';
q[cnt++]=temp/b+'0';
temp=(temp%b)*10;
}
r=temp/10;
if(q[0]!='0'){
for(int i=0;i<cnt;i++) printf("%c",q[i]);
printf(" %d",r);
}else if(cnt==1){
printf("0 %d",r);
}else{
for(int i=1;i<cnt;i++) printf("%c",q[i]);
printf(" %d",r);
}
return 0;
}
測試結(jié)果
image.png