课堂
1. Hello World
# include
# include
using namespace std;
int main ( )
{
cout << "Hello World" << endl;
bool false / true ; 1 byte 1 字节 1 Byte= 8 bit 1 字节= 8 比特
char 'a' , 'b' , ' ' , '\n' ; 1 byte
int - 2147483648 ~ 2147483647 4 byte
float 1.23 , 1.23e-2 , 6 - 7 有效数字 4 byte
double 15 - 16 有效数字 8 byte
long long - 2 ^ 63 ~ 2 ^ 63 - 1 8 byte
long double 18 到19 位
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2. A + B
# include
# include
using namespace std;
int main ( )
{
int a, b;
scanf ( "%d%d" , & a, & b) ;
printf ( "a+b=%d\na*b=%d\n" , a+ b, a* b) ;
char a, b;
scanf ( "%c%c" , & a, & b) ;
printf ( "%c %c\n" , a, b) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
3. 浮点数的比较
# include
# include
using namespace std;
const double eps= 1e-6
int main ( )
{
double x = 1.23456789 ;
double a = x* x;
double b = sqrt ( a) ;
printf ( "%.10f\n" , b) ;
if ( fabs ( x- b) < eps) puts ( "相等" ) ;
return 0 ;
}
例题
1. A + B
AcWing 1.A+B
# include
using namespace std;
int main ( )
{
int a, b;
cin>> a>> b;
cout<< a + b<< endl;
return 0 ;
}
2. 差
Acwing 608.差
# include
using namespace std;
int main ( )
{
int a, b, c, d, X;
cin>> a>> b>> c>> d;
X = a* b- c* d;
cout<< "DIFERENCA = " << X << endl;
return 0 ;
}
3. 圆的面积
Acwing 604.圆的面积
# include
# include
using namespace std;
int main ( )
{
double R, pi= 3.14159 ;
scanf ( "%lf" , & R) ;
printf ( "A=%.4lf\n" , pi* R* R) ;
}
Acwing 606.平均数1
# include
using namespace std;
int main ( )
{
double a, b;
scanf ( "%lf%lf" , & a, & b) ;
printf ( "MEDIA = %.5lf" , ( 3.5 * a+ 7.5 * b) / 11.0 ) ;
}
5. 工资
Acwing 609.工资
# include
using namespace std;
int main ( )
{
int a, b;
double c;
scanf ( "%d%d%lf" , & a, & b, & c) ;
printf ( "NUMBER = %d\nSALARY = U$ %.2lf" , a, b* c) ;
}
6. 油耗
Acwing 615.油耗
# include
using namespace std;
int main ( )
{
int a;
double b;
scanf ( "%d%lf" , & a, & b) ;
printf ( "%.3lf km/l" , a/ b) ;
}
7. 两点间的距离(cmath)
Acwing 616.两点间的距离
# include
# include
using namespace std;
int main ( )
{
double x1, y1, x2, y2;
scanf ( "%lf%lf%lf%lf" , & x1, & y1, & x2, & y2) ;
printf ( "%.4lf\n" , sqrt ( ( x1- x2) * ( x1- x2) + ( y1- y2) * ( y1- y2) ) ) ;
return 0 ;
}
8. 钞票
Acwing 653.钞票
# include
using namespace std;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
printf ( "%d\n" , n) ;
printf ( "%d nota(s) de R$ 100,00\n" , n/ 100 ) ;
n%= 100 ;
printf ( "%d nota(s) de R$ 50,00\n" , n/ 50 ) ;
n%= 50 ;
printf ( "%d nota(s) de R$ 20,00\n" , n/ 20 ) ;
n%= 20 ;
printf ( "%d nota(s) de R$ 10,00\n" , n/ 10 ) ;
n%= 10 ;
printf ( "%d nota(s) de R$ 5,00\n" , n/ 5 ) ;
n%= 5 ;
printf ( "%d nota(s) de R$ 2,00\n" , n/ 2 ) ;
n%= 2 ;
printf ( "%d nota(s) de R$ 1,00\n" , n/ 1 ) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Acwing 654.时间转换
# include
using namespace std;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
printf ( "%d:%d:%d" , n/ 3600 , n% 3600 / 60 , n% 3600 % 60 ) ;
return 0 ;
}
习题
1. 简单乘积
Acwing 605.简单乘积
# include
using namespace std;
int main ( )
{
int a, b;
cin>> a>> b;
cout<< "PROD = " << a* b<< endl;
return 0 ;
}
2. 简单计算
Acwing 611.简单计算
# include
# include
using namespace std;
int main ( )
{
double n1, s1, p1;
double n2, s2, p2;
cin >> n1 >> s1 >> p1;
cin >> n2 >> s2 >> p2;
printf ( "VALOR A PAGAR: R$ %.2lf\n" , s1* p1+ s2* p2) ;
}
3. 球的体积
Acwing 612.球的体积
# include
# include
using namespace std;
int main ( )
{
double R, pi= 3.14159 ;
scanf ( "%lf" , & R) ;
printf ( "VOLUME = %.3lf\n" , ( 4 / 3.0 ) * pi* R* R* R) ;
return 0 ;
}
4. 面积
Acwing 613.面积
# include
# include
using namespace std;
int main ( )
{
double a, b, c, pi= 3.14159 ;
scanf ( "%lf%lf%lf" , & a, & b, & c) ;
printf ( "TRIANGULO: %.3lf\n" , 0.5 * a* c) ;
printf ( "CIRCULO: %.3lf\n" , pi* c* c) ;
printf ( "TRAPEZIO: %.3lf\n" , 0.5 * ( a+ b) * c) ;
printf ( "QUADRADO: %.3lf\n" , b* b) ;
printf ( "RETANGULO: %.3lf" , a* b) ;
return 0 ;
}
5. 平均数2
Acwing 607.平均数2
# include
using namespace std;
int main ( )
{
double a, b, c;
scanf ( "%lf%lf%lf" , & a, & b, & c) ;
printf ( "MEDIA = %.1lf" , ( 2 * a+ 3 * b+ 5 * c) / 10.0 ) ;
}
6. 工资和奖金
Acwing 610.工资和奖金
# include
# include
# include
using namespace std;
int main ( )
{
string name;
cin >> name;
double a, b;
scanf ( "%lf\n%lf\n" , & a, & b) ;
printf ( "TOTAL = R$ %.2lf\n" , a+ b* 0.15 ) ;
return 0 ;
}
Acwing 614.最大值
# include
# include
# include
using namespace std;
int main ( )
{
int a, b, c, m, n;
scanf ( "%d%d%d" , & a, & b, & c) ;
m = ( a+ b+ abs ( a- b) ) / 2 ;
n = ( c+ m+ abs ( c- m) ) / 2 ;
printf ( "%d eh o maior" , n) ;
return 0 ;
}
8. 距离
Acwing 617.距离
# include
# include
using namespace std;
int main ( )
{
int L, t;
cin >> L;
t = L* 2 ;
printf ( "%d minutos" , t) ;
return 0 ;
}
9. 燃料消耗
Acwing 618.燃料消耗
# include
# include
using namespace std;
int main ( )
{
double a, b;
cin >> a >> b;
printf ( "%.3lf" , a* b/ 12.0 ) ;
return 0 ;
}
10. 钞票和硬币
Acwing 656.钞票和硬币
# include
# include
using namespace std;
int main ( )
{
double n;
cin >> n;
int m= n* 100 ;
cout << "NOTAS:" << endl;
cout<< m/ 10000 << " nota(s) de R$ 100.00" << endl;
m = m% 10000 ;
cout<< m/ 5000 << " nota(s) de R$ 50.00" << endl;
m = m% 5000 ;
cout<< m/ 2000 << " nota(s) de R$ 20.00" << endl;
m = m% 2000 ;
cout<< m/ 1000 << " nota(s) de R$ 10.00" << endl;
m = m% 1000 ;
cout<< m/ 500 << " nota(s) de R$ 5.00" << endl;
m = m% 500 ;
cout<< m/ 200 << " nota(s) de R$ 2.00" << endl;
m = m% 200 ;
cout << "MOEDAS:" << endl;
cout<< m/ 100 << " moeda(s) de R$ 1.00" << endl;
m = m% 100 ;
cout<< m/ 50 << " moeda(s) de R$ 0.50" << endl;
m = m% 50 ;
cout<< m/ 25 << " moeda(s) de R$ 0.25" << endl;
m = m% 25 ;
cout<< m/ 10 << " moeda(s) de R$ 0.10" << endl;
m = m% 10 ;
cout<< m/ 5 << " moeda(s) de R$ 0.05" << endl;
m = m% 5 ;
cout<< m/ 1 << " moeda(s) de R$ 0.01" << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
11. 天数转换
Acwing 655.天数转换
# include
using namespace std;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
printf ( "%d ano(s)\n%d mes(es)\n%d dia(s)\n" , n/ 365 , n% 365 / 30 , n% 365 % 30 ) ;
return 0 ;
}