信息学奥赛一本通(C++版)第二章 顺序结构程序设计

本文介绍了C++编程中顺序结构程序设计的相关知识,包括基本运算符、常量变量、数据类型及其存储空间大小、浮点数处理、输入输出操作,通过实例如A+B问题、浮点数相除余、计算几何问题等,帮助读者深入理解C++的顺序结构程序设计。

第一节 运算符和表达式:

1006:A+B问题

#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int a,b,c;
	cin>>a>>b;
	c=a+b;
	cout<<c<<endl;
	return 0; 
}

1007:计算(a+b)×c的值 

#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<(a+b)*c<<endl;
	return 0;
}

 1008:计算(a+b)/c的值

#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<(a+b)/c<<endl;
	return 0;
}

1009:带余除法

#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int a,b,c,d;
	cin>>a>>b;
	c=a/b;
	d=a%b;
	cout<<c<<" "<<d;
	return 0;
}

1010:计算分数的浮点数值

#include <bits/stdc++.h> 
using namespace std;
int main()
{ 
    double a,b,c; 
    scanf("%lf %lf",&a, &b);
	c=a/b;
    printf("%.9lf", c);
	return 0;
}

 第二节 常量和变量:

 1011:甲流疫情死亡率

刚开始编译错误,发现头文件打错了,改正如下↓

#include <bits/stdc++.h>
using namespace std;
int main()
{
double a,b,c;
cin>>a>>b;
c=b/a*100;
printf("%.3lf%%",c);
return 0;
}

1012:计算多项式的值

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double a,b,c,d,x,y;
    cin>>x>>a>>b>>c>>d;
    cout<<fixed<<setprecision(7)<<a*x*x*x+b*x*x+c*x+d;
    return 0;
}

 1013:温度表达转化

#include<bits/stdc++.h>
using namespace std;
int main()
{
double f,c;
cin>>f;
c=5*(f-32)/9;
printf("%.5lf",c);
return 0;

}

1014:与圆相关的计算

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double r,d,C,S;
	scanf("%lf",&r);
	d=r*2;
	C=2*3.14159*r;
	S=3.14159*r*r;
	printf("%.4lf %.4lf %.4lf",d,C,S);
	return 0;
}

1015:计算并联电阻的阻值

#include<bits/stdc++.h>
using namespace std;
int main()
{
float r1,r2,r;
cin>>r1>>r2;
r=1/(1/r1+1/r2);
cout<<fixed<<setprecision(2)<<r<<endl;//输出格式化,保留两位小数
return 0;

}

第三节 标准数据类型:

1414:【17NOIP普及组】成绩

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,he;
	scanf("%d%d%d",&a,&b,&c);
	a=a/10*2;
	b=b/10*3;
	c=c/10*5;
	he=a+b+c;
	printf("%d",he);
	return 0;
}

1016:整型数据类型存储空间大小

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a;
    short b;
    cout<<sizeof(a)<<" "<<sizeof(b)<<endl;//定义:sizeof是计算对象所占的字节数,通常用来查看变量、数组或结构体等所占字节个数的操作运算符,不知道的可以自己上网查。
    return 0;
}

1017:浮点型数据类型存储空间大小

#include<bits/stdc++.h>
using namespace std;
int main()
{
    float a;
    double b;
    cout<<sizeof(a)<<" "<<sizeof(b)<<endl;
    return 0;
}

1018:其他数据类型存储空间大小

#include<bits/stdc++.h>
using namespace std;
int main()
{
    bool a;
    char b;
    cout<<sizeof(a)<<" "<<sizeof(b)<<endl;
    return 0;
}

1019:浮点数向零舍入

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a;
	double b;
	cin>>b;
	a=b;
	cout<<a;
	
return 0;
}

1020:打印ASCII码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a;
    cin>>a;
    cout<<int(a);
    return 0;
}

1021:打印字符

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a;
    scanf("%d",&a);
    printf("%c",a);
    return 0;
}

1022:整型与布尔型的转换

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
bool x;
scanf("%d",&a);
x=a;
b=x;
printf("%d",b);
return 0;

}

1023:Hello,World!的大小

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
a=sizeof("Hello, World!");
printf("%d",a);
return 0;

}

第四节 数据输入输出:

1024:保留3位小数的浮点数

#include<bits/stdc++.h>

using namespace std;

int main()

{
	float a;
	scanf("%f",&a);
	printf("%.3f",a);
	
return 0;
}

1025:保留12位小数的浮点数

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double a;
	scanf("%lf",&a);
	printf("%.12lf",a);
	
return 0;
}

1026:空格分隔输出

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
char b;
float c;
double d;
scanf("%c%d%f%lf",&b,&a,&c,&d);
printf("%c ",b);
printf("%d ",a);
printf("%.6f ",c);
printf("%.6f",d);
return 0;

}

1027:输出浮点数

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double a;
	scanf("%lf",&a);
	printf("%f\n",a);
	printf("%.5f\n",a);
	printf("%e\n",a);
	printf("%g\n",a);
	return 0;
}

1028:字符菱形

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a;
    cin>>a;
    cout<<" "<<" "<<a<<" "<<" "<<endl;
    cout<<" "<<a<<a<<a<<" "<<endl;
    cout<<a<<a<<a<<a<<a<<endl;
    cout<<" "<<a<<a<<a<<" "<<endl;
    cout<<" "<<" "<<a<<" "<<" "<<endl;
    return 0;
}

第五节 顺序结构实例:

1029:计算浮点数相除的余

#include<bits/stdc++.h>
using namespace std;
int main()
{
double a,b;
int k;
cin>>a>>b;
k=a/b;
a=a-k*b;
printf("%g",a);
return 0;
}

1030:计算球的体积

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double r,V,pi;
	pi=3.14;
	scanf("%lf",&r);
	V=pi*r*r*r*4/3;
	printf("%.2lf",V);
return 0;
}

1031:反向输出一个三位数

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,g,s,b;
	cin>>a;
	g=a%10;
	s=a%100/10;
	b=a/100;
	cout<<g<<s<<b;
	return 0;
}

1032:大象喝水查

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double pi , v , water ;
    int h, r , tong ;
    pi = 3.14159;
    scanf("%d%d", &h , &r );
    v = pi*r*r*h/1000.0;//记住这是/1000.0 不是1000
    water = 20.0/v;//同理20.0
    tong = ceil(water);//ceil的作用是向上取整
    printf("%d\n" , tong );
    return 0;
}

1033:计算线段长度

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << fixed << setprecision(3) << sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) << endl;//这里前面解释过
    return 0;
}

1034:计算三角形面积

#include<bits/stdc++.h>
using namespace std;
int main()
{
    float x1=0,x2=0,x3=0,y1=0,y2=0,y3=0;
    double a=0,b=0,c=0,area=0,p=0;//面积计算后有可能越界float
    scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
    a=sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );//a边边长
    b=sqrt( (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) );//b边边长
    c=sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) );//c边边长
    p=(a+b+c)/2.0;//半周长
    area=sqrt( p*(p-a)*(p-b)*(p-c) );//海伦公式
    printf("%.2lf",area);
    return 0;
}

1035:等差数列末项计算

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d;
scanf("%d%d%d",&a,&b,&c);
d=(b-a)*(c-1)+a;
printf("%d",d);
return 0;
}

1036:A×B问题

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a,b,c;
	cin>>a >>b;
	c=a*b;
	cout<<c;
	return 0;
}

1037:计算2的幂

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	cin>>a;
	b=pow(2,a);
	cout<<b<<endl;
	return 0;
}

1038:苹果和虫子

#include<bits/stdc++.h>
using namespace std;
int main ()
{
    int n,x,y,a;
    scanf("%d%d%d",&n,&x,&y);
    a=n-ceil(y*1.0/x);
    if(a<0){
        printf("%d",0);
        }
    else{
        printf("%d",a);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

harry04

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值