这个数非常大,可能有几十位,远远超出int的范围,用char data[100]数组来存储。
遍历这个数组,用模拟手算的方法计算商和余数。
int main() {
char data[100] = { '0'};
printf("请输入这个数:");
gets(data);
int len = strlen(data);
//求余数,商,模拟手算的方法
char res[100] = { '0' };
int count = 0;
int temp = 0, carry = 0; //carry为余数
for (int j = 0; j < len; j++) {
temp = data[j] - '0' + carry * 10;
carry = temp % 3;
temp = temp / 3;
res[count++] = temp + '0';
}
printf("\n余数:%d", carry);
if(res[0] == '0')
printf("\n商:%s\n", res+1); //最高位的0不打印
else
printf("\n商:%s\n", res);
return 0;
}
运行结果如下: