A1065 A+B and C (64bit) (20)(20 分)

Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C.

Input Specification:

The first line of the input gives the positive number of test cases, T (<=10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.

Output Specification:

For each test case, output in one line "Case #X: true" if A+B&gtC, or "Case #X: false" otherwise, where X is the case number (starting from 1).

Sample Input:

3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0

Sample Output:

Case #1: false
Case #2: true
Case #3: false

思考

这里暂时缺少,胡凡书籍图片,暂时空着。



7月30日补上

AC代码

#include <stdio.h>
#include <stdbool.h>
int main() {
int T, tcase = 1;
scanf("%d", &T);
while(T--) {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
long long res = a + b;//保存可能溢出的结果
bool flag;
if(a > 0 && b > 0 && res < 0) flag = true;
else if(a < 0 && b < 0 && res >= 0) flag = false;
else if(res > c) flag = true;
else flag = false;
if(flag == true) {
printf("Case #%d: true\n", tcase++);
} else {
printf("Case #%d: false\n", tcase++);
}
}
return 0;
}

c语言的布尔型

现在的C语言已经有了布尔型:从C99标准开始,类型名字为“_Bool”.

​ 在此之前的C语言中,使用整型int来表示真假。在输入时:使用非零值表示真;零值表示假。在输出时:真的结果是1,假的结果是0;(这里我所说的“输入”,意思是:当在一个需要布尔值的地方,也就是其它类型转化为布尔类型时,比如 if 条件判断中的的条件;“输出”的意思是:程序的逻辑表达式返回的结果,也就是布尔类型转化为其他类型时,比如 a==b的返回结果,只有0和1两种可能)。

​ 所以,现在只要你的编译器支持C99,可以直接使用布尔型了。另外,C99为了让CC++兼容,增加了一个头文件stdbool.h。里面定义了booltruefalse,让我们可以像C++一样的定义布尔类型。

1. 我们自己定义的“仿布尔型”

​ 在C99标准被支持之前,我们常常自己模仿定义布尔型,方式有很多种,常见的有下面两种:

/* 第一种方法 */
#define TRUE 1
#define FALSE 0 /* 第二种方法 */
enum bool{false, true};

2. 使用_Bool

​ 现在,我们可以简单的使用 _Bool 来定义布尔型变量。_Bool类型长度为1,只能取值范围为0或1。将任意非零值赋值给_Bool类型,都会先转换为1,表示。将零值赋值给_Bool类型,结果为0,表示。 下面是一个例子程序。

#include <stdio.h>
#include <stdlib.h>
int main(){
_Bool a = 1;
_Bool b = 2; /* 使用非零值,b的值为1 */
_Bool c = 0;
_Bool d = -1; /* 使用非零值,d的值为1 */
printf("a==%d, /n", a);
printf("b==%d, /n", b);
printf("c==%d, /n", c);
printf("d==%d, /n", d);
printf("sizeof(_Bool) == %d /n", sizeof(_Bool));
system("pause");
return EXIT_SUCCESS;
}

运行结果如下:(只有0和1两种取值)

a==1,
b==1,
c==0,
d==1,
sizeof(_Bool) == 1

3. 使用stdbool.h

​ 在C++中,通过bool来定义布尔变量,通过truefalse对布尔变量进行赋值。C99为了让我们能够写出与C++兼容的代码,添加了一个头文件<stdbool.h>。在gcc中,这个头文件的源码如下:(注,为了清楚,不重要的注释部分已经省略)

/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.

        This file is part of GCC.
*/
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#else /* __cplusplus ,应用于C++里,这里不用处理它*/
/* Supporting <stdbool.h> in C++ is a GCC extension. */
#define _Bool bool
#define bool bool
#define false false
#define true true
#endif /* __cplusplus *
/* Signal that all the definitions are present. */
#define __bool_true_false_are_defined 1
#endif /* stdbool.h */

​ 可见,stdbool.h中定义了4个宏,bool、true、false、__bool_true_false_are_defined。 其中bool就是 _Bool类型,true和false的值为1和0,__bool_true_false_are_defined的值为1。

下面是一个例子程序

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* 测试C99新添加的头文件 stdbool.h */
int main(){
bool m = true;
bool n = false;
printf("m==%d, n==%d /n", m, n);
printf("sizeof(_Bool) == %d /n", sizeof(_Bool));
system("pause");
return EXIT_SUCCESS;
}

执行结果为:

m==1,  n==0
sizeof(_Bool) == 1

A1065 A+B and C (64bit) (20)(20 分)的更多相关文章

  1. 1065 A+B and C (64bit) (20 分)

    1065 A+B and C (64bit) (20 分) Given three integers A, B and C in [−2^​63​​,2​^63​​], you are suppose ...

  2. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  3. MVC4 学习笔记 之 URL中存在编译的空格 20%20%

    /Config/Edit/QQCC%20%20%20%20%20%20%20 原因是: 通过EF直接添加了空格? NO 是因为你的数据库字段设计问题,因为你当然设计如>:sID nchar(10 ...

  4. 2016年11月29日 星期二 --出埃及记 Exodus 20:20

    2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...

  5. 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决

    下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...

  6. PAT A1065 A+B and C (64bit) (20 分)

    AC代码 #include <cstdio> int main() { #ifdef ONLINE_JUDGE #else freopen("1.txt", " ...

  7. [C++]PAT乙级1012.数字分类 (20/20)

    /* 1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和, ...

  8. [C++]PAT乙级1008.数组元素循环右移问题 (20/20)

    /* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...

  9. [C++]PAT乙级1007.素数对猜想 (20/20)

    /* 1007. 素数对猜想 (20) 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷 ...

随机推荐

  1. eclipse配置android开发环境并搭建第一个helloWord工程

    一.搭建Android在eclipse下环境    一.JDK(不用安装  下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...

  2. mysql必知必会学习笔记(1)

    chapter 13 13.1 数据分组 如果要将数据分为多个逻辑组怎么办?? 要用数据分组 13.2 创建分组 使用group by子句.group by会指示对mysql进行分组,然后对每个组进行 ...

  3. [JQuery] Using skill in JQuery

    Using skill of JQuery 获取兄弟节点 $('#id').siblings() 当前元素的所有兄弟节点 $('#id').prev() 当前元素的前一个兄弟节点 $('#id').p ...

  4. 关于wav文件fft处理后x,y轴坐标数据的问题

    1.关于横坐标的频率的最大值是采样频率,那么每个点对应的频率值就很好算了:f(n) = [Fs/(N/2)]*n  (Fs是采样频率,常见的是44.1KHz(44100),N是采样点数,k表是第k个点 ...

  5. 学习笔记:location.hash和history.pushState()

    在浏览器中改变地址栏url,将会触发页面资源的重新加载,这使得我们可以在不同的页面间进行跳转,得以浏览不同的内容.但随着单页应用的增多,越来越多的网站采用ajax来加载资源.因为异步加载的特性,地址栏 ...

  6. 解决ie9以及以下console未定义

    页面明明已经删除了所有的console,但是ie9下依旧会报错 console未定义 只能这样解决了 window.console = window.console || (function () { ...

  7. Jquery删除table里面checkbox选中的多个行

    自己闲来无聊,写了一篇关于jq选中复选框删除数据的一个功能,不足之处,还望多多包涵 js代码 <script type="text/javascript" src=" ...

  8. map侧连接

    两个数据集中一个非常小,可以让小数据集存入缓存.在作业开始这些文件会被复制到运行task的节点上. 一开始,它的setup方法会检索缓存文件. 与reduce侧连接不同,Map侧连接需要等待参与连接的 ...

  9. 巧用netsh命令实现端口转发(端口映射)不求人

    好处:即时生效,随意修改删除,不影响其他ip映射 记事本保存为bat格式批量添加后,并查看映射: netsh interface portproxy add v4tov4 listenport=701 ...

  10. 撸了个 django 数据迁移工具 django-supertube

    撸了个 django 数据迁移工具 django-supertube 支持字段映射和动态字段转化. 欢迎 star,issue https://github.com/FingerLiu/django- ...