博文

c++符号重载多盒子中最小体积盒子(2005-07-29 22:44:00)

摘要:#include<iostream> #include<cstdlib> #include<ctime> using namespace std; class box { public:     box(double alength=1.0,double abreadth=1.0,double aheight=1.0);     ~box()     {         cout<<endl<<"constructor destroyed."<<endl;     }     double getvolume()const     {         return height*length*breadth;     }     double getlength()const     {         return length;     }     double getbreadth()const     {         return breadth;     }     double getheight()const     {    ......

阅读全文(4320) | 评论:0

一道c++竞赛题(2005-07-28 13:01:00)

摘要:已知n个整数 x1,x2,…..xn, 以及一个整数k (k<n)。从 n 个整数中任选k个整数组合相加,可分别得到一系列的和。例如当 n=4, k=3,4个整数分别为3,7,12,19 时,可得全部的组合为:        3+7+12=22   3+7+19=29        7+12+19=38 3+12+19=34。       现在,要求你计算出和为素数的组合数有多少种。例如上例,只有一种组合的和为素数:(3+7+19=29)。 输入: n, k(1≤n≤20,k<n)    x1,x2,…xn(1≤xi≤5000000) 输出: 一个整数(满足条件的种数) 输入输出样例: 输入: 4 3 7 12 19 输出:   1 源程序: #include<iostream> #include<math.h>   const int N=21;   using namespace std;   int issushu(long num)   {       for(long i=2;i<=sqrt(num);i++)       if(num%i==0)           return 0;       return 1;   } long totalsum(long a[],int n) {     long sum=0;     for(int i=0;i<n;i++) &n......

阅读全文(5879) | 评论:0

一个题目(c++)(2005-07-18 23:40:00)

摘要:在论坛上看到这样的题目: 任意一个正整数的立方都可以写成一串连续奇数和。例如:       13*13*13=157+159+。。。。。。+179+181+183 这道题的算法是:任意输入的正整数的平方为x,那么一串连续奇数的第一个数应为x减去这个正整数减一, 按等差为2的数列直到x加上这个正整数减一.例如:如果输入为12,即求12的立方, 则应为:144-(12-1)=133,135,137,139.........144+(12-1)=155. 程序: #include<iostream> #include<cmath> using namespace std; int main() {     int num;     cout<<"plese input the num:";     cin>>num;     int num2=pow(num,2);     int sum=num2-(num-1),sum2=num2+num-1;     cout<<endl<<"the result is:"<<endl;     cout<<num<<"*"<<num<<"*"<<num<<"="<<sum;     while(sum<=sum2)     {           sum+=2;      cout<<&......

阅读全文(3773) | 评论:0

n个数的最小公倍数(2005-07-18 23:15:00)

摘要: #include<stdio.h> #define N 3/*随意改变这个值实现n的变化*/ int yueshu[15]; int zuixiao(int b[],int count) { int i=0,small=32667; for(;i<count;i++) if(b[i]<b[0]) small=i; if(small==32667) return 0; else return small; } int xingma(int number,int i) { if(number%i==0) return 1; else return 0; } int zdgyshu(int c[],int x,int num) { int i=0,k=0,j,data,count=0,dataelse=0; for(i=2;i<=c[num];i++) if(xingma(c[num],i)) { yueshu[k]=i; k++; } for(i=0;i<k;i++) { data=c[i]; for(j=0;j<x;j++) { if(j==num) continue; else { if(xingma(c[j],data)) count++; } } if(count==x-1) dataelse=data; } if(dataelse!=0) return dataelse; else return 0; } main() { int array[N],i,public_mnum,public_mtime,smallest,sum=1; clrscr(); printf("please input %d numbers:",N); for(i=0;i<N;i++) scanf("%d",&array[i]); smallest=zuixiao(array,N); public_mnum=zdgyshu(array,N,smallest); if(public_mnum==0) { for(i=0;i<N;i++) sum*=array[i]; printf("\nthe public_mtime num is:%d",su......

阅读全文(4759) | 评论:0

C++(类的静态成员运用)(2005-07-15 12:07:00)

摘要:#include<iostream> using namespace std; class Box { public: Box(); Box(double lengthvalue,double breadthvalue,double heightvalue); double volume()const; int comparevolume(const Box& otherbox)const; int getobjectcount()const {return objectcount;} private: static int objectcount; double length; double breadth; double height; }; Box::Box() {     cout<<endl<<"Default constructor called"<<endl;     ++objectcount;     length=breadth=height=1; } Box::Box(double lengthvalue,double breadthvalue,double heightvalue):length(lengthvalue), breadth(breadthvalue),height(heightvalue) {     cout<<endl         <<"constructor called"         <<endl;     ++objectcount;     if(length<=0)         length=1......

阅读全文(4031) | 评论:0

c++(类大小的测定)(2005-07-14 16:32:00)

摘要: #include<iostream> using namespace std; class SizeBox { public:     SizeBox();     int totalSize(); private:     char* pMaterial;     double length;     double breadth;     double height; }; SizeBox::SizeBox():length(1.0),breadth(1.0),height(1.0),pMaterial("Cardboard") {cout<<endl<<"Default constrcutor called"<<endl; } int SizeBox::totalSize() {     return sizeof(length)+sizeof(breadth)+sizeof(height)+sizeof(pMaterial); } int main() { SizeBox box; SizeBox boxes[10]; cout<<endl<<"The data numbers of a box occupy"<<box.totalSize()<<"bytes."; cout<<endl<<"A singal box object occupies"<<sizeof SizeBox<<"bytes."; cout<<endl<<"An array f 10 Box objects  occupies"<<sizeof(boxes)<<&......

阅读全文(3780) | 评论:0

c++(面向对象关于类的盒子问题)(2005-07-13 23:04:00)

摘要:#include<iostream> using namespace std; class Box { public:     Box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);     double volume();     int comparevolume(Box& otherbox); private:     double length;     double breadth;     double height; }; Box::Box(double lengthvalue,double breadthvalue,double heightvalue):length(lengthvalue), breadth(breadthvalue),height(heightvalue) {     cout<<endl         <<"constructor called"         <<endl;    length=lengthvalue;    breadth=breadthvalue;    height=heightvalue; } double Box::volume() {     return length*height*breadth; } int Box::comparevolume(Box& otherbox) {     if(otherbox.v......

阅读全文(3494) | 评论:0

c++(最大公约数和最小公倍数)(2005-07-12 13:27:00)

摘要:#include<iostream> using namespace std; int main() {     int m=0,n=0;     cout<<endl         <<"请输入两个数(整型,从小到大):";     cin>>n>>m;     while(n>m)     {         cout<<endl             <<"输入格式错误,请重新输入.:";         cin>>n>>m;     }     int m1=m;     int n1=n;     int i=m%n;     while(i!=0)     {         m=n;         n=i;         i=m%n;     }     cout<<endl &nb......

阅读全文(6164) | 评论:0

c++(冒泡排序)(2005-07-12 12:35:00)

摘要:#include<iostream> #define swap(x,y,t)((t)=(x),(x)=(y),(y)=(t)) using namespace std; const int N=10; void sort(int b[],int count); int main() {     int a[N];     cout<<endl         <<"please input the "<<N<<"numbers:"<<endl;     for(int i=0;i<N;i++)         cin>>a[i];     cout<<endl         <<"the original numbers you have inputed are: "<<endl;     for(i=0;i<N;i++)     cout<<" "<<a[i];     cout<<endl         <<"After sorting the numbers ,the numbers are:"<<endl;     sort(a,N);     return 0; } void sort(int b[......

阅读全文(7660) | 评论:0

c++(模板和重载)(2005-07-10 15:28:00)

摘要:#include<iostream> #include<iomanip> using namespace std; template <class T> T larger(T a,T b); template<>long* larger<long*>(long* a,long* b); template<class T>T larger(const T array[],int count); template<class TReturn,class TArg>TReturn larger(TArg a,TArg b); int main() {     cout<<endl;     cout<<"the larger between 1.2 and 3.5 is:"<<larger(1.2,3.5)<<endl;     int value=25;     int value1=21;     cout<<"the larger between"<<value<<"and"<<value1<<"is:"<<larger(value,value1)<<endl;     long value3=35;     long value4=25;     cout<<"the larger between"<<value3<<"and"<<value4<<"is:"<<larger(value3,value4)<<endl; ......

阅读全文(3556) | 评论:0