zoukankan      html  css  js  c++  java
  • Calculate A + B.

    Problem Description

    Calculate A + B.

    Input

    Each line will contain two integers A and B. Process to end of file.

    Output

    For each case, output A + B in one line.

    Sample Input

    1 1

    Sample Output

    2
     
    一般代码(不通过):
    #include<stdio.h>
    int main(){
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d
    ",a+b);
        return 0;    
    }

    标准代码:

    #include<stdio.h>
    int main(){
        int a,b;
        while(~scanf("%d%d",&a,&b))
        {
            print("%d
    ",a+b);
        }
        return 0;
    }

    值得一提的有三点:

    • 这里为什么把scanf加到while里面循环一下。

      • 以上两种写法平时都是对的,更多人倾向于第一种,而第二种是ACM中经常用的一种写法。在ACM中输入和输出,不用控制台,而是使用文本输入,就需要判断文本是否为空文本,通常在文本的最后存在字符EOF表示资料结束。

    • “~”的作用。

      • “~”是按位取反。按位取反的意思就是:对所有整数取反=本身的相反数-1。~9=-10、~10=-11。
      • 接着说一下while,针对本题,while(0)代表终止循环,while(非0)执行接下来的循环体。
      • scanf的返回值是输入值的个数,例如本题:当a,b都成功写入,scanf返回值就是2。如果文本内没有输入值就是返回EOF,EOF代表-1,-1按位取反结果是0,while(~scanf("%d", &n))就是当没有输入的时候退出循环。关于scanf的具体用法可以看一下:C语言中scanf函数与空格回车
    • while (~scanf("%d%d",&n,&m))和while (scanf("%d%d",&n,&m)!=EOF)区别。

      • 两者没区别。后者的具体意思进链接:ACM之while(scanf("%d",&n)!=EOF)。总的概括都是:当scanf函数的返回值不等于EOF时 继续进行循环。因为当返回值等于EOF时,(EOF!=EOF)=0,当while(0)时,终止循环。

    借鉴:位运算符之---按位取反运算符(简单易懂)

       while (~scanf("%d%d",&n,&m))

    题目来源:http://acm.hdu.edu.cn/listproblem.php?vol=1

  • 相关阅读:
    fpga不错的源代码下载地址
    iverilog命令选项解释
    altera官方视频教程下载地址
    niosII EDS和QuartusII安装心得体会
    FPGA的JTAG口很脆弱?
    poj2379
    poj2472
    poj2935
    poj3366
    poj1493
  • 原文地址:https://www.cnblogs.com/shcsw/p/13259408.html
Copyright © 2011-2022 走看看