求sn=a+aa+aaa+aaaa+......的值
#include<stdio.h>
int main(void)
{
int i, a, n, sum, t;
scanf("%d %d", &a, &n);//取地址
t = a;
sum = a;
for (i = 0; i < (n-1); i++)
{
a = a * 10 + t;
sum = sum + a;
}
printf("%d", sum);
}
1.研究规律
2.i<(n-1)——注意i的终值
3.t,sum——灵活使用变量
小球自由落体运动
#include<stdio.h>
#include<math.h>
int main(void)
{
/*********Begin*********/
int N,i;
float M,h,x;
scanf("%f %d",&M,&N);
h=M/2;
x=M;
for(i=2; i<=N; i++)
{
x=x+h*2;
h=h/2;
}
printf("%.2f %.2f",h,x);
/*********End**********/
return 0;
}
1.取地址
最大公约数和最小公倍数
#include<stdio.h>
int main(void)
{
int m, n, a, b, max, min;
scanf("%d %d", &m, &n);
a = m % n;
b = m * n;
while (a != 0)
{
m = n;
n = a;
a = m % n;
}
max = n;
min = b / max;
printf("%d %d", max, min);
}
字符串中各类字符数的统计
#include<stdio.h>
int main(void)
{
char s[81];
int i=0,q=0,w=0,e=0,r=0;
gets(s);
while(s[i]!='\0'){
if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')
q++;
else if (s[i] >= '0' && s[i] <= '9')
w++;
else if (s[i] == ' ')
e++;
else
r++;
i++;
}
printf("%d %d %d %d", q, w, e, r);
}
1.gets(str)
2.while('\0')
3.i++