20191117-STD::讲解及求平均数例题
-
#include "stdafx.h"
-
#include "cstdio"
-
#include "iostream"
-
#include "algorithm"
-
using namespace std;
-
int main()
-
{
-
char grade='A';
-
switch(grade)
-
{
-
case 'A': cout<<"85~100\n";break;
-
case 'B': cout<<"70~84\n";break;
-
case 'C': cout<<"60~69\n";break;
-
case 'D': cout<<"<60\n";break;
-
default: cout<<"error\n";break;
-
}
-
}
注释掉using namespace std;,代码如下:
-
#include "stdafx.h"
-
#include "cstdio"
-
#include "iostream"
-
#include "algorithm"
-
//using namespace std;
-
int main()
-
{
-
char grade='A';
-
switch(grade)
-
{
-
case 'A': std::cout<<"85~100\n";break;
-
case 'B': std:: cout<<"70~84\n";break;
-
case 'C': std:: cout<<"60~69\n";break;
-
case 'D': std:: cout<<"<60\n";break;
-
default: std:: cout<<"error\n";break;
-
}
-
}
解题思路:
如果不加using namespace std;,则cout等命令前需加上std::,如上例,注释掉了using namespace std;,下面的代码做了相应修改.
下一题,题目:
随机输入三个带小数的数,求平均值:
-
#include "stdafx.h"
-
#include "cstdio"
-
#include "iostream"
-
#include "algorithm"
-
using namespace std;
-
int main()
-
{ float a,b,c;
cin>>a>>b>>c; -
cout<<(a+b+c)/3;
-
}
解题思路:因为是带小数的题,所以用到了FLOAT,求平均数即:(a+b+c)/3