例2.1 输入两个正整数a和b,试交换a,b的值(使a的值等于b,b的值等于a)(三变量法)
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Input a,b=";
cin>>a>>b;
c = a; a = b; b = c;
cout<<"a = "<<a<<" b = "<<b<<endl;
}
例2.2 求3个整数的和
输入a,b,c这3个整数,求它们的和。
样例输入:2 3 5
样例输出:10
#include<iostream>
using namespace std;
int main()
{
int a,b,c,s;
cin>>a>>b>>c;
s = a+b+c;
cout<<s<<endl;
return 0;
}
例2.3
小明买图书
已知小明有n元,他买了一本书,这本书原价为m元,现在打8折出售。求小明还剩多少钱(保留两位小数)。
样例输入:100 100 样例输出:20.00
分析:浮点型变量单精度float 或双精度double
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
double n = 0,m = 0,c = 0,d = 0;
cin>>n>>m;
c = m*0.8;
d = n-c;
printf("%.2lf",d);//保留两位小数,注意double用lf,float用f
return 0;
}
例2.4 变量自加运算
#include<iostream>
using namespace std;
int main()
{
int x = 7,y = 8;
int z1 = y-(x++);
int z2 = y-(++x); //计算z1=1,计算后x=8
cout<<"z1="<<z1<<endl<<"z2="<<z2 ;
}
int main()
{
int n1 = 4,n2 = 5,n3;
n3 = n1>n2;//n3的值为0
n3=n1<n2;//n3的值为1
n3 = n1 ==4;//n3的值变为1
n3 = n1!=4;//n3的值变为0
n3 = n1 == 1+3;//n3的值变为1
}
例2.5
输入半径r,输出圆的直径,周长,面积,数和数之间以一个空格分开,每个数保留小数点后四位
#include<cstdio>
using namespace std;
const double PI = 3.14159;
int main()
{
double r,d,c,s;
scanf("%lf",&r);
d = r*2;
c = 2*PI*r;
s = PI*r*r;
printf("%.4lf %.4lf %.4lf",d,c,s);
return 0;
}
例2.6 数学中经典的“鸡兔同笼”问题,已知头共30个,脚共90只,问笼中的鸡和兔各有多少只?
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int h,f,j,t;
h = 30;f = 90;
j = (4*h-f)/2;//鸡
t = h-j;//兔
cout<<"j="<<j<<" t="<<t<<endl;
return 0;
}
p29的
例2.6 整型数据类型存储空间大小
分别定义 int,short 类型的变量各一个,并依次输出它们的存储空间大小(单位:字节)
#include<iostream>
using namespace std;
int main()
{
int x;
short y; //sizeof返回一个对象或者类型所占内存字节数
cout<<sizeof(x)<<" "<<sizeof(y)<<endl;
return 0;
}
例2.7 计算ASCII码值
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int b = 'A';
b*= 2;
printf("%d",b);
return 0;
}
例2.8 利用getchar函数接受键盘输入
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
char ch = getchar();//读入字符
cout<<"input="<<ch<<endl;
}
字符输出函数putchar
例2.9 利用putchar 函数输出字符
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
char c = 'B';
putchar(c);
putchar('\x42');//转义字符输出字母B
putchar(0x42);//十六进制
putchar(66);//十进制
}
例2.12 某幼儿园里,有5个小朋友编号为1,2,3,4,5,他们按自己的编号顺序围坐在一张圆桌旁。他们身上都有若干个糖果(键盘输入),现在他们做一个分糖果游戏。从1号小朋友·开始,将自己的糖果均分三份(如果有多余的糖果,则立刻吃掉),自己留一份,其余两份分给他的相邻两个小朋友。接着2号,3号,4号,5号小朋友同样这么做。问一轮后,每个小朋友手上分别有多少糖果?
【分析】题目中有5个小朋友,他们初始时糖果数量不确定,用a,b,c,d,e分别存储5个小朋友的糖果数,初始值由键盘输入。
#include<cstdio>
using namespace std;
int main()
{
int a,b,c,d,e;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
a = a/3; b = b+a; e = e+a; //一号小朋友分糖
b = b/3; c = c+a; a = a+b; //二号小朋友分糖
c = c/3; d = d+c; b = b+c; //三号小朋友分糖
d = d/3; e = e+d; c = c+d; //四号小朋友分糖
e = e/3; a = a+e; d = d+e; //五号小朋友分糖
printf("%5d%5d%5d%5d%5d\n",a,b,c,d,e);//%5d按5位宽度输出
return 0;
}
例2.13 输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数
【分析】先求出自然数的个位数,十位数,百位数,然后个位数与百位数对调
#include<iostream>
using namespace std;
int main()
{
int m;
cin>>m;//输入一个三位数
int a = m/100;//百位数
int b = (m/10)%10;//十位数
int c = m%10;//个位数
int n = c*100+b*10+a;//重新组合对调后的数
cout<<"n="<<n<<endl;
}
例2.1某班有男同学x位,女同学y位,x位男生平均分是87分,y位女生的平均分是85,问全体同学平均分是多少分?
#include<iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
cout<<float(x*87+y*85)/(x+y)<<endl;
}
例2.15 歌手大赛上6名评委给一个参赛者打分,六个人打得平均分为9.6分;如果去掉一个最高分,这名参赛者的平均分为9.4分,如果去掉一个最低分,这名参赛者的平均分为9.8分;如果去掉一个最高分和一个最低分,这名参赛者的平均分是多少?
#include<cstdio>
int main()
{
float sc_all = 6*9.6;
float sc_high = 5*9.4;
float sc_low = 5*9.8;
float high = sc_all-sc_high;//求最高分
float low = sc_all-sc_low;//求最低分
float ans = (sc_all-high-low)/4;
printf("%5.2f\n",ans);
}
例2.16海伦公式求三角形面积
#include<cstdio>
#include<math.h>
int main()
{
float a,b,c;
scanf("%f%f%f",&a,&b,&c);
float p = (a+b+c)/2;
float s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%0.3f\n",s);
return 0;
}