题目1:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
#include"stdio.h"
#include"string.h"
void main()
{
char s[100];
int a=0, b=0, c=0, d=0, i, n;
printf("please input a string:");
gets(s);
n = strlen(s);
for(i = 0; i < n; i++){
if((s[i] >= 'a'&&s[i] <= 'z')||(s[i] >= 'A'&&s[i] <= 'Z'))
a++;
else if(s[i] == ' ')
b++;
else if(s[i] >= '0' && s[i] <= '9')
c++;
else
d++;
}
printf("English letter:%d\nSpace:%d\nNumble:%d\nOther:%d\n", a, b, c, d);
}
题目2:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
#include"stdio.h"
#include"math.h"
void main()
{
int a, n, s, i, j, temp;
s = 0;
printf("please input a numble:");
scanf("%d", &a);
printf("please input the numble of numble:");
scanf("%d",&n);
for(i = 0; i < n; i++){
for(j = 0; j <= i; j++){
temp = a*pow(10,j);
s = s+temp;
}
}
printf("%d",s);
getchar();
}
题目3:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3。编程找出1000以内的所有完数。
#include"stdio.h"
int IsPerNum(int x);
void main()
{
int i;
for(i = 1; i <=1000; i++){
if(IsPerNum(i))
printf("%d\n",i);
}
}
int IsPerNum(int x)
{
int i, n, s=0;
for(i = 1; i < x; i++){
if(x%i == 0){
s = s+i;
}
}
if(x == s)
return 1;
else
return 0;
}
题目4:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
个人总结:找出规律用递归
#include"stdio.h"
#include"math.h"
float Height(int i);
void main()
{
float i, s, t,h=100.;
t = 0.;
s = 0.;
for(i = 1; i <= 10; i++){
t = Height(i);
s = s+2*t;
}
s = s+h;
printf("the sum of the distance: %f\n", s);
i = Height(10);
printf("the height of 10th: %f\n", i);
}
float Height(int i)
{
int h = 100;
return h*pow(1./2, i);
}
题目5: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
个人总结:找出规律用递归
#include"stdio.h"
int fun(int n);
void main()
{
int s=0, n=9;
s = fun(n);
printf("the total is %d\n", s);
}
int fun(int n)
{
if(n <= 0)
return 1;
else
return (fun(n-1)+1)*2;
}