zoukankan      html  css  js  c++  java
  • 【转】关于使用printf和scanf对short进行输入输出的一段有趣对话

    http://hi.baidu.com/homonia/blog/item/d6eb7c5445748b1a3b293561.html

    简单(195535436) 10:17:06
    请诸位大侠帮忙看一下这个程序,
    输入6个short整数,然后输出

    简单(195535436) 10:17:22
    #include <cstdio>
    using namespace std;

    #define MAX_N 100
    short poly_coeff[MAX_N + 1];

    int main(void)
    {
    short N = 5;

    short n;
    for(n = N; n >= 0; n--)
    scanf("%d", &poly_coeff[n]);

    for(n = N; n >= 0; n--)
    printf("%d ", poly_coeff[n]);

    return 0;
    }

    Derek(393304094) 10:18:08
    What's the problem?

    简单(195535436) 10:18:10
    #include <cstdio>
    using namespace std;

    #define MAX_N 100

    short poly_coeff[MAX_N + 1];

    int main(void)
    {
    short N = 5;

    short n;
    for(n = N; n >= 0; n--)
    scanf("%d", &poly_coeff[n]);

    for(n = N; n >= 0; n--)
    printf("%d ", poly_coeff[n]);

    return 0;
    }

    简单(195535436) 10:18:28
    输入:1 2 3 4 5 6
    输出:0 0 0 0 0 6

    简单(195535436) 10:21:41
    输入输出改为c++的cin和cout,可以
    将short改为int,可以
    将n从0开始循环,也可以

    就是上面这种情况有问题

    简单(195535436) 10:24:18
    更奇怪的是,将
    printf("%d ", poly_coeff[n]);
    加入第一个for循环也可以正常输出

    猩猩(2113501) 10:26:25
    不奇怪

    猩猩(2113501) 10:26:28
    注意%d

    简单(195535436) 10:27:28
    why?

    Derek(393304094) 10:27:37
    嗯,改为int poly_coeff[MAX_N + 1]就可以了

    简单(195535436) 10:27:45
    是啊

    猩猩(2113501) 10:28:02
    你的数组是short的啊

    猩猩(2113501) 10:28:09
    然后读入的时候用了%d啊

    Derek(393304094) 10:28:33
    每次输入的时候,"%d"表示接受一个整数,所以覆盖了数组后一个元素的值

    猩猩(2113501) 10:30:10

    数组内存: 1 2 3 4 5 6
    输入内存 0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    输出也是%d 0 0
    0 0
    0 0
    0 0
    0 0
    0 6


    简单(195535436) 10:31:05
    明白,太崇拜你们了

    简单(195535436) 10:31:17
    改为“%hd”就可以了

    猩猩(2113501) 10:31:29
    所以,**,做人要 hd啊

    简单(195535436) 10:31:31
    thx


    简单(195535436) 10:31:59
    hd = ?

    澍(664686993) 10:32:05
    厚道

    猩猩(2113501) 10:32:06
    厚道

    简单(195535436) 10:32:15

    Derek(393304094) 10:32:42
    那 %hd=?

    简单(195535436) 10:32:55
    输入短整型

    简单(195535436) 10:33:04
    或者输出

    Derek(393304094) 10:33:12
    我还以为是百分之百厚道?

    帆(44485203) 10:33:23
    re这句

    猩猩(2113501) 10:33:26
    re

    简单(195535436) 10:33:32
    re

    澍(664686993) 10:35:23
    100%RE

  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/platero/p/1880806.html
Copyright © 2011-2022 走看看