C++短一点的程序设计
- 提问者网友:临风不自傲
- 2021-05-11 06:27
- 五星知识达人网友:枭雄戏美人
- 2021-05-11 07:10
using namespace std;
int main()
{
float r,s;
cin>>r;
s=3.14*r*r;
cout<<s;
return 0;
}这是求圆的面积的程序
- 1楼网友:轮獄道
- 2021-05-11 11:42
- 2楼网友:逐風
- 2021-05-11 10:36
这是判断一个数是不是质数的:
#include"iostream.h" void main() { int i,n,flag=1; cout<<"Enter a z_num:"; //提示输入一个整数 cin>>n; for(i=2;i<=n/2;i++) if(n%i==0) {flag=0; break;} if(flag==1) cout<<"Yes"; //是质数,就输出YES else cout<<"No"; //不是, 就输出NO cout<<endl; }
- 3楼网友:duile
- 2021-05-11 09:42
不知道你想干什么
!!!!!!
- 4楼网友:渊鱼
- 2021-05-11 08:20
随便给一个吗? 这是个算24点的程序
#include <iostream> #include <sys/timeb.h> #include <vector> #include <string> #include <stdio.h>
using std::vector; using std::string; using namespace std;
class Node { public: enum OpType { UNARY, PLUS, SUB, SUB2, MUL, DIV, DIV2 };
double& value(){ return value_; } double value() const { return value_; }
bool Add() { op_ = PLUS; value_ = left_->value_ + right_->value_; return true; } bool Sub() { op_ = SUB; value_ = left_->value_ - right_->value_; return true; } bool Sub2() { op_ = SUB2; value_ = right_->value_ - left_->value_; return true; } bool Mul() { op_ = MUL; value_ = left_->value_ * right_->value_; return true; } bool Div() { if(right_->value_==0)return false; op_ = DIV; value_ = left_->value_ / right_->value_; return true; } bool Div2() { if(left_->value_==0)return false; op_ = DIV2; value_ = right_->value_ / left_->value_;return true; }
Node( double d = 0 ) : left_(0), right_(0), op_(UNARY), value_(d) { } Node( Node* l, Node *r ) : left_(l), right_(r), op_(UNARY), value_(0) { }
string getExpr() { if( op_ == UNARY ) { char buf[32]; return ""; }
string str; switch( op_ ) { case PLUS: case SUB: case MUL: case DIV: str += "("; str += left_->getExpr(); str += " +--*//"[op_]; str += right_->getExpr(); str += ")"; break; case SUB2: case DIV2: default: str += "("; str += right_->getExpr(); str += " +--*//"[op_]; str += left_->getExpr(); str += ")"; } return str; }
double value_; OpType op_; Node *left_; Node *right_; };
template<unsigned int L> bool query( double target, Node** cand, string& expr ) { for( unsigned int k=0; k<L-1; ++k ) { Node* newcand[L-1]; for( unsigned int j=k+1; j<L; ++j ) { for( unsigned int i=k+1; i<j; ++i ) { if( cand[i]->value() == cand[j]->value() ) continue; }
Node node( cand[k], cand[j] ); Node **pNode = newcand; for( unsigned int i=0; i<L; ++i ) { if( i!=k && i!=j ) *pNode++ = cand[i]; }
newcand[L-2] = &node;
if( node.Add() && query<L-1>( target, newcand, expr ) ) return true; if( node.Sub() && query<L-1>( target, newcand, expr ) ) return true; if( node.Sub2() && query<L-1>( target, newcand, expr ) ) return true; if( node.Mul() && query<L-1>( target, newcand, expr ) ) return true; if( node.Div() && query<L-1>( target, newcand, expr ) ) return true; if( node.Div2() && query<L-1>( target, newcand, expr ) ) return true; } } return false; }
template<> bool query<1>( double target, Node** cand, string& expr ) { double diff = target - cand[0]->value(); if( diff < 0.000001 && diff > -0.000001 ) { //expr = cand[0]->getExpr(); return true; } return false; }
int main( int argc, char** argv) { const int LEN = 4; if( argc != LEN+1 ) { exit( -1 ); }
double target = 24;
Node nodes[LEN]; Node *cands[LEN]; for( int k=0; k<LEN; ++k ) { int i = atoi( argv[k+1] ); if( i == 0 ) { printf( "I don't like ZERO. Sorry!\n" ); exit( -1 ); } nodes[k].value() = i; cands[k] = &nodes[k]; }
string expr; if( query<LEN>( target, cands, expr ) ) printf( "%s\n", expr.c_str() ); else printf( "fail!\n" );
return 0; }