(7)題目:打印出所有的“水仙花數”,所謂“水仙花數”是指一個三位數,其各位數字立方和等於該數
本身。例如:153是一個“水仙花數”,因爲153=1的三次方+5的三次方+3的三次方。
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "math.h"
void main()
{
int a,b,c,x;
for(x=100;x<=999;x++)
{
a=x/100;
b=x%100/10;
c=x%10;
if(pow(a,3)+pow(b,3)+pow(c,3)==x)
printf("%d\n",x);
}
}
(8)題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。
/* Note:Your choice is C IDE */
#include "stdio.h"
main()
{char a;
int letters=0,space=0,number=0,others=0;
printf("請輸入一行字符\n");
while((a=getchar())!='\n')
{
if(a>='a'&&a<='z'||a>='A'&&a<='Z')
letters++;
else if(a==' ')
space++;
else if(a>='0'&&a<='9')
number++;
else
others++;
}
printf("總計:字符=%d 空格=%d 數字=%d 其他=%d\n",letters,space,number,others);
}
(9)題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時
共有5個數相加),幾個數相加有鍵盤控制。
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "math.h"
void main()
{
int t,a,b,i,sum=0;
scanf("%d %d",&a,&b);
t=a;
for(i=1;i<=b;i++)
{
sum+=a;
a=a+t*pow(10,i);
}
printf("a+aa+...=%ld\n",sum);
}