C语言 Example(三)

1、求出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。

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int a, b, c;
	int i, sum;
	for (i = 100;i < 999;i++)
	{
		a = i / 100;
		b = (i % 100) / 10;
		c = i % 10;
		sum = a * a*a + b * b*b + c * c*c;
		if (i == sum)
		{
			printf("%d ", i);
		}
	}
	printf("\n");
	system("pause");
	return 0;
}

 2、求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字, 例如:2+22+222+2222+22222 

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	int num = 0;
	int sum = 0;
	printf("input a number:\n");
	scanf("%d", &num);
	int Sn = num;
	for (i = 0;i < 5;i++)
	{
		printf("%d+", Sn);
		sum += Sn;
		Sn = Sn * 10 + num;
	}
	printf("\b=%d\n", sum);
	system("pause");
	return 0;
}

3、编写一个程序,可以一直接收键盘字符, 如果是小写字符就输出对应的大写字符, 如果接收的是大写字符,就输出对应的小写字符, 如果是数字不输出。 

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int ch = 0;
	printf("请输入字符:>\n");
	while ((ch = getchar()) != EOF)
	{
		if (ch >= 'a'&&ch <= 'z')
			printf("%c", ch - 32);
		else if (ch >= 'A'&&ch <= 'Z')
			printf("%c", ch + 32);
		else if (ch >= '0'&&ch <= '9')
			;
		else
			;
	}
	system("pause");
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值