c++基础例题100道Word格式文档下载.docx
- 文档编号:13385392
- 上传时间:2022-10-10
- 格式:DOCX
- 页数:83
- 大小:39.04KB
c++基础例题100道Word格式文档下载.docx
《c++基础例题100道Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《c++基础例题100道Word格式文档下载.docx(83页珍藏版)》请在冰豆网上搜索。
i="
endl;
}
}
/*2、一个数如果恰好等于它的因子之和,这个数就称为"
完数"
例如,6的因子为1、2、3,而6=1+2+3,因此6是"
编程序找出1000之内的所有完数,并按下面的格式输出其因子:
6-〉1,2,3*/
voidmain()
inti,j,sum=0,a[50],k,t;
for(i=1;
=1000;
sum=0;
for(j=1;
j<
i;
j++)
{
if(i%j==0)
{
sum+=j;
a[k++]=j;
}
}
t=k;
if(sum==i)
->
;
for(k=0;
k<
t;
k++)
cout<
a[k];
if(k<
t-1)cout<
"
k=0;
}
/*3、求Sn=a+aa+aaa+...+aa...a之值,其中a是一个数字。
例如:
2+22+222+...+22222(此时n=5),n由键盘输入。
*/
doublea,sn=0.0,sum=0.0;
intn,i;
cout<
pleaseinputanumber"
cin>
>
a;
pleaseinputnnumber"
n;
sn=a;
sum=a;
for(i=2;
=n;
sum=sum*10+a;
sn+=sum;
Sn="
sn<
/*4、一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。
求它在第10次落地时,共经过了多少米?
第10次反弹多高?
{
doubleh1=100,h2=100,sum=0.0;
inti;
=10;
i++)
sum+=h2;
h1=h1/2.0;
h2=h1*2;
sum="
sum<
"
h1="
h1<
/*5、猴子吃桃问题。
猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。
第二天早上又将剩下的桃子吃掉了一半,又多吃了一个。
以后每天早上都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩一个桃子了。
求第一天共摘了多少桃子。
intnumber,i;
number=1;
for(i=10;
i>
1;
i--)
number=(number+1)*2;
number="
number<
第4章函数
/*1、写一函数用"
气泡法"
对输入的10个字符按由小到大的顺序排列。
inti,j,temp,a[10];
pleaseinputtennumbers:
\n"
for(i=0;
10;
cin>
a[i];
i++)//每循环一次确定数组中一个数的位置
for(j=i+1;
j++)//每次循环对比一个数的大小
if(a[i]>
a[j])
temp=a[j];
a[j]=a[i];
a[i]=temp;
cout<
resortresult="
for(i=0;
a[i]<
/*2、用递归方法求n阶勒让得多项式的值,递归公式为
1(n=0)
Pn(x)=x(n=1)
((2n-1)*x*Pn-1(x)-(n-1)*Pn-2(x))/n(n>
1)*/
doublefun(double,double);
doublen,x,sum;
inputnandx"
n>
x;
sum=fun(n,x);
P"
n<
("
x<
)"
="
}
doublefun(doublen1,doublex1)
if(n1==0)
return1;
elseif(n1==1)
returnx1;
elseif(n1>
1)
return((2*n1-1)*x1*fun(n1-1,x1)-(n1-1)*fun(n1-2,x1))/n1;
/*3、编写一函数,由实参传来一字符串,统计此字符串中字母、数字、空格、和其它字符的个数,并在主函数中输入字符串以及输出上述结果。
voidjudge(chara[]);
constintsize=100;
chara[size];
cin.getline(a,size);
judge(a);
voidjudge(chara[100])//判断字符类型
intletter=0,number=0,others=0,i=0;
while(a[i]!
='
\0'
)
if((a[i]>
a'
&
z'
)||(a[i]>
A'
))letter++;
//统计字母个数
elseif(a[i]>
0'
&
a[i]<
9'
)number++;
//统计数字个数
elseothers++;
//统计其他数个数
i++;
letter="
letter<
number="
others="
others<
/*4、给出年、月、日,计算该日是该年的第几天。
intlead(int);
intly,year,month,date,i,sum=0;
inputyear、month、date:
year>
month>
date;
inta[12]={31,0,31,30,31,30,31,31,30,31,30,31};
ly=lead(year);
if(ly==1)
a[1]=29;
//366天
elsea[1]=28;
//365天
month-1;
i++)//当前月之前所有月天数累加和
sum+=a[i];
sum+=date;
//加上当前月天数
你输入的日期是当年的第"
天"
intlead(inty)//判断闰年
if((y%4==0&
y%100!
=0)||(y%400==0))return1;
//是闰年
elsereturn0;
//不是闰年
/*5、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。
intcdivisor(int,int);
intcmultiple(int,int,int);
intx,y,d,m;
inputtwonumber:
x>
y;
d=cdivisor(x,y);
m=cmultiple(x,y,d);
commondivisoris"
d<
endl<
commonmultipleis"
m<
intcdivisor(intx1,inty1)//最大公约数
intr,temp;
if(x1<
y1)
temp=x1;
x1=y1;
y1=temp;
while(x1%y1)//当较大数除以较小数余数等于0时,较小数为最大公约数
r=x1%y1;
y1=r;
returny1;
intcmultiple(intx2,inty2,intd1)//最小公倍数
returnx2*y2/d1;
//两数相乘结果除以它们的最大公约数为最小公倍数
/*6、写一函数,将两个字符串连接。
string.h>
chara[size],b[size];
inputtwostring:
cin.getline(b,size);
strcat(a,b);
a="
a<
/*7、写一函数,将一个字符串的元音字母复制到另一个字符串,然后输出。
voidscpy(char*,char*);
chara[size]="
Helloworld"
charb[size]="
Net"
a="
b="
b<
scpy(a,b);
voidscpy(char*p,char*q)
while(*q!
if(*q=='
||*q=='
e'
E'
i'
|
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 基础 例题 100
![提示](https://static.bdocx.com/images/bang_tan.gif)