1.位数:
输入一个不超过10的9次的正整数,输出它的位数。例如12735的位数是5。不使用任何数学函数。
C语言版(输入输出函数):
#include <stdio.h>
int main(void)
{
int a,c,i=0,num;
char arr[9];
while(scanf("%d",&a)==1)
{
for(i = 0;a > 0;i++)
{
arr[i] = a% 10 + '0';
a =a/10;
}
printf("%d\n",i);
}
}
fopen版
#include <stdio.h>
int main(void)
{
FILE *fin,*fout;
fin=fopen("digit.in","rb");
fout=fopen("digit.out","wb");
int a,c,i=0,num;
char arr[9];
while(fscanf(fin,"%d",&a)==1)
{
for(i = 0;a > 0;i++)
{
arr[i] = a% 10 + '0';
a =a/10;
}
fprintf(fout,"%d\n",i);
}
fclose(fin);
fclose(fout);
}
freopen版
#include <stdio.h>
int main(void)
{
int a,c,i=0,num;
char arr[9];
freopen("digit.in","r",stdin);
freopen("digit.out","w",stdout);
while(fscanf(stdin,"%d",&a)==1)
{
for(i = 0;a > 0;i++)
{
arr[i] = a% 10 + '0';
a =a/10;
}
fprintf(stdout,"%d\n",i);
}
fclose(stdin);
fclose(stdout);
}
2.水仙花数
输出100~999的所有水仙花数。若3位数ABC满足ABC=A的3次+B的3次+C的3次,即为水仙花数。
c语言版(输入输出函数):
#include <stdio.h>
int main(void)
{
int a,x,y,z;
for(a=100;a<999;a++)
{
x=a/100;
y=a/10%10;
z=a%10;
//printf("%d%d%d",x,y,z);
if(a==x*x*x+y*y*y+z*z*z)
printf("%4d",a);
}
}
fopen版:
#include <stdio.h>
int main(void)
{
int a,x,y,z;
FILE *out;
out=fopen("daffodil.out","w");
for(a=100;a<999;a++)
{
x=a/100;
y=a/10%10;
z=a%10;
//printf("%d%d%d",x,y,z);
if(a==x*x*x+y*y*y+z*z*z)
fprintf(out,"%4d",a);
}
return 0;
}
freopen版:
#include <stdio.h>
int main(void)
{
int a,x,y,z;
freopen("daffodil.out","w",stdout);
for(a=100;a<999;a++)
{
x=a/100;
y=a/10%10;
z=a%10;
//printf("%d%d%d",x,y,z);
if(a==x*x*x+y*y*y+z*z*z)
printf("%4d",a);
}
return 0;
}