2. 任意输入三条边(a,b,c实型),若能构成三角形,则计算并输出其面积,否则输出标志“No Triangle!”
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if(a + b > c && a + c > b && b + c > a)
{
int t = (a + b + c) / 2;
cout << "palm=" << sqrt(t*(t - a)*(t - b)*(t - c)) << endl;
}
else
cout << "No Triangle!" << endl;
return 0;
}
3. 输入一段英文(以符号“$”表示文章输入结束),分别统计出其中元音字母的个数。
#include <iostream>
#include <cmath>
using namespace std;
double squareN(double i, double neg){
if(i == 1)
return neg;
else
return neg * squareN(i - 1, neg);
}
double eternal(double st){
if(st > 0)
return st;
else
return -st;
}
int main()
{
int i = 0;
double pi = 1;
double stop;
do{
++i;
if(i % 2 == 0)
stop = 1.0 / (2.0*i + 1.0);
else
stop = -(1.0 / (2.0*i + 1.0));
pi += stop;
}while(eternal(stop) > 0.000001);
pi *= 4;
cout << pi << endl;
return 0;
}
3.编程序输出各种星号图。
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 11;
for(int i = 0; 2*i + 1 <= n; ++i){
for(int j = 5 - i; j != 0; --j)
cout << " ";
for(int k = 2*i + 1; k != 0; --k)
cout << "*";
cout << endl;
}
return 0;
}
4. 编程序生成一张从英尺到米的转换表( 1 米大约等于 3.28 英尺),以方便工厂里工人师傅使用。输出形式如下图所示,第 i 行第 j 列的值 v ij代表 i*10+j 英尺对应的米数。每输出 10 英尺的转换表后,另起一行输出。(提示:为使输出美观,可以在各项中间用 ’\t’分隔,或者用教材 p53 讲的输入输出控制流。)