刷题3/17

Nacissistic number

#include<stdio.h>
int main()
{
	int i,a,b,c;
	for(i=100;i<1000;i++)
	{
		a=i%10;
		b=i/10%10;
		c=i/100%10;
		if(i==(a*a*a+b*b*b+c*c*c))
			printf("%d ",i);
	}
	return 0;
} 

Nesting of conditional operators

#include<stdio.h>
int main()//Nesting of conditional operators
{
	int sc;
	char grd;
	printf("Please input your score:");
	scanf("%d",&sc);
	grd=(sc>=90)?'A':((sc>=60)?'B':'C');
	printf("%c\n",grd);
	return 0;
}

GCD和LCM(Greatest common divisor& Least common multiple)

#include<stdio.h>
int main()//GCD和LCM/LCD
{
	int a,b,c,d,e;
	printf("Please input two number:\n");
	scanf("%d %d",&a,&b);
	if(a<b)
	{
		c=b;
		b=a;
		a=c;
	}
	d=a%b;
	e=a*b;
	while(d!=0)
	{
		a=b;
		b=d;
		d=a%b;
	}
	printf("The greatest common divisor of these two numbers is:%d, and the least common multiple is:%d\n",b,e/b);//GCD与LCM的乘积为两数之积
	return 0;
}

Count the number of symbols

#include<stdio.h>
int main()//Count the number of symbols
{
	char c;
	int letters=0,spaces=0,digits=0,others=0;
	printf("Please input some letters:\n");
	while((c=getchar())!='\n')//Pay attention to the idea of loop wrapping and the difference between getchar() and scanf() functions
	{
		if((c>='a'&&c<='z')||(c>='A'&&c<='z'))
		{
			letters++;
		}
		else if(c>='0'&&c<='9')
		{
			digits++;
		}
		else if(c==' ')
		{
			spaces++;
		}
		else
		{
			others++;
		}
	}
	printf("Letters Count:%d,Digits Count:%d,Spaces Count:%d,Others Count:%d",letters,digits,spaces,others);
	return 0;
}

a&n

#include<stdio.h>
int main()
{
	int s=0,a,n,t;
	printf("Please input the value of a and n:\n");
	scanf("%d%d",&a,&n);
	t=a;
	while(n>0)//Consume the value of n and simultaneously shrink the value of a
	{
		s+=t;
		a=a*10;
		t+=a;
		n--;
	}
	printf("a+aa+aaa+...=%d\n",s);
	return 0;
}

Find factor

#include<stdio.h>
#define N 50000
int main()
{
	int i,j,a,b,s;
	int c[256];
	for(i=2;i<=N;i++)
	{
		s=c[0]=1;
		a=0;
		for(j=2;j<=(i/2);j++)//When you have a factor problem, you should conjecture 1/2.
		{
			if(i%j==0)
			{
				s+=j;
				c[a++]=j;
			}
		}
		if(i==s)//It's common that A conditional structure is nested within a loop structure.
		{
			printf("%d=%d",i,c[0]);
			for(b=1;b<=a;b++)
			{
				printf("+%d",c[b]);
			}
			printf("\n");
		}
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值