uva10494 if we were a child again

来源:互联网 发布:mac系统能做什么 编辑:程序博客网 时间:2024/05/17 03:42

大数的相除与取余   不多说

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=1435&mosmsg=Submission+received+with+ID+13135833

#include <stdio.h>#include <string.h>// 大数字符串str与整型x相除,商赋给strvoid myDiv(char str[], int x) {    int b[10000], len = strlen(str);    for (int i=0; i<len; i++)        b[i] = str[i] - '0';    long long cp = 0;   // 注意这里的long long    for (int i=0; i<len; i++) {        long long tmp = b[i] + cp*10;                        // 注意这里的long long        b[i] = tmp / x;        cp = tmp % x;    }    // 修正前面的0然后将结果赋给str字符串    int i = 0, j;    while (i<len && 0==b[i])        i++;    if (i == len) {        str[0] = '0';        str[1] = '\0';    }    else {        for (j=i, i=0; j<len; j++)            str[i++] = b[j] + '0';        str[i] = '\0';    }}// 大数字符串str与整型x取余,余数返回int myMod(char str[], int x) {    int len = strlen(str);    long long tmp = 0;  // 注意这里的long long    for (int i=0; i<len; i++) {        tmp = tmp*10 + str[i]-'0';        tmp %= x;    }    return tmp;}int main() {    char str[10000], ch[5];    int n;    while (scanf("%s %s %d", str, ch, &n) != EOF) {        if (ch[0] == '/') {            myDiv(str, n);            printf("%s\n",str);        }        else {            int x = myMod(str, n);            printf("%d\n", x);        }    }    return 0;}

注意:  提交一定要消去注释  不然会 Compile error   经验所得

0 0
原创粉丝点击