1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
//head.file
#ifndef BIGN_H_
#define BIGN_H_
#include<string.h>
#include<iostream>
#include<string>
#include<sstream>
std::string int2str(int);
struct Nrep{
char si;
std::string name;
int sz;
Nrep(int n){ ... }
~Nrep(){}
private:
Nrep(const Nrep&);
Nrep& operator=(const Nrep&);
};
class bign
{
Nrep* num;
int* ref;
public:
bign():num(new Nrep(0)),ref(new int(1)){}
bign(int i):num(new Nrep(i)),ref(new int(1)){}
bign(const bign& b):num(b.num),ref(b.ref){++*b.ref;}
virtual ~bign();
int size() const{ return num->sz;}
char operator[](int i) const {if((i<=num->sz)&&(i>0)) return num->name[i-1];else return 0;}
char getSi() const{ return num->si;}
bign& operator=(const bign&);
bign& operator+=(const bign&);
bign& operator-=(const bign&);
bign& operator*=(const bign&);
bool operator==(const bign&) const;
bool operator>(const bign&)const;
bool operator<(const bign&) const;
friend std::ostream& operator<<(std::ostream& oo,const bign& b){if (b.getSi()=='-')return oo<<b.getSi()<<b.num->name;else return oo<<b.num->name;}
};
bool big_than(bign& ,bign&);
bool less(bign& ,bign&);
bign& operator* (bign&, const bign&);
#endif /*BIGN_H_*/
//bign.cpp
......
//main cpp
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include"bign.h"
int main(int argc,char* argv[]){
std::string from;
if(argc!=2)
from="a1.in";
else
from=argv[1];
std::ifstream ii(from.c_str());
if(ii==0){
if(argc <=1)
std::cout<<"Usage: "<<argv[0]<<" [filename] \n\tPlease try again!\n";
else
std::cout<<"Can't open the file: "<<from<<std::endl;
return -1;
}
std::string to;
if(argc>=2)
to=argv[2];
else
to=from;
int n=to.find_last_of('.');
if (n<1)
to =to + ".out";
else
to.replace(n,from.length(),".out");
std::ofstream oo(to.c_str());
ii>>n;
for(int loop=0;loop<n;loop++){
int re1=0;
int num=0;
std::vector<bign> v1,v2;
ii>>num;
for(int i=0;i<num;i++){
ii>>re1;
v1.push_back(re1);
}
for(int i=0;i<num;i++){
ii>>re1;
v2.push_back(re1);
}
std::sort(v1.begin(),v1.end());
std::sort(v2.begin(),v2.end());
bign result;
for(int i=0;i<num;i++)
result += v1[i] * v2[num-i-1];
std::cout<<"Case #"<<loop+1<<": "<<result<<std::endl;
oo<<"Case #"<<loop+1<<": "<<result<<std::endl;
}
return 0;
}
|