c - 该算法存在一些缺陷,无法通过名为 "partly calculate A add B"的所有OJ测试

标签 c algorithm

OJ 的问题是:

Question: Give three positive integer like A Da B Db and calculate Pa+Pb. And the rule likes Example: A = 3862767, Da = 6,so Pa = 66, because there are two number "6" in integer A.

Input Format: Enter A Da B Db in one line, and use space to split them, and 0<A,B<10^10

Output Format: Show the value of Pa+Pb in one line

Input Example 1: 3862767 6 13530293 3

Output Example 1: 399

Input Example 2: 3862767 1 13530293 8

Output Example 2: 0

我的问题:我已经通过了五项测试中的四项,但不知道为什么我的代码无法全部通过并且编译器是 gcc 4.7.2

我的代码是:

int fun(int a, int b) {
    int n = 0;
    short i = 1,k;
    do {
        k = a % 10;
        if (k == b) {
            n += i*k;
            i *= 10;
        }
    } while ((a /= 10) != 0);
    return n;
}

int main() {
    int a1, a2, a3, a4;
    scanf("%d %d %d %d", &a1, &a2, &a3, &a4);
    printf("%d", fun(a1, a2) + fun(a3, a4));
    return 0;
}

最佳答案

是的,我不应该使用 shortint,更改为 long long 后测试已通过。@david-eisenstat

关于c - 该算法存在一些缺陷,无法通过名为 "partly calculate A add B"的所有OJ测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38683592/

相关文章:

c - 我如何修复由 c 中的 man 2 stat 成员造成的泄漏

c - 使用 while 循环进行减法

algorithm - 交换 LinkedList 的相邻节点

algorithm - 在列表中找到所有可能对的最快方法是什么?

algorithm - B+树插入顺序

c - 如果 char[] 位于 struct __attribute__((aligned)) 内部,则 char[] 的步长是否保证为 1?

printf 可以在不刷新标准输出的情况下写入终端吗?

c++ - 将矩阵拆分为小矩阵 block 的方法

algorithm - 在二叉搜索树中插入 n 个序列的摊销成本是多少?

计算数组中当前元素右侧的更大元素