博文
大数的阶乘算法(2007-12-14 23:49:00)
摘要: 读大学的时候很多语言课程都会布置这个做大数阶乘算法的作业,今天我就把他贴出来,以后大家都不用愁了。
#include "stdafx.h"#include "stdio.h"#include "iostream.h"
int main(int argc, char* argv[]){ int carry,n,j; int a[2000]; int digit=1; int temp,i; cout<<"please enter n:"<<endl; cin>>n; a[0]=1; for(i=2; i<=n; i++) { for(carry=0,j=1; j<=digit; ++j) { temp=a[j-1]*i+carry; a[j-1]=temp%10; carry=temp/10; } while(......
C基础练习源程序(部分)转(2007-12-10 21:07:00)
摘要:
C语言基础类的。这些都是在近两年中陆续写就的。在TC2.01或者DEV C++下面编译通过。用来给学生上课还是不错的。以@隔开每个程序:
@
/*一百钱买100只鸡。cock五元一只,then三元一只,chicken一元三只。求cock,*//*then,chicken的各数。*/#includemain(){ int x,y,z,r; for(x=0;x<=25;x++) for(y=0;y<=33;y++) for(z=0;z<=100;z++)
/*此题是穷举法的运用一例。当它们的个数满足下面的条件的时候则运算成功*//*每种鸡从0考察到100元的情况,同时小鸡数要能被3整除*/ if((x+y+z==100)&&(5*x+3*y+z/3==100)&&(z%3==0)) printf("cock=%d,then=%d,chicken=%d\n",x,y,z);}
@
/*用递归求谭本上的一个函数*/
float p(int n,float x){ if (n==0) return(1); else if (n==1) return(x); else return(((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n);}main(){ int a=3; float b=4; printf("%10.2f\n",p(a,b));}
@
/*求最大公约数的两种写法*/
#includemain(){ int i,m,n,y,b; scanf("%d%d",&m,&n); for(i=1;i<=(m>n?n:m);i++) if ((m%i==0)&&(n%i==0)) y=i;
/*上面这句将记录公约数,但每次记录......
用指针删除空格(2007-12-07 22:12:00)
摘要:我其实觉得指针挺变态一玩意,不过sts好象还把它当宝贝,呵呵
#include<iostream>using namespace std;void move(char *str){ char *p=str; do { if(*str!=' ') *p++=*str; }while(*++str); *p='\0';}int main(){ char str[]="abc ded"; move(str); cout<<str;
}......
模仿除法的过程(2007-12-07 22:08:00)
摘要:/*地址 http://yzfy.org/bbs/viewthread.php?tid=417&extra=page%3D1 题目描述:7是一个恶魔数字,如果一个数是7的倍数,或者它的数位上含有数字7,那么这个数也是恶魔数字输入:多组测试数据,每组只有一行,一行只有一个整数n(1<=n<=1e(1e5)),最后输入一个0来表示结束输出:判断这个数是不是恶魔数字,是的话输出yes,否则输出no样例输入:7171470940样例输出:yesyesyesyesno*//*例#include <cstdio>
using namespace std;
unsigned int data[70];char str[100002];
int main(){ for (unsigned int i = 0; i < 70; ++i)data[i] = i%7; //记录每个数和7的余数,为后面除法作准备 unsigned int tmp = 0; for (;gets(str) != NULL;) { if ((str[0] == '0')&&(str[1] == '\0')) return 0; else { tmp = 0; const char* pc = str; for (;......
