s*=i;
returns;
}
voidmain()
{intk,n,s;
cin>>n;
s=0;
for(k=0;k<=n;k++)
s+=fun(n);
cout<
}
6.用牛顿的迭代法求一元方程的在x=1,5附近你的根,要求精度为10》-8。
#include
#include
intlove()
{floata=1.5;
floatb=3;
floaty;
do
{y=(a+b)/2;
if((2*pow(y,3)-4*pow(y,2)+3*b-6)<0)
a=y;
else
b=y
}while(fabs(a-b)returny;
}
voidmain()
{cout<<"方程的根为:
\n";
cout<}
思路:
本题主要是运用迭代法进行阶乘的运算。
也需要关注一下其他方法求阶乘
7、编写程序,求过函数调用,求正整数m,n的最大公约数和最小公倍数。
#include
intlove(inta,intb)
{intt;
if(a
{t=a;
a=b;
b=t;
}
if(a%b==0)
returnb;
love(b,a/b);
}
voidmain()
{inta,b,c;
cout<<"请输入一个数:
\n";
cin>>a;
cout<<"请输入一个数:
\n";
cin>>b;
c=love(a,b);
cout<<"两个数的最大公约数是:
"<cout<<"两个数的最小公倍数:
"<<(a*b)/c;
}
思路:
本题主要运用的是牛顿的迭代法。
8、.编写程序,通过函数调用,判断任一整数是否是回文数。
#include
boollove(char*p);
voidmain()
{char*i;
cout<<"请输入数字:
\n";
cin>>i;
love(i);
}
boollove(char*p)
{char*u;
u=p;
while(u)
{u++;
}
u--;
while(u==p)
{if(*u!
=*p)
return0;
p++;
u--;
}
return1;
}
总结:
本题主要抓住用指针数组来进行回文数的判定。
要注意指针数组和数组指针的区别。
#include
voiddelsame(chara[],n)
{foe(inti=0;iif(a[i]==a[i+1])
{for(intj=i+1;jm--;i--;
}
}
voidmain()
{char*p;
cout<<";
2011年秋
改错题:
#include
#include
#include
intprimes[25];
intisPrimes(intn)
{for(inti=2;iif(n%i==0)return0;
return1;
}
intfun(inti,int&j,int&k)
{intsum=0;
j=k=0;
while(k
{sum+=primes[k];
if(sum>primes[i])
{j++;
sum=0;
}
else
if(sumk++;
else
return1;
}
return0;
}
voidmain()
{inti,j,low,high,count=0;
for(i=2;i<100;i++)
if(!
isPrimes(i))
primes[count++]=i;
cout<<"小于100的素数中满足条件的素数为:
"<for(i=0;iif(fun(i,low,high))
{cout<for(j=low;jcout<cout<
}
试着定义一个类ID,实现根据某人的身份证号码判断其所在年份的虚岁年龄。
#include
#include
classID
{charid[19];
intage;
public:
IDchar(char*p)
{strcpy(id,p);age=0;}
intsubstr(char*p0,charn0,intn)
{ints=0;
char*p=p0+n0;
for(inti=0;i{s=s*10+(*p-'0');
p++;
}
}
returns;
}
voidfun(inty1)
{inty;
y=subsr(id,6,4);
cout<<"指定年份:
"<age=y1-y+1;
}
voidprint()
{cout<<"身份证:
"<cout<<"虚岁年龄:
"<};
voidmain()
{inty;
charstr[20];
cout<<"请输入指定的年份和身份证号码:
";
cin>>y>>str;
IDtest(str);
test.fun(y);
test.print();
}
2011年秋
改错题:
#include
#include
voidprocess(char*str,int&maxlen,char*subs)
{char*ptr=str,c;
intlen;
maxlen=0;
while(*ptr)
{len=1;
while(*ptr==*(ptr+1))
{len++;
ptr++;
}
if(len>maxlen)
{maxlen=len;
c=*ptr;
}
ptr++;
}
for(inti=0;i*(subs+i)=c;
*subs='0';
}
voidmain()
{char*text,subt[100];
intlen;
text=newchar[100];
cout<<"请输入字符:
";
cin.getline(text,100);
process(text,len.subt);
cout<<"该字符串的最长平台的长度为:
"<cout<<"该字符串的最长平台:
"<delete[]text;
}
编程:
定义一个类num,实现求大于整数m的最小