1.求出0~999之間的所有“水仙花數”並輸出。“水仙花數”是指一個三位數,其各位數字的立方和確好等於該數本身,如;153=1+5+3?,則153是一個“水仙花數”。
阿新 • • 發佈:2018-12-18
1.在螢幕上輸出以下圖案:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
2.求出0~999之間的所有“水仙花數”並輸出。“水仙花數”是指一個三位數,其各位數字的立方和確好等於該數本身,如;153=1+5+3?,則153是一個“水仙花數”。
/* 在數論中,水仙花數(Narcissistic number)也稱為自戀數、自冪數、阿姆斯壯數或阿姆斯特朗數(Armstrong number),是指一N位數,其各個數之N次方和等於該數。 例如153、370、371及407就是三位數的水仙花數,其各個數之立方和等於該數: 153 = 1^3 + 5^3 + 3^3。 370 = 3^3 + 7^3 + 0^3。 371 = 3^3 + 7^3 + 1^3。 407 = 4^3 + 0^3 + 7^3。 */ 3. 求Sn=a+aa+aaa+aaaa+aaaaa的前5項之和,其中a是一個數字,例如:2+22+222+2222+22222
4.編寫一個程式,它從標準輸入讀取C原始碼,並驗證所有的花括號都正確的成對出現。 1.
#include<stdio.h> #define N 10 int main() { int i=0; int j=0; for(i=0; i<N; i++) { for(j=0; j<N-i-1; j++) printf(" "); for(j=0; j<2*i+1; j++) printf("*"); printf("\n"); } for(i=0; i<N-1; i++) { for(j=0; j<=i; j++) printf(" "); for(j=0; j<2*(N-i)-3; j++) printf("*"); printf("\n"); } return 0; }
2.
#include<stdio.h> #include<math.h> int main() { int i=0; for(i=0; i<1000; i++) { int count=1; int sum=0; int temp=0; temp=i; while(temp/10)**//判斷這個數有幾位** { count++; temp/=10; } temp=i; while(temp) { sum += pow((temp % 10), count);**//pow(3,3)==27** temp /= 10; } if(i==sum) printf("%d ",i); } return 0; }
3.
#include<stdio.h>
#include<math.h>
int main()
{
int i=0;
int n=0;
int tmp=0;
int sum=0;
int a=0;
scanf("%d%d",&a,&n);
for(i=0; i<n; i++)
{
tmp=tmp*10+a;
sum=sum+tmp;
}
printf("%d",sum);
return 0;
}
#include<stdio.h>
int main()
{
int count = 0;
char ch = 0;
while((ch = getchar())!=EOF)
{
if(ch == '{')
count++;
else if(ch == '}')
{
if(count == 0)
{
printf("不匹配");
}
else
{
count--;
}
}
}
if(count == 0)
{
printf("匹配!");
} else
{
printf("不匹配!");
}
return 0;
}