博文

c++入门实例之循环与判断三(2007-03-16 17:10:00)

摘要:求100至200之间的所有素数。素数即不能被   2——其2次方根√m   中的任一个整数整除。 #include<iostream>#include<cmath>using namespace std;int main(){ int m,k,s; for(m=101;m<200;m+=2) {   s=sqrt(m);   for(k=2;k<=s;k++)   {    if(m%k==0)      break;   }  if(m%k!=0)    cout<<m<<"  "; } return 0;}......

阅读全文(2165) | 评论:0

C++入门实例之编程初步二(2007-03-16 14:44:00)

摘要:        这是一个古老的数学问题,有一对免子,从出生第三个月起每个月都生一对兔子,假如兔子都不死的话,求每个月兔子的总对数。这实际上是一个Fibonacci数,即前两个数是1,第三个数起,其后的每个数都是前面两个数之和。即:1    1    2    3    5    8    13......因此可用递归算法实现: #include<iostream>#include<iomanip>using namespace std;int main(){  long f1=1,f2=1;  for(int i=1;i<=10;i++)         //求20个月时每个月兔子的总对数   {       cout<<setw(16)<<f1<<setw(16)<<f2;    //每次输出两个月        f1=f1+f2;        f2=f2+f1;    }  return 0;}         如果要实现某一个月的兔子的总对数,可用函数的递归调用: #include<iostream>#include<iomanip>using namespace std;int main(){  long fac(int );  cout<<"输入要查询的月份:";  int n;  cin>&g......

阅读全文(2208) | 评论:0

c++入门实例之循环与判断二(2007-03-16 13:52:00)

摘要:二、求PI的值。PI/4=1-1/3+1/5-1/7+1/9......直至最后一项的绝对值小于10的负17次方(即1E-7)。我看钱能的书上有不止一种算法,但我也是初学,所以在此只列出一种最简单的算法。        本文旨在掌握for循环与while循环的使用。1、用for循环实现: #include<iostream>#include<cmath>     //由于要用到绝对值函数fabs(),故包含此头文件#include<iomanip>   //格式化输出using namespace std;int main(){ int s=1;double n=1,m,t=0;for(m=1;fabs(n)>1e-7;m+=2,n=s/m)       //m在循体内完成初始化{  t=t+n;  s=-s;}cout<<setiosflags(ios::fixed)<<setprecision(8);cout<<"pi="<<4*t<<endl;return 0;} 2、用while循环实现,只需修改循环体即可: #include<iostream>#include<cmath>     //由于要用到绝对值函数fabs(),故包含此头文件#include<iomanip>   //格式化输出using namespace std;int main(){ int s=1;double n=1,m=1,t=0;    //此处要对m进行初始化为1while(fabs(n)>1e-7){  t=t+n;  s=-s;  m+=2;  n=s/m;}cout<<setiosflags(ios::fixed)<<setprecision(8);c......

阅读全文(2455) | 评论:0

c++入门实例之循环与判断一(2007-03-16 11:00:00)

摘要:一、一运输公司要对用户计算运费,假设每吨每公里的价格为P,货物重量为W,路程为S,折扣为D,其运费计算标准如下:     路程:s<250                     折扣:d=0                250<=s<500                       d=0.02               500<=s<1000                      d=0.05              1000<=s<2000                     d=0.08            &nb......

阅读全文(2354) | 评论:2

C++入门实例之编程初步一(2007-03-15 14:03:00)

摘要:一、求一元二次方程 ax2+bx+c=0 的根:#include<iostream>#include<cmath>#include<iomanip.h>using namespace std;int main(){ float a,b,c; float x1,x2; cout<<"请输入a,b,c的值:"; cin>>a>>b>>c; float t=b*b-4*a*c; if(t<0)   cout<<"此方程无实根."<<endl; else  {    x1=(-b+sqrt(t))/(2*a);    x2=(-b-sqrt(t))/(2*a);    cout<<setiosflags(ios::fixed)<<setiosflags(ios::right);    cout<<setprecision(4);    cout<<"x1= "<<x1<<endl;    cout<<"x2= "<<x2<<endl;  } return 0;}二、判别某一年是否为闰年,满足闰年的条件是:1、能被4整除而不能被100整除,2、能同时被100和400整除。#include<iostream>using namespace std;int main(){  int year;  cout<<"请输入要查询的年份:";  cin>>year;  if((year%4==0 && year%100!=0)||(year%400==0))   cout<<year<<"是闰年"<<endl;  els......

阅读全文(2783) | 评论:0

C++入门实例之数据类型与表达式(2007-03-15 11:46:00)

摘要:一、阅读以下代码,观察输出结果,通过此段代码,可进一步认识    C++中复合    运算符的应用。    #include<iostream>    using namespace std;    int main()将    {     int i,j,m,n;     i=8;     j=10;     m=++i+j++;     n=(++i)+(++j)+m;     cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl;     return 0;     }    通过运行,其输出应为:10    12    19    41二、将“China”设为密码,用原来字母后的第四个字母代替原来的         字母,如:A后的第四个字母是E,用E代替A,因此“China”        应译为  “Gimre” ,编程实现:    #include<iostream>    using namespace std;    int main()    {       char c1,c2,c3......

阅读全文(2213) | 评论:0