ACwing语法基础课第一节课例题与习题及个人总结
第一次课例题
若涉及到浮点数的计算,float一般是6到7位有效数字,double一般是15到16位有效数字,但是为了方便起见,建议直接设为double,因为若涉及浮点数的乘除运算,使用float类型很容易出现精度问题,从而造成答案偏差;
同时,一定要注意占位符与变量的对应关系,int,float,double在计算机中存储的方式是不同的,若学过计组的话应该很好理解,若是输出时,int型变量用%f去占位自然会出现错误,一定要用对应的%d占位。
//基础的运算
//AcWing 1. A + B  https://www.acwing.com/activity/content/problem/content/1822/
#include <iostream>
using namespace std;
int main(){
    int A,B;
    cin >> A >> B;
    cout << A+B;
    return 0;
}
//AcWing 608. 差  https://www.acwing.com/activity/content/problem/content/1823/
#include <iostream>
using namespace std;
int main(){
    int A,B,C,D;
    cin >> A >> B >> C >> D;
    cout << "DIFERENCA = " << (A*B-C*D);
    return 0;
}
//AcWing 604. 圆的面积  https://www.acwing.com/activity/content/problem/content/1824/
#include <iostream>
#include <math.h>
#define pi 3.14159
using namespace std;
int main(){
    double R, A;
    cin >> R;
    A = pi * pow(R, 2);
    printf("A=%.4f", A);  // 注意这里要用printf输出好一点,因为题目要求输出四位小数,而且printf比cout要快一点
    return 0;
}
//AcWing 606. 平均数1  https://www.acwing.com/activity/content/problem/content/1825/
#include <iostream>
#include <math.h>
using namespace std;
int main(){
    float A,B;
    cin >> A >>B;
    printf("MEDIA = %.5f", (3.5*A+7.5*B)/11); // 注意这里要用printf输出好一点,因为题目要求输出5位小数
    return 0;
}
//AcWing 609. 工资   https://www.acwing.com/activity/content/problem/content/1826/
#include <iostream>
using namespace std;
int main(){
    int tag, time;
    float wage;
    cin >> tag >> time >> wage;
    cout << "NUMBER = " << tag << endl;
    printf("SALARY = U$ %.2f", wage * time);
    return 0;
}
// AcWing 615. 油耗  https://www.acwing.com/activity/content/problem/content/1827/
#include <iostream>
using namespace std;
int main(){
    int km;
    float l;
    cin >> km >> l;
    printf("%.3f km/l", km/l);
    return 0;
}
//616. 两点间的距离  https://www.acwing.com/activity/content/problem/content/1828/
#include <iostream>
#include <math.h>
using namespace std;
int main(){
    double x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    printf("%.4f", sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2 )));
    return 0;
}
//AcWing 653. 钞票  https://www.acwing.com/activity/content/problem/content/1829/
#include <iostream>
using namespace std;
int main(){
    int dollar;
    cin >> dollar;
    cout << dollar << endl;
    cout << dollar/100 << " nota(s) de R$ 100,00" << endl , dollar %= 100;
    cout << dollar/50 << " nota(s) de R$ 50,00" << endl , dollar %= 50;
    cout << dollar/20 << " nota(s) de R$ 20,00" << endl , dollar %= 20;
    cout << dollar/10 << " nota(s) de R$ 10,00" << endl , dollar %= 10;
    cout << dollar/5 << " nota(s) de R$ 5,00" << endl , dollar %= 5;
    cout << dollar/2 << " nota(s) de R$ 2,00" << endl , dollar %= 2;
    cout << dollar/1 << " nota(s) de R$ 1,00" << endl ;
    return 0;
}
//AcWing 654. 时间转换  https://www.acwing.com/activity/content/problem/content/1830/
#include <iostream>
using namespace std;
int main(){
    int ++time;
    cin >> time;
    cout << time/3600 << ":" << (time%3600)/60 << ":" << (time%3600)%60;
    return 0;
}
第一次课练习
一些总结:float的有效表示范围是6到7位,double是15到16位,一般来说如果题目里面有小数,可以优先考虑用double,有时候用float会造成精度丢失。同时运算的时候,要注意变量类型的自动转换。
比如double l = time*s/12.0;:这里面若变量time与s是整数,而要求的结果是double类型,若除以/12则运算就是先用int计算完除法,再转换为double类型,输出的其实还是个整数,若数据大或者实际结果有小数则必然会出现精度丢失的问题,而若除以/12.0则两个int型变量会转换为浮点类型,从而保证精度。
同时,控制输出位有效位,或者说控制输出位的小数部分有效位十分关键,也许要求输出的是一个整数,但是若用int计算必然会造成精度丢失,那么可以考虑用float或者double计算,但在输出时使得其仅输出整数部分,即%.0f
// AcWing 605. 简单乘积  https://www.acwing.com/activity/content/problem/content/1831/
#include <iostream>
using namespace std;
int PROD;
int main(){
    int i, j;
    cin >> i >> j;
    cout << "PROD = " << i*j;
    return 0;
}
// AcWing 611. 简单计算  https://www.acwing.com/activity/content/problem/content/1832/
#include <iostream>
using namespace std;
int a ,b ,n ,m;
double a_value, b_value;  // 这里不能用float,而要用double,
int main()
{
    cin >> a >> n >> a_value ;
    cin >> b >> m >> b_value;
    printf("VALOR A PAGAR: R$ %.2f", n*a_value + m*b_value);
}
// AcWing 612. 球的体积  https://www.acwing.com/activity/content/problem/content/1833/
#include <iostream>
using namespace std;
#define pi 3.14159 
int main()
{
    double R;
    cin >> R;
    double v ;
    v = (4/3.0)*pi*(R*R*R);
    printf("VOLUME = %.3lf", v);
    return 0;
}
// AcWing 613. 面积  https://www.acwing.com/activity/content/problem/content/1834/
#include <iostream>
#define pi 3.14159
using namespace std;
double a, b, c;
int main(){
    cin >> a >> b >> c;
    //三角形
    double s1 = a*c/2;
    //圆
    double s2 = c*c*pi;
    //梯形
    double s3 = (a + b)*c/2;
    //正方形
    double s4 = b*b;
    //长方形
    double s5 = a*b;
    printf("TRIANGULO: %.3lf\n", s1);
    printf("CIRCULO: %.3lf\n", s2);
    printf("TRAPEZIO: %.3lf\n", s3);
    printf("QUADRADO: %.3lf\n", s4);
    printf("RETANGULO: %.3lf\n", s5);
    return 0;
}
//AcWing 607. 平均数2  https://www.acwing.com/activity/content/problem/content/1835/
#include <iostream>
using namespace std;
float A, B, C;
int main()
{
    cin >> A >> B >> C;
    float media = (A*2+B*3+C*5)/(2+3+5);
    printf("MEDIA = %.1f", media);
    return 0;
}
//AcWing 610. 工资和奖金  https://www.acwing.com/activity/content/problem/content/1836/
#include <iostream>
using namespace std;
char s[20];
float wage, month;
int main()
{
    cin >> s;
    cin >> wage >> month;
    float salary = wage + 0.15*month;
    printf("TOTAL = R$ %.2f", salary);
    return 0;
}
//AcWing 614. 最大值  https://www.acwing.com/activity/content/problem/content/1837/
#include <iostream>
#include <math.h>
using namespace std;
int a, b, c;
int main(){
    cin >> a >> b >> c;
    int max_ab = (a + b + abs(a - b)) / 2;  //求ab中最大值
    int max_abc = (max_ab + c + abs (max_ab - c)) / 2;  // 求abc最大值
    cout << max_abc << " eh o maior";
    return 0;
}
//AcWing 617. 距离  https://www.acwing.com/activity/content/problem/content/1838/
#include <iostream>
using namespace std;
int L;
int main(){
    cin >> L;
    double v = 30 / 60.0;
    double time = L / v;
    printf("%.lf minutos", time);
    return 0;
}
//AcWing 618. 燃料消耗  https://www.acwing.com/activity/content/problem/content/1839/
#include <iostream>
using namespace std;
double t, s;  // 这里t与s的取值范围在1到10^7,而在后续计算中t*s取极值时,会有14位有效数字,因此用double防止精度丢失
int main()
{
    cin >> t >> s;
    double l = t*s/12.0;  // 注意,这里一定要除以12.0而不是12,否则计算出来的是个整数,因为t与s没有转换为浮点数,而是直接以整数计算的
    printf("%.3lf", l);
    return 0;
}
//AcWing 656. 钞票和硬币  https://www.acwing.com/activity/content/problem/content/1839/
// 除了如下代码的这种方法,还可以将金额的单位从元转换为分,进而将浮点数转换为整数,就可以方便计算了,也不会出现丢失精度的情况
#include <iostream>
using namespace std;
int main()
{
    double N;
    cin >> N;
    // 分解为钞票
    int nota_100 = N / 100.00;
    N -= 100.00*nota_100;
    int nota_50 = N / 50.00;
    N -= 50.00*nota_50;
    int nota_20 = N / 20.00;
    N -= 20.00*nota_20;
    int nota_10 = N / 10.00;
    N -= 10.00*nota_10;
    int nota_5 = N / 5.00;
    N -= 5.00*nota_5;
    int nota_2 = N / 2.00;
    N -= 2.00*nota_2;
    // 分解为硬币
    int moeda_1 = N / 1.00;
    N -= moeda_1*1.00;
    int moeda_5 = N / 0.50;
    N -= moeda_5*0.5;
    int moeda_25 = N / 0.25;
    N -= moeda_25*0.25;
    int moeda_10 = N / 0.10;
    N -= moeda_10*0.10;
    int moeda_05 = N / 0.05;
    N -= moeda_05*0.05;
    // int moeda_01 = N / 0.01;  // 到达这一步,若N是0.01那么输出的moeda理论上应该为1,但是实际上为0,因为精度问题,0.01在double中存储的是0.99999999999999,故最后输出是0而不是1
    float moeda_01 = N / 0.01;  // 用float定义moeda_01变量,避免了int类型丢失0.01的情况
    cout << "NOTAS:" << endl;
    cout << nota_100 << " nota(s) de R$ 100.00" << endl;
    cout << nota_50 << " nota(s) de R$ 50.00" << endl;
    cout << nota_20 << " nota(s) de R$ 20.00" << endl;
    cout << nota_10 << " nota(s) de R$ 10.00" << endl;
    cout << nota_5 << " nota(s) de R$ 5.00" << endl;
    cout << nota_2 << " nota(s) de R$ 2.00" << endl;
    cout << "MOEDAS:" << endl;
    cout << moeda_1 << " moeda(s) de R$ 1.00" << endl;
    cout << moeda_05 << " moeda(s) de R$ 0.50" << endl;
    cout << moeda_25 << " moeda(s) de R$ 0.25" << endl;
    cout << moeda_10 << " moeda(s) de R$ 0.10" << endl;
    cout << moeda_05 << " moeda(s) de R$ 0.05" << endl;
    // cout << moeda_01 << " moeda(s) de R$ 0.01" << endl; // double可能会出现吧0.1丢失问题
    printf("%.0f moeda(s) de R$ 0.01", moeda_01);  // 这里输出因为moeda_01是float类型,然而要求的输出是整数个硬币,因此控制小数有效位为0,即可
    return 0;
}
//AcWing 655. 天数转换  https://www.acwing.com/activity/content/problem/content/1841/
#include <iostream>
using namespace std;
int day, ano, mes, dia;
int main(){
    cin >> day;
    ano = day / 365;
    mes = (day % 365) / 30;
    dia = ((day % 365) % 30) ;
    cout << ano << " ano(s)" << endl;
    cout << mes << " mes(es)" << endl;
    cout << dia << " dia(s)" << endl;
    return 0;
}
												
											ACwing语法基础课第一节课例题与习题及个人总结的更多相关文章
- [iOS]Objective-C 第一节课
		
Objective-C 第一节课 本节课的主要内容 创建Objective-C的第一个工程 HelloWorld Objective-C中的字符串 创建Objective-C的第一个工程 打开Xcod ...
 - centos mysql  实战  第一节课   安全加固  mysql安装
		
centos mysql 实战 第一节课 安全加固 mysql安装 percona名字的由来=consultation 顾问+performance 性能=per con a mysql ...
 - Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig  CentOS远程连接  Linux中的输入流 第一节课
		
Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中 ...
 - Java第一节课动手动脑
		
在第一节课的动手动脑中,主要解决四则运算问题. 首先第一个是出30道四则运算题目,在100以内.这个问题需要控制随机数生成的范围和结果的范围在100以内就可以. 第一次改进是3点:一为避免重复,二为定 ...
 - 左神算法第一节课:复杂度、排序(冒泡、选择、插入、归并)、小和问题和逆序对问题、对数器和递归(Master公式)
		
第一节课 复杂度 排序(冒泡.选择.插入.归并) 小和问题和逆序对问题 对数器 递归 1. 复杂度 认识时间复杂度常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数 ...
 - JAVAWEB第一节课的课后思考
		
第一开发一个网站需要的一些技术 至少熟悉一种建站程序.(html,javascript等等)对空间和域名的知识有一定的了解.有一些美工基础(例如ps设计等等).对编程有一些了解.HTML的代码知识基本 ...
 - springboot的第一节课
		
快速开始spring boot应用 官方向导搭建boot应用 地址:http://start.spring.io/ 设置项目属性: 3.解压,拷贝到工作空间,导入maven项目 4.写Controll ...
 - JavaScript第一节课
		
1.用法:位于<script></script>可以位于body和head中,不限制标签数量,也可以创建外部Js文件,然后引入.(引入方法:<script src=&qu ...
 - 【皇甫】☀Struts_第一节课
		
本章讲解内容: DTD是Docunent Type Defintion的缩写,即文档类型定义.DTD用来描述XML文档结构. DOM4J是一个非常优秀的javaXML API,具有性能优异,功能强大和 ...
 - 初学Python——第一节课
		
一.Python语言的特性: 1.与C语言不同,Python语言是一门解释性语言.程序在执行过程中,执行一步.编译一步. 2.Python是一个动态类型语言,不需要定义变量的数据类型. 3.Pytho ...
 
随机推荐
- jmeter 添加断言和查看断言结果
			
在对应的请求下添加响应断言,这里我们添加响应文本来作为检查点,来检查上面的这个请求是否成功 断言和断言结果是成对出现的,是为了检查我们添加的断言是否验证成功,如下图,如果成功,里面就会有对应的结果,且 ...
 - 怎么在CAD表格中画斜线?CAD表格斜线一分为二绘制步骤
			
在Excel表格中经常能看到用一条斜线将单元格一分为二,那么,你知道怎么在CAD表格中画斜线吗?本文小编就以浩辰CAD软件为例来给大家分享一下CAD表格斜线一分为二的绘制步骤,一起来看看吧! CAD表 ...
 - Python——02.变量及标识符
			
变量概念: -- 字面量:与字面上显示值一致的量称作字面量,在程序中可直接使用字面量:abc,123, 我是XX,等等 -- 变量:变量可通过赋值保存字面量,变量是可变的,跟随赋值的字面量不同而变化 ...
 - 【广告】UEOI 招聘减章
			
欢迎加入UEOI! 招聘目标:>=25人 各位++rp.
 - VSCode搭建Go语言环境
			
一.安装go 1. 获取go安装包 https://golang.org/dl/ 2. 本地安装(省略) 3. 配置和环境变量 GO111MODULE 是否支持gomod GOROOT go安装的 ...
 - select标签如何实现 每个option传递多个值
			
设计项目时 我们有时候会利用 <select> <option value="值1" > </option> </select> ...
 - HDLbits——Shift18
			
// Build a 64-bit arithmetic shift register, // with synchronous load. The shifter can shift both le ...
 - 【mysql练习】转置,总计,分组
			
1,有如下一个表,要求把这个表按照2016,2017,2018,2019,把从1月到12月的金额展示出来. profit表: year month amount2019 1 52019 3 62018 ...
 - 基于Linux的ssh协议配置sftp
			
sftp采用的是ssh加密隧道,安装性方面较ftp强,而且依赖的是系统自带的ssh服务,不像ftp还需要额外的进行安装 1. 创建sftp组 创建完成之后使用cat /etc/group命令组的信息 ...
 - git reset 加不加 --hard的区别
			
通常我们提交代码一般都是 git add ,git commit -m, git push的这么个流程.添加到暂存区,提交到git库生成版本号,push到远程仓库以供他人可以使用.这是一个完整的且 ...