#include<stdio.h>
#include<stdlib.h>
int count(int n);
/*count how many ways to climb up N steps stairs.*/
int main (int argc, char *argv[])
{
int n,ct;
printf("please input n:\n");
scanf("%d",&n);
ct=count(n);
printf("there are %d ways to climb up N steps stairs!\n",ct);
system("PAUSE");
return 0;
}
int count(int n)
{
if(1==n)
return 1;
else if(2==n)
return 2;
else return count(n-1)+count(n-2);
}
【程序输入输出】for example
please input n:
5
there are 8 ways to climb up N steps stairs!
int judgeDigit(int m);
/*This function return the digit of parameter m*/
void judgeEqual(int m,int n);
/*parameter m is a integer,parameter n is the digit of m,this function is used to judge m whether is a Armstrong integer and output it*/
int main (int argc, char **argv)
{
int i,len;
printf("All 2 digit to 5 digit Armstrong integers are following:\n");
for(i=10;i<=99999;i++)
{
len=judgeDigit(i);
judgeEqual(i,len);
}
printf("\n");
system("PAUSE");
return 0;
}
int judgeDigit(int m)
{/*This function return the digit of parameter m*/
int len=0;
do
{
++len;
m=m/10;
}while(m);
return len;
}
void judgeEqual(int m,int n)
{/*parameter m is a integer,parameter n is the digit of m,this function is used to judge m whether is a Armstrong integer and output it*/
int j,temp=m,sum=0;
for(j=1;j<=n;j++)
{
sum+=(int)(pow(temp%10,n));
temp=temp/10;
}
if(m==sum)/*if m is equal to sum,that is to say m is a Armstrong integer*/
printf("%d\t",m);
}
【程序输入输出】
no input;
output:
All 2 digit to 5 digit Armstrong integers are following:
153 370 371 407 1634 8208 9474 54748 92727 93084
注:用gcc调试就得不到153这个结果,但windows下用vc6.0就可以得到。不解中,这是编译器问题还是程序问题?
3、
【问题描述】将1,2,3,4,5,6,7,8,9共9个数分成三组,组成3个三位数,且使这3个三位数构成1:2:3的比例,例如:3个三位数192,384,576满足以上条件.192:384:576=1:2:3。试求出所有满足条件的3个三位数。
bool judge( int a, int b, int c )
{
char tmp_buf[ 10 ];
sprintf( tmp_buf, "%d%d%d", a, b, c );
int nTimeResult = 1;
int nSumResult = 0;
for ( int i = 0; i < 9; i++ )
{
nTimeResult *= ( tmp_buf[ i ] - '0' );
nSumResult += ( tmp_buf[ i ] - '0' );
}
解此题还须注意一点:数列的项必须定义为double型,因为延长到第50项如果定义为int or float型,数列的项会被截断即超过int和float的表示范围。
【代码】
cCODE:
#include<stdio.h>
#include<stdlib.h>
int main (int argc, char **argv)
{
double a1=1,a=0;
int i=1;
while(i<=50)
{
printf("%.0lf ",a1); //'.0’ is just for do not output the decimal place
i++;
if(i%2==1)
a=2*a1+1;
else
a=2*a1;
a1=a;
}
system("PAUSE");
return 0;
}
int main (int argc, char **argv)
{
int a[]={1,2,3,4,5,6,7,6};
printf("The max element is %d!\n",max(a,7));
/*caution:he length of a is 8,but the argument is 7*/
system("PAUSE");
return 0;
}
void splitN(int n,int m);//n是需要拆分的数,m是拆分的进度。
int x[1024]={0},total=0 ;//total用于计数拆分的方法数,x[]用于存储解
int main()
{
int n ;
printf("please input the natural number n:");
scanf("%d",&n);
splitN(n,1);
printf("There are %d ways to split natural number %d.\n",total,n);
system("PAUSE");
return 0 ;
}
【程序输入输出】
input:
please input the natural number n:5
output:
1 1+1+1+1+1
2 1+1+1+2
3 1+1+3
4 1+2+2
5 1+4
6 2+3
There are 6 ways to split natural number 5