当前位置:网站首页>C语言中的算术运算及相关练习题
C语言中的算术运算及相关练习题
2022-07-02 11:38:00 【theskylife】
写在前面:
读本篇文章可以解决以下问题:如何利用C语言进行算术运算?如何利用位运算来给自己的代码提速?实战代码中如何巧妙利用位运算?实现同一功能的多种写法。
1. 运算符
| 运算符 | 说明 | 例子 |
|---|---|---|
| = | 赋值运算符 | a = b; |
| + 、- 、* 、/、() | 基本四则运 | a = (b + c) * d; |
| % | 求余运算符 | a = b % 2; |
| &、 |、 ^、 ~ | 位运算(非常重要的一类) | a = ~b | c; |
| <<、>> | 左移和右移 | a = b >>2; |
1.1 位运算
位运算均是在二进制情况下完成运算的。
1.1.1 按位与(&)
二进制位全为1结果为1,有一个为0,则为0。位数不足时,前面补0。
如:
- 2&3 --> 10 & 11 --> 10,转为十进制为2
- 8 & 3 --> 1000 & 11 --> 1000 & 0011–>0,转为十进制为0
运用场景:
判断数字n能否整除2,n & 1为1则不能整除,为0则能整除
1.1.2 按位或(|)
二进制位有一个为1,则为1,全为0则为0。位数不足时,前面补0。
如:
- 2|3 --> 10 | 11 --> 11,转为十进制为3
- 8 | 3 --> 1000 | 11 --> 1000 | 0011 -->1011,转为十进制为11
1.1.3 按位异或运算符(^)
二进制位相同为0,不同为1。位数不足时,前面补0。
如:
- 2^3 --> 10 ^ 11 --> 01,转为十进制为1
- 8 ^ 3 --> 1000 ^ 11 --> 1000 ^ 0011–>1011,转为十进制为11。
运用场景:
- ^ 是 ^ 的逆运算。
- a ^ b = c,则 c ^ b = a和c ^ a = b。
- a ^ a = 0。
- 0 ^ a = a。
1.1.4 按位取反(~)
二进制位0变为1,1变为0
如:
- ~3 --> ~11 -->11(30个1)00[补码],转为十进制为-4
补码 = ~原码 + 1
原码取反时,符号位不变
举例:
1的原码:0…0(31个0)1, ~原码: 01…1(30个1)0,补码:01…1(31个1)
-1的原码: 10…0(30个0)1, ~原码: 1…1(31个1)0,补码:1…1(32个1)
-1的补码=1原码全部取反+1
1.2 左移和右移
1.2.1 左移
左移:低位补0
左移1位,相当于扩大两倍。
如: 2 << 1,转为十进制为4.
1.2.2 右移
右移:高位补符号位
如:3 >> 1, 转为十进制为1.
2.数据类型转换
数据类型转换分为两种:强转换和弱转换
强转换:使用int,double等函数进行转换,会丢失精度。
弱转换: 使用类似3*1.0/2方法,将int类型转为double。
3.进制转换
3.1 二进制转十进制
10010=12**4+12**1=18
3.2 十六进制转十进制
8F=816**1+1516**0=143
3.3 十进制转二进制
方法1:使用短除法,直到为0,然后从下往上写;
方法2:凑数
27=16+8+2+1=24+23+21+20=11010
4.math常用的函数
写在前面:
使用下列函数,需要导入math库;
在编译时,使用一下命令
gcc test.c -lm
4.1 pow函数:指数函数
原型: double pow(double a, double b);
例子:pow(4,2)=16.0000
4.2 sqrt函数:开平方函数
原型: double sqrt(double X);
例子:pow(16)=4.0000
4.3 ceil函数:向上取整函数
原型: double ceil(double X);
例子:ceil(2.79)=3.0000
4.4 floor函数:向下取整函数
原型: double floor(double X);
例子:floor(2.79)=2.0000
4.5 abs函数:整数绝对值函数
原型:int abs(int X);
例子:abs(-2)=2
4.6 fabs函数:实数绝对值函数
原型:double fabs(double X);
例子:fabs(-2.6)=2.6
4.7 log函数:以e为底对数函数
原型:double log(double X);
例子:log(4)=1.386294
4.8 log10函数:以e为底对数函数
原型:double log10(double X);
例子:log10(4)= 0.602060
4.9 acos函数:arccos函数
原型:double acos(double X);
X为角度的弧度值
例子:acos(-1)=3.1415926
5.定宽整数类型
#include <stdio.h>
#include <inttypes.h>
int main(){
printf("%zu\n", sizeof(int64_t)); //查看内存大小
printf("%s\n", PRId64); //格式宏常量
printf("INT32_MIN : %" PRId32 ", INT32_MAX : %" PRId32 "\n", INT32_MIN, INT32_MAX); //32位整形最小最大值
printf("INT64_MIN : %" PRId64 ", INT64_MAX : %" PRId64 "\n", INT64_MIN, INT64_MAX); //64位整形最小最大值
int64_t n = 7;
printf("%+"PRId64"\n", n);
return 0;
}
6.练习题
题目1
输入一个数字a,输出其立方根。点击查看答案
题目2:
求π的值。点击查看答案
题目3:
输入一个角度值,并将其转为弧度值。点击查看答案
题目4:
循环读入两个数字a,b,并交换两个值。点击查看答案
参考答案
题目1答案
// 题目1
#include<stdio.h>
#include<math.h>
int main(){
double a;
scanf("%lf", &a);
//解法1,领会思想
printf("%lf\n", pow(a, 1.0 / 3));
//解法2
printf("%lf\n", cbrt(a));
return 0;
}
题目2答案
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
// #define pi 3.14
int main(){
double a;
scanf("%lf", &a);
printf("%lf\n", a * pi / 180);
return 0;
}
题目3答案
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
// #define pi 3.14
int main(){
double a;
scanf("%lf", &a);
printf("%lf\n", a * pi / 180);
return 0;
}
题目4答案
// 方法1
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
int c;
c = a;
a = b;
b = c;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
// 方法2
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
// 方法3
#include<stdio.h>
int main(){
int a, b;
while (~scanf("%d%d", &a, &b)){
printf("a is %d, b is %d\n", a, b);
b ^= a;
a ^= b;
printf("after change: a is %d, b is %d\n", a, b);
}
return 0;
}
边栏推荐
- Delete element (with transition animation)
- 广州市应急管理局发布7月高温高湿化工安全提醒
- A white hole formed by antineutrons produced by particle accelerators
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
- Fabric.js 缩放画布
- Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
- Convolutional neural network (Introduction)
- taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
- 检查密码
- LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
猜你喜欢

Yyds dry goods inventory software encryption lock function

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境

途家木鸟美团夏日折扣对垒,门槛低就一定香吗?

buuctf-pwn write-ups (7)

buuctf-pwn write-ups (7)

Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

STM32-DAC实验&高频DAC输出测试

什么是 eRDMA?丨科普漫画图解

Design and implementation of car query system based on php+mysql
随机推荐
Fabric. JS zoom canvas
【无标题】LeetCode 2321. 拼接数组的最大分数
Yyds dry goods inventory software encryption lock function
[apipost] tutorial
由粒子加速器产生的反中子形成的白洞
C#代码审计实战+前置知识
info [email protected] : The platform “win32“ is incompatible with this module.
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
STM32 library function for GPIO initialization
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Fabric. JS free draw circle
Fundamentals of software testing
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
mathML转latex
4、数组指针和指针数组
tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
NLA natural language analysis makes data analysis more intelligent
【NOI模拟赛】伊莉斯elis(贪心,模拟)
buuctf-pwn write-ups (7)