统计给定的n个数中,负数、零和正数的个数。
Input
    输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output
    对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample Input

    6 0 1 2 3 -1 0
    5 1 2 3 4 0.5
    0

Sample Output

    1 2 3
    0 0 5


 1 #include <cstdio>
2 using namespace std;
3 int main(){
4 int n;
5 while(scanf("%d", &n) == 1 && n){
6 int positive = 0;
7 int zero = 0;
8 int negative = 0;
9 double temp = 0.0;
10 for(int i = 0; i < n; i++){
11 scanf("%lf", &temp);
12 if(temp == 0){
13 zero++;
14 }else if(temp > 0){
15 positive++;
16 }else{
17 negative++;
18 }
19 }
20 printf("%d %d %d\n", negative, zero, positive);
21 }
22 return 0;
23 }

(1)printf的%f说明符既可以输出float型又可以输出double型。
根据“默认参数提升”规则(在printf这样的函数的可变参数列表中,不论作用域内有没有原型,都适用这一规则)float型会被提升为double型。因此printf()只会看到双精度数。
(2)scanf对于float类型必须用%f,double必须用%lf,对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。
(通过指针)向float存储和向double存储大不一样,因此,scanf区别%f和%lf。
(3)事实上,printf中没有定义%lf,但是很多系统可能会接受它。要确保可移植性,就要坚持使用%f。

建议大家使用double类型时,用%lf输入,%f输出避免出错。

float占32位,double占64位,(52位为系数位 11位为指数部分,1位为符号位)

C语言float和double输入问题的更多相关文章

  1. C语言 float、double数据在内存中的存储方式

    float在内存中占4个字节(32bit),32bit=符号位(1bit)+指数位(8bit)+底数位(23bit) 指数部分 指数位占8bit,可以表示数值的范围是0-(表示0~255一共256个数 ...

  2. C语言中关于float和double的输入输出格式

    1.对于double类型,输入格式为scanf("%lf %lf", &foo, &bar); 对于float类型,输入格式为scanf("%f %f, ...

  3. c语言基本数据类型short、int、long、char、float、double

    C 语言包含的数据类型如下图所示: 一.数据类型与“模子”short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 怎么去理解它们呢? 举个例子 ...

  4. c语言基本数据类型(short、int、long、char、float、double)

    一 C 语言包含的数据类型 short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 在不同的系统上,这些类型占据的字节长度是不同的: 在32 ...

  5. c语言基本数据类型short、int、long、char、float、double大小及命名规则

    C 语言包含的数据类型: 一.数据类型与“模子”short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 怎么去理解它们呢? 举个例子:见过藕煤 ...

  6. 不可或缺 Windows Native (4) - C 语言: 预处理命令,输入,输出

    [源码下载] 不可或缺 Windows Native (4) - C 语言: 预处理命令,输入,输出 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 预处理命令 输入 ...

  7. 【转】JAVA程序中Float和Double精度丢失问题

    原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...

  8. 精确计算java中float和double的精度

    [本文相关的代码放在github上.地址为:https://github.com/VigourJiang/StructuredFloat] Java中double类型的格式基本遵循IEEE 754标准 ...

  9. 从数据表字段 float 和 double 说起

    今天在公司讨论项目重构的问题时,公司的 DBA 针对表中的字段大概介绍了一下 float 和 double 的存储方式.然后,我发现这个问题又回到了浮点数类型在内存中的存储方式,即 IEEE 对浮点数 ...

随机推荐

  1. 牛客网-n的约数【dfs】

    题目描述:戳这里 解题思路:这题思路好想,n最多也就是20个不同的素数相乘,把所有可能的素数找到,然后枚举素数个数就行了. n = p1^q1 + p2^q2 + p3 ^q3 + ... + pi ...

  2. Loss_Function_of_Linear_Classifier_and_Optimization

    Loss_Function_of_Linear_Classifier_and_Optimization Multiclass SVM Loss:    Given an example(xi, yi& ...

  3. ReactDOM API All In One

    ReactDOM API All In One React DOM API render() hydrate() unmountComponentAtNode() findDOMNode() crea ...

  4. Nmap & ncat

    Nmap & ncat https://github.com/udacity/course-ud303 https://nmap.org/dist/nmap-7.30-setup.exe Yo ...

  5. LeetCode & list cycle

    LeetCode & list cycle 链表是否存在环检测 singly-linked list 单链表 "use strict"; /** * * @author x ...

  6. nasm astrspn函数 x86

    xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...

  7. WiFi 6 & 5G

    WiFi 6 & 5G https://zhuanlan.zhihu.com/p/85509996 https://www.bilibili.com/read/cv3206576/ https ...

  8. input support upload excel only

    input support upload excel only demo https://codepen.io/xgqfrms/pen/vYONpLB <!-- <input placeh ...

  9. c++ 去掉字符串首尾空格

    http://www.cplusplus.com/reference/regex/regex_replace/ #include <iostream> #include <regex ...

  10. js 使用socket-io发送文件

    更多 前端 import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; import { MediaDevice ...