lvshengwei  
日历
<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 0
  • 文章 - 4
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿

文章档案

搜索

  •  

最新评论

 

今天开始复习cpp;

1.基本形式:(以helloworld为例)

#include <iostream>

using namespace std;

int main()

{

cout<<"helloworld"<<endl;

system ("pause");

return 0;

}

2.标准文件输入输出形式:

#include <fstream>

using namespace std;

ifstream cin("a.in");

ofstream cout("a.out");

int main()

{cout<<"helloworld"<<endl;

return 0;

}

3.基本结构(顺序,选择,循环)

e.g(全部来自www.marcool.net或周末上课材料);

①三座小山

请在屏幕上打印一个如下图的三层小山,注意,每行后面不要有多于空格。

#include <iostream>
using namespace std;
int main()
{
cout<<"  *"<<endl<<" ***"<<endl<<"*****";
return 0;

}

②天数问题

在屏幕上输入一个年份,月份,程序运行输出该月的天数

#include <iostream>
using namespace std;
int main()
{
   int y,m;
   cin>>y>>m;
   if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)cout<<31<<endl;
   else if(m==4||m==6||m==9||m==11)cout<<30<<endl;
   else if(y%400==0||(y%100&&y%4==0))cout<<29<<endl;
   else cout<<28<<endl;
return 0;
}

③过生日

月底王老师家的宝宝要过生日了,王老师很期待第一次在宝宝出生的日期给宝宝过生日。为了帮助爱子心切的王老师,你来编一个程序来计算宝宝第一次过生日的日期吧。

#include<iostream>
using namespace std;
int a,b,c;
int main()
{
 cin>>a>>b>>c;
    if(b==2&&c==29)
    {
   if((a+4)%100==0&&(a+4)%400!=0) 
   {
    cout<<a+8<<" "<<b<<" "<<c<<endl;
   }
   else cout<<a+4<<" "<<b<<" "<<c<<endl;
 }
    else cout<<a+1<<" "<<b<<" "<<c<<endl;
    //system("pause");
    return 0;
}

*注意:只考虑闰年还不够,因为生日若是1996 2 29应输出2004 2 29,相差八年;

 4.素数问题

①判断是否素数

#include <iostream>

#include <math.h>

using namespace std;

int main()

{int n,i;

 bool bj=true;

cin>>n;

for(i=2;i<sqrt(n);i++)

if(n%i==0)bj=false;

if(bj==true)cout<<"yes";

else cout<<"no";

return 0;

}

②m至n之间素数

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    //输入m和n ,求m至n间的质数 
    int i,k,x,m,n;
    bool bj;
    cin>>m>>n;
    if(m>n) swap(m,n);
    for(x=m;x<=n;x++)
    {
        //一个数判断 
    bj=true;
    k=sqrt(x);
    for(i=2;i<=k;i++)
    {
        if(x%i==0)
        {
            bj=false;
            break;
        }
    }
    if(bj && x!=1)
    {
        cout<<x<<" "<<endl;
    }
    }
    system("pause");
    return 0;
}

③验证歌德巴赫猜想

【题目描述】

在1742年,德国数学家哥德巴赫在给欧拉的信中提出了著名的哥德巴赫猜想:

“任意一个大于4的偶数都可以表示成两个素数之和”

举几个简单的例子:

  • 8 = 3 + 5.
  • 20 = 3 + 17 = 7 + 13.
  • 42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

这个猜想至今都没有被证明,如今你的任务是用计算机证明在1000000之内哥德巴赫猜想的正确性。

【输入格式】

输入文件名:goldbach.in

一个偶数N, 4 < N <= 1000000

【输出格式】

输出文件名:goldbach.out

N的分解式N = X + Y,其中X和Y要求是素数且X < Y,若有多种分解方式,输出X最小的那种。

【样例输入】

8

【样例输出】

8 = 3 + 5

(注意空格!!!)

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long int i,j,x,n,a,b;
bool bj;
cin>>n;
for(x=2;x<n;x++)
{
                  bj=true;
    for(j=2;j<=sqrt(x);j++)
    {
         if(x%j==0)
         {
                   bj=false;
                   break;
                   }
      }
      if(bj==true  && x!=1)
        {a=x;
          if(n-a==x)
          b=n-a;
   { if(a>b)
      {swap(a,b);}
      cout<<n<<" "<<" = "<<a<<" + "<<n-a<<endl;
      }
}
}
system ("pause");
return 0;
}

5.其他数学问题

①数列(循环即可);

fib:1 1 2 3 5 8 13 21 34 ...
输入fib数列的项数n(n>3) ,输出该数列的第n项。
1    1      2     3     5     8
a    b      c
      a     b      c
             a     b      c
int i,a,b,c,n;
cin>>n;
a=1;
b=1;
for(i=3;i<=n;i++)
{
     c=a+b;
     a=b;
     b=c;

cout<<c;

②最大公约数(辗转相除法)

#include <iostream>
using namespace std;
int main()
{
     int a,b,c;
     cin>>a>>b;
  //   if(a>b)swap(a,b);
     c=a%b;
     while(c!=0)
     {a=b;
     b=c;
      c=a%b;
    }
    cout<<b<<endl;  
    return 0;
}

③翻转数(背熟)

#include <iostream>
using namespace std;
//ifstream cin("reverse.in");
//ofstream cout("reverse.out");
int main()
{

     int x,y;
     cin>>x;
     y=0; 
     while(x!=0) 
     { 
         y=y*10+x%10; 
         x=x/10; 
     } 
     cout<<y<<endl; 
  //   system("pause");
     return 0;
}

④最值问题
输入n个数(0~1000),输出这些数的最大值和最小值?
方法一:1+n-1
int i,n,x,zd;
cin>>n;
//第一个数假定为最大
cin>>x;
zd=x;
//从第二个数开始打擂台
for(i=2;i<=n;i++)
{
     cin>>x;
     if (x>zd )zd=x;

}
cout<<zd<<endl;
方法二:
int i,x,n,zd;
cin>>n;
zd=0;//此处可假定小于等于输入数据的最小值
for(i=1;i<=n;i++)
{
   cin>>x;
   if(x>zd) zd=x;
}
cout<<zd;

以上基本简单内容必须用熟。

return 0;

posted on 2012-07-30 19:01 Samuel-Lv 阅读(89) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


 
Copyright © Samuel-Lv Powered by: 博客园 模板提供:沪江博客