想法:
與直式除法一樣,一邊作商數,一邊作餘數,直到被除數的個位數為止。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
char num[10001]; | |
long long int b,R; | |
char oper[2]; | |
int main() | |
{ | |
//freopen ("input.txt","rt",stdin); | |
while (scanf("%s%s%lld",num,oper,&b) != EOF){ | |
int Q[10001],pos = 0; | |
int i; | |
for (i = 0, R = 0; num[i]; i++){ | |
R = R * 10 + (num[i] - '0'); | |
Q[pos++] = R / b; // ans前面幾個數字可能為0,等等輸出要從非0開始 | |
R = R % b; | |
} | |
if (oper[0] == '/'){ | |
for (i = 0; i < pos && Q[i] == 0; i++) | |
; | |
if (i == pos) printf("0"); // 如果被除數比除數小的情況 | |
else | |
for (; i < pos; i++) printf ("%d",Q[i]); | |
printf("\n"); | |
} | |
else | |
printf("%lld\n",R); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言