当前位置:网站首页>C语言数据类型
C语言数据类型
2022-07-29 05:27:00 【yc_ZZ】
一、数据类型

补充:
unsigned int 0~4294967295
int -2147483648~2147483647
unsigned long 0~4294967295
long -2147483648~2147483647
long long的最大值:9223372036854775807 最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615
2147483647 这个数字一定要记住
是开long long还是int的界限划分
二、重点讲一下浮点数
问题:
编写程序计算 :
1 - 1/2 + 1/3 - 1/4 + … +1/99 - 1/100 的值,
并显示出来(保留结果为小数点后三位)。
错解:
#include<stdio.h>
int main(void)
{
int i,j=1;
float sum=0.0;
for (i=1;i<=100;i++)
{
sum += j*(1/i);
j *= (-1);
}
printf("%.3f",sum);
/*********End**********/
return 0;
}
运行结果:
1.000
正解:
#include<stdio.h>
int main(void)
{
int i,j=1;
float sum=0.0;
for (i=1;i<=100;i++)
{
sum += j*(1.0/i);
j *= (-1);
}
printf("%.3f",sum);
/*********End**********/
return 0;
}
运行结果:
0.688
反思:
因为整数相除只能得到整数。
要想得到浮点数,必须有浮点数参与计算。
三、四舍五入
一、浮点数自带的四舍五入输出
浮点数输出时就可以指定位数四舍五入保留输出
例如
%.0f 表示四舍五入保留整数
%.1f表示四舍五入保留1位小数
%.2f表示四舍五入保留2位小数
…
二、STL库round()函数
对于小数而言,round()函数仅仅保留到整数位,即仅仅对小数点后一位四舍五入
保留小数用法:
如果想要保留小数位数,则可以先乘后除
#include<stdio.h>
#include<math.h>
int main()
{
double x=1.5684;
printf("对1.5684保留两位有效数字:");
printf("%.2lf\n",round(x*100)/100);
return 0;
}
三、例题

#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int greatCount = 0;
int goodCount = 0;
int i = 0;
while(i<n){
int grade;
cin>>grade;
if(grade>=60) goodCount++;
if(grade>=85) greatCount++;
i++;
}
float good = 100.0*goodCount/n;
float great = 100.0*greatCount/n;
printf("%.0f%\n",good);
printf("%.0f%\n",great);
return 0;
}
2、round()函数
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int greatCount = 0;
int goodCount = 0;
int i = 0;
while(i<n){
int grade;
cin>>grade;
if(grade>=60) goodCount++;
if(grade>=85) greatCount++;
i++;
}
cout << round(100.0 * goodCount / n) << "%" << endl;
cout << round(100.0 * greatCount / n) << "%" << endl;
return 0;
}
边栏推荐
- Thinkphp5 frequently asked questions
- Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
- How to pre circumvent the vulnerabilities of unsafe third-party components?
- The performance and viewing methods of websites attacked by DDoS
- Shell脚本-全局变量、局部变量、环境变量
- Merkle tree existential function modified for the first time
- day02_ Basic grammar
- Design and simulation code of 4-bit subtracter based on FPGA
- MQTT服务器搭建以及使用MQTT.fx测试
- Common server faults and their solutions
猜你喜欢

软件定义边界SDP

Condition 条件对象源码浅读

比较单片机3种时钟电路方案

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

Tcp/ip 五层参考模型以及对应的典型设备以及ipv6

Joint use skills of joiner.on and stream().Map

Design and simulation code of 4-bit subtracter based on FPGA

day04_ array

Using STP spanning tree protocol to solve the problem of two-layer loop in network

如何画出优秀的架构图
随机推荐
VMware虚拟机在物理机win10系统下如何连接外网
网络工具中的“瑞士军刀”-nc
Common server faults and their solutions
NeuralCF-神经协同过滤网络
OpenResty的核心与cosocket
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
网站受DDoS攻击的表现以及查看方法
day04_数组
Network Security Learning (I)
day03_ 2_ task
'function vtable for error: undefined reference to ... ' 问题的原因及解决方法
MQTT服务器搭建以及使用MQTT.fx测试
解决文件大导致磁盘满的问题
解决分频模块modelsim下仿真输出为stx的错误
失效的访问控制
SDN拓扑发现原理
finally 和 return 的执行顺序
ping 原理
成长为架构师途中的一些思考
如何画出优秀的架构图