Ex3_2

来源:互联网 发布:网络儿童英语 编辑:程序博客网 时间:2024/06/10 01:54
// Ex3_2_2.cpp : Defines the entry point for the console application.//#include "stdafx.h" void main() {int n,p2,p3,i;long s,m[3000]; m[1]=1;s=1;p2=1;p3=1;       // 排头p2,p3赋初值 printf("  请输入n: "); scanf("%d",&n);for(i=2;i<=n;i++)if(2*m[p2]<3*m[p3]){ m[i]=2*m[p2]+1; s+=m[i];p2++;}else if(2*m[p2]>3*m[p3]){ m[i]=3*m[p3]+1; s+=m[i];p3++;}  else{m[i]=3*m[p3]+1; s+=m[i];p3++;p2++; }  printf("  m(%d)=%ld\n",n,m[n]);  printf("  s=%ld\n",s);/*  for(int j=1;j<n;j++)  {  if(m[j]>m[j+1])  {    printf("m[%d]>m[%d]\n",j,j+1);break;  }  else if(m[j]==m[j+1])  {    printf("m[%d]=m[%d]\n",j,j+1);break;  }  }*/ }/*请输入n: 2011m(2011)=19821s=17941009Press any key to continue */