#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;
}