急 c++的题 456三道!

作者&投稿:郯非 (若有异议请与网页底部的电邮联系)
三道C++题目,急!!!~

// 第一题#include #include using namespace std;int n, num[100000], dp[100000], dpmax[100000];int main() {int maxindex;ios::sync_with_stdio(false);cin >> n;for (int i = 1; i > num[i];}sort(num, num + n);dp[0] = 0;dpmax[0] = 0;for (int i = 1; i i - 1) {maxindex = i - 1;}dp[i] = dpmax[maxindex] + 1;dpmax[i] = max(dpmax[i - 1], dp[i]);}cout << dp[n] << endl;}

这些都是算法题吧,代码不难,难的是算法。
对每个序列考虑其代价序列,即以下操作的代价:reduce(1),reduce(2),...,reduce(n-1)。容易验证每次操作得到的新序列与原序列相比,其代价序列的变化就是删掉一个,并调整与其相邻的一个值(例如执行reduce(2),则相应的代价序列的变化是删掉reduce(2)的代价值,然后若a[3]比a[2]大,则删掉的是a[2],因此须调整reduce(3)的代价值为reduce a[2]和a[4]的代价,否则须调整reduce(1)的代价值为reduce a[1]和a[3]的代价),而且这种调整不会使值变小。综上,代价和至少是原序列中reduce(1),reduce(2),...,reduce(n-1)的代价和,而且容易验证每次都进行代价最小的操作可以达到这个下界。因此程序要做的仅仅是计算reduce(1),reduce(2),...,reduce(n-1)的代价并输出它们的和。
记f(n)为分解的方案总数。若n为奇数,分解必含至少1个1,把这个1去掉即n-1的分解,因此f(n)=f(n-1)。若n为偶数,把所有分解分为两类:含1的分解,同奇数的分析,有f(n-1)=f(n-2)种;不含1的分解,把分解中所有因子都除以2即得n/2的一个分解,且是一一对应,因此此类分解有f(n/2)种。综上n为偶数时f(n)=f(n-2)+f(n/2)。程序就利用以上递推式递归计算即可。
这是带权值的区间调度问题,用动态规划算法。详情可以参见http://download.csdn.net/detail/qiantangbanmu/6506139
第194页。

5,6太简单了。自己想。

这玩意自己不练没长进啊。

4、

#include<iostream>
#include<ctime>                   //C++提供的时间头文件  
using namespace std;
class TimeShower{
int hor;
int min;
int sec;
public:

TimeShower(){
time_t t=time(0);               //设置时间long int型
              tm* gt=localtime(&t);
              hor=gt->tm_hour;
              min=gt->tm_min;
              sec=gt->tm_sec;
}

void getTime(){
time_t t=time(0);               //设置时间 
              tm* gt=localtime(&t);
              hor=gt->tm_hour;
              min=gt->tm_min;
              sec=gt->tm_sec;
}
void printTime(){                    //输出时间 
    cout<<"---------------极简数字时钟--------------"<<endl;
cout<<"--------------『"<<hor<<':'<<min<<':'<<sec<<"』----------------";
}

};
int main(){
TimeShower t1();
t1.pringTime();
return 0;
}

帮人帮到底吧。。。。。。。。。

5、

#include<iostream>
using namespace std;
class complex{
int real;
int image;
public:
complex(int a=0,int b=0){
real=a;
image=b;
}

void print(){
cout<<'('<<real<<','<<image<<')'<<endl; 
}

   friend complex operator+(complex &a,complex &b);    //友元函数运算符重载
   
   complex operator-(complex &a){                  //成员函数运算符重载
    complex c(real-a.real,image-a.image);
    return c;
   }
};
complex operator+(complex &a,complex &b){
complex c(a.real+b.real,a.image+b.image);
return c;
}
int main(){
complex c1(1,2),c2(1,-1);
complex c3=c1+c2;
complex c4=c1-c2;
c3.print();
c4.print();
}


6、和5的思路一致。就是多了个化简。

化简容易。构造函数里面找入参的最大公约数。然后同是分子,分母同时除以最大公约数就行了。

别告诉我最大公约数也不会求。/汗。

或者定义一个化简成员函数。每次加减乘除完了,调用一下。就的到化简的对象了。




麦积区13190678489: 三道C++函数题 急求答案 -
秘吉彼优: #include <stdio.h> #include<iostream.h>#define BASE 10#define DIGIT 100int main(void) { int i,j,input; int x[DIGIT]; printf("Please enter");scanf("%d",&input);while(input) { for (i=0;input!=0;input/=BASE) x[i++]=input%BASE; cout<<"该数是"...

麦积区13190678489: 求助:3道C++的编程题~!~!~!~!~!!!!! -
秘吉彼优: //第一题#include void main() { int a,b,c; for(a=1;a for(b=0;b for(c=1;c if(100*(a+c)+10*(b+b)+a+c==1333) cout}//第二题#include #include int sushu(int n) { for(int i=2;i if(n%i==0) return 0; return 1; } void main() { int n; cout cin>>n; if(n%2!=0||n cout else ...

麦积区13190678489: 三道C++题目,急!!! -
秘吉彼优: // 第一题#include <iostream>#include <algorithm>using namespace std;int n, num[100000], dp[100000], dpmax[100000];int main() { int maxindex; ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; } sort(num, num + n); dp[...

麦积区13190678489: 几道简单的C++题目,急求答案 -
秘吉彼优: 正确率90%吧 BCBCB CDDCB11. (grade>80)&&(grade12. 1,513. 注释部分、编译预处理部分、程序正文部分14. 子函数声明15. 016. 顺序结构、分支结构、循环结构17. gets、getline18. 指针变量19. switch

麦积区13190678489: c++编程3道题 急求解!! -
秘吉彼优: #include<iostream>using namespace std;void main(){ char s[100000]; cin>>s; for(int i=6;i>0;i--) cout<<s[i];}

麦积区13190678489: C++编程题(急急急!!!) -
秘吉彼优: 自己写的,试过可行,满足你的题目要求,请采纳!谢谢#include <cstdlib>#include <iostream>#include <string> using namespace std; class Cshape { public:Cshape(float a,float b) { high=a; width=b; } ~Cshape(){} protected:float high;float width; }; ...

麦积区13190678489: C++试题 急求答案
秘吉彼优: #include <iostream> using namespace std; int main() { char s1[]="student and teacher"; int geshu=0; cout<<s1<<endl; for(int i=0; s1[i]!='\0'; i++) { if((s1[i]=='t')|| (s1[i]=='n')) { int y=i; geshu++; for(y; s1[y]!='\0'; y++) { s1[y]=s1[y+1]; } i--; } } cout<<s1<<"\t"<<geshu<<endl; return 0; }

麦积区13190678489: 求3道C++的题目. 1、定义两个同种的单向链表(结点中包含一个整型数和一个指向本结点类型的指针) -
秘吉彼优: 第一题没什么意思.第二题不知所云,只有后指针算什么双向链表…… 第三题如果是洗牌后发牌还有点意思.可以设置洗牌次数.人工洗牌的话一般是把牌分成两堆然后依次插入.完美的情况就是分成数量相同的两堆然后一张一张依次插入合成一堆.搞一个函数完成这个操作,洗几次牌就调用几次.然后再写一个函数完成分发操作,依次将牌从链表上取下生成四个新链表分给四个人

麦积区13190678489: C++编程题求助!急! -
秘吉彼优: 都调试通过了 第一题:#include "iostream" using namespace std; template T getmin(T&x,T&y) { return x} int _tmain(int argc, _TCHAR* argv[]) { float x,y; cout cin>>x>>y; cout return 0; } 第二题:#include "iostream" using namespace std; ...

麦积区13190678489: C++编程题(急急急) -
秘吉彼优: #include <iostream>#include <cmath> using namespace std; class Point //Point类声明 { public: Point(int xx=0, int yy=0) {X=xx;Y=yy;} Point(Point &p); int GetX() {return X;} int GetY() {return Y;} private: int X,Y; }; Point::Point(Point &p) //拷贝构造函数...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网