当前位置:网站首页>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;
}
边栏推荐
- Conversion of fixed-point number to floating-point number of vivado IP core
- Floating point addition and subtraction method of vivado IP core floating point
- How to pre circumvent the vulnerabilities of unsafe third-party components?
- What if the 80443 port of the website server has been maliciously attacked?
- day10_ Exception handling & enumeration
- 解决文件大导致磁盘满的问题
- SQL developer graphical window to create database (tablespace and user)
- After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
- Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
- NeuralCF-神经协同过滤网络
猜你喜欢

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

将源码包转换为rpm包

centos 部署postgresql 13

CNN-卷积神经网络

day02_ Basic grammar

Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation

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

day03_1_流程控制

Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus

Ram block memory generator of vivado IP core
随机推荐
案例补充、ATM
SQL developer graphical window to create database (tablespace and user)
10种常见的软件架构模式
超低成本DDoS攻击来袭,看WAF如何绝地防护
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction
如何在开发板上使用sftp命令访问sftp-server
CNAME记录和A记录的区别
Arrays&Object&System&Math&Random&包装类
finally 和 return 的执行顺序
Is it OK to directly compare the size of two numbers in FPGA?
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
day12_多线程
day15_泛型
What if the 80443 port of the website server has been maliciously attacked?
Those vulnerability attacks on app
etcd原理
day16-集合上
day03_1_流程控制
Six common ways for hackers to attack servers
华为交换机CE12808导入导出配置文件