题目:10494 - If We Were a Child Again
题目大意:高精度数/ 或%低精度数;
解题思路:就是除法的计算思路;注意用高精度数%低精度数结果一定会比低精度数位数多,所以用long long;还有从字符类型转换成整型int 要 - ‘0’(值48);
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int N = 10000;
char s1[N];
unsigned b;
char ch;
struct bign {
int len, s[N];
bign () {
len = 1;
memset(s, 0, sizeof(s));
}
bign operator =(const char * a) ;
long long operator % (unsigned a);
bign operator /(unsigned a) ;
};
bign s , tmp;
bign bign::operator = (const char * a) {
len = strlen(a);
for(int i = 0 ; i < len ; i++) {
s[i] = a[i] - '0';
}
return * this;
}
long long bign::operator % (unsigned a) {
long long sum = 0;
for(int i = 0 ; i < len; i++ ) {
sum = sum * 10 + s[i];
sum = sum % a;
}
return sum;
}
bign bign::operator /(unsigned a) {
bign num;
num.len = 0;
long long sum = 0;
for (int i = 0 ; i < len; i++) {
sum = sum * 10 + s[i];
num.s[num.len++] = sum / a;
sum = sum % a;
}
return num;
}
int main () {
while (scanf("%s %c %u",s1, &ch, &b) != EOF) {
s = s1;
long long a;
if(ch == '%') {
a = s % b;
printf("%d\n", a);
}
else if(ch == '/') {
tmp = s / b;
bool bo = 0;
for(int i = 0 ; i < tmp.len ; i++ ) {
if(tmp.s[i] != 0 )
bo = 1;
if(bo)
printf("%d", tmp.s[i]);
}
if(bo == 0)
printf("0");
printf("\n");
}
memset(s.s, 0, sizeof(s.s));
memset (tmp.s, 0, sizeof(tmp.s));
memset(s1, 0, sizeof(s1));
}
return 0;
}