//编写一个算法,利用栈将一个非负的十进制整数N转换为一个二进制整数
#include<stdio.h>
#include<malloc.h>
struct ll
{
int data[1024];
int bottom;
};
int main()
{
void exchange(struct ll *s,int n);
int n;
struct ll s;
printf("please input numbers:");
scanf("%d",&n);
exchange(&s,n);
printf("\n");
return 0;
}
void exchange(struct ll *s, int n)
{
int i;
s->bottom = 0;
while(n!=0)
{
s->data[s->bottom]=n % 2;
n=n/2;
s->bottom++;
}
for(i=s->bottom;i>=0;i--)
printf("%d\t",s->data[i]);
}
//Fibonacci数列
#include<stdio.h>
int main()
{
int sum=2,n,p1=1,p2=2,p3,i;
printf("please input numbers:");
scanf("%d",&n);
printf("1+1");
for(i=0;i<n;i++)
{
p3=p1+p2;
p1=p2;
p2=p3;
sum=sum+p3;
printf("+%d",p3);
}
printf("=%d\n",sum);
return 0;
}
//打印楼梯
#include<stdio.h>
#include<stdlib.h>
void fun1(void);
int main(void)
{
fun1();
system("pause");
return 0;
}
void fun1(void)
{
int i, j;
printf("%c%c\n", 1, 2);
for (i=1; i<=8; i++)
{
for (j=1; j<=i; j++)
{
printf("%c%c", 219, 219);
}
printf("\n");
}
printf("\n");
}
//求PI
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, sum = 0;
double x, y;
srand(time(NULL));
for(i=1;i<500000;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
if((x*x+y*y)<1)
sum++;
}
printf("PI=%f\n",(double)4*sum/500000);
return 0;
}