ACwing语法基础课第一节课例题与习题及个人总结

2023-03-12,,

第一次课例题

若涉及到浮点数的计算,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语法基础课第一节课例题与习题及个人总结的相关教程结束。

《ACwing语法基础课第一节课例题与习题及个人总结.doc》

下载本文的Word格式文档,以方便收藏与打印。