[c语言]——水仙花数

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值