while用法小简介(涉及EOF用法)

版权声明:版权声明:文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_43581971/article/details/84575057

题目描述:Your task is to calculate  :   a+b

         (a+b多实例测试升级版)

输入:Input contains multiple test cases.Each test case contains a pair of integers a and b,one pair of integers per line.A test case containing 0 0 terminates the input and this test case is not to be processed.

输出:For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

样例输入:

          1 5
          10 20
           0 0

样例输出:

         6

         30

考察点:while,EOF语句的用法。

常见误区:输入时逻辑语句不对   需要好好理解“||”(或)和“&&”(且)在C语言中的意思。

解题思路:自己想想看喽,嘿嘿。

代码如下:

#include<stdio.h>
int main()
{
	int a,b;
	while(scanf("%d %d",&a,&b)&&a!=0||b!=0)
	{
		printf("%d\n",a+b);
	}
	return 0;
}

注:

重头戏:     while(scanf("%d %d",&a,&b)&&a!=0||b!=0)

一定定要好好理解斜体加粗的意思   嗯哼

关于while注释如下:

        while:实现循环表达的一种,循环的内容只能是一个语句,可以是一个简单的语句,还可以是复合语句(用花括号括起来的若干语句),表达式的值为真(即非0)时,进行循环;而当表达式为假(即不满足while括号内的内容时也用0表示),并不执行语句。while循环特点:先判断条件表达式,后执行循环体语句。

关于EOF:

      EOF :常被用做文件结束的标记;常用来判断调用一个函数是否成功。(哈哈哈,百度了解一下)

你可能会遇到有很多题,说“读到两个0时结束循环,前面没有组数n的限制”,这个时候你可以试试while+EOF ,相信我,你不会失望哒    耶耶耶。

猜你喜欢

转载自blog.csdn.net/qq_43581971/article/details/84575057