# HW 12

—— 比尔盖茨

## main函数：

int main()
{
Cup c1;
int i, j;
cin>>i>>j;
Cup c2(i), c3(c2);
c3.setVolume(j);
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
class Cup
{
public:
int vol;
Cup(int _v = 0):vol(_v)
{
printf("A cup of %d ml is created.\n",vol);
}
Cup(const Cup &b):vol(b.vol)
{
printf("A cup of %d ml is copied.\n",vol);
}
~Cup()
{
printf("A cup of %d ml is erased.\n",vol);
}
void setVolume(int _v){vol=_v;}
};

## main函数：

int main()
{
int m, n, i, j, id;
string str;
Friends friends;
cin>>m;
for (i = 0; i < m; i++)
{
cin>>str>>n;
Group group(str);
for (j = 0; j < n; j++)
{
cin>>id>>str;
QQ qq(id, str);
}
}

cin>>m;
for (i = 0; i < m; i++)
{
cin>>str;
friends.findGroup(str);
}

cin>>n;
for (i = 0; i < n; i++)
{
cin>>j;
friends.findQq(j);
}
return 0;
}

## AC代码：

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
class QQ
{
public:
int val;
string name;
QQ(int _val,string _name):val(_val),name(_name){}
bool operator <(const QQ&b)
{
return val<b.val;
}
};
class Group
{
public:
string n;
vector<QQ> QQlist;
Group(string _n):n(_n){}
{
QQlist.push_back(b);
}
};
class Friends
{
public:
vector<Group> Glist;
{
sort(b.QQlist.begin(),b.QQlist.end());
Glist.push_back(b);
}
void findGroup(string name)
{
for(int i = 0;i<Glist.size();i++)
{
if(Glist[i].n ==name)
{
if(Glist[i].QQlist.size()==0)
{
cout << "Group " << name << " : empty." << endl;
}
else
{
cout << "Group " << name << " :";
int len = Glist[i].QQlist.size();
for(int j= 0;j<len-1;j++)
{
cout << " " << Glist[i].QQlist[j].val << " " << Glist[i].QQlist[j].name << ";";
}
cout << " " << Glist[i].QQlist[len-1].val << " " << Glist[i].QQlist[len-1].name << "." << endl;
}
return ;
}
}
cout << "Group " << name << " : not existing." << endl;
return ;
}
void findQq(int v)
{
//sort(Glist.begin(),Glist.end());
//bool flag = false;
int len = Glist.size();
vector <string> f;
cout << "QQ " << v << " in :";
for(int i = 0;i<len;i++)
{
int len1 = Glist[i].QQlist.size();
for(int j = 0;j<len1;j++)
{
if(Glist[i].QQlist[j].val==v)
{
f.push_back(Glist[i].n);
}
}
}
sort(f.begin(),f.end());
if(f.size()==0)
cout << " empty." << endl;
else
{
for(int i = 0;i<f.size()-1;i++)
cout << " " << f[i] << ";";
cout << " " << f[f.size()-1] << "." << endl;
}
return ;
}
};

## main函数：

int main()
{
int a, b, c, d;
string name;
cin>>name>>a>>b>>c>>d;
Role one(name, a, b, c, d);
cin>>name>>a>>b>>c>>d;
Role two(name, a, b, c, d);
one.combat(two);
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
class Role
{
public:
int hp,ce,de,fa;
string name;
Role(string _name,int _hp,int _ce,int _de,int _fa):hp(_hp),ce(_ce),de(_de),fa(_fa),name(_name) {}
void combat(Role &b)
{
bool flag = false;
if(fa>b.fa)
flag = true;
while(hp>0&&b.hp>0)
{
if(flag)
{
if(ce<=b.de)
{
b.hp--;
}
else
b.hp-=(ce-b.de);
b.de--;
if(b.hp<0)
b.hp=0;
if(b.de<0)
b.de = 0;
cout << name << " attacks " << b.name << ":" << b.name;
printf(" hp=%d,de=%d\n",b.hp,b.de);
}
else
{
if(b.ce<=de)
{
hp--;
}
else
hp-=(b.ce-de);
de--;
if(hp<0)
hp=0;
if(de<0)
de = 0;
cout << b.name << " attacks " << name << ":" << name;
printf(" hp=%d,de=%d\n",hp,de);
}
flag = !flag;
}
if(hp==0)
cout << b.name << " wins." << endl;
else
cout << name << " wins." << endl;
}
};

## main函数：

int main()
{
int a, b, c;
cin>>a>>b>>c;
Time t1(a, b, c);
cin>>a>>b>>c;
Time t2(a, b, c);
cout<<"Deference is "<<(t2 - t1)<<" seconds."<<endl;
return 0;
}

考点：运算符重载，注意要取秒之差的绝对值

## AC代码：

#include <bits/stdc++.h>
using namespace std;
class Time
{
public:
int h, m, s;
Time(int _h,int _m,int _s):h(_h),m(_m),s(_s){};
int operator -(const Time& b)
{
int d  = h*3600+m*60+s;
int d1 = b.h*3600+b.m*60+b.s;
return abs(d-d1);
}
};

## Main函数：

int main()
{
Animal *animal;
char ch;
while(cin>>ch)
{
switch(ch)
{
case 'c':
animal = new Cat();
break;
case 'd':
animal = new Dog();
break;
case 'r':
animal = new Rooster();
break;
}
animal->eat();
animal->fun();
delete animal;
}
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
class Animal
{
public:
Animal(){};
virtual void eat()=0;
virtual void fun()=0;
virtual ~Animal(){};
};
class Cat:public Animal
{
public:
Cat():Animal(){}
void eat()
{
printf("Cat eats fishes.\n");
}
void fun()
{
printf("Cat catches mouses.\n");
}
~Cat(){};
};
class Dog:public Animal
{
public:
Dog():Animal(){};
void eat()
{
printf("Dog eats bones.\n");
}
void fun()
{
printf("Dog can be housekeeping.\n");
}
~Dog(){};
};
class Rooster:public Animal
{
public:
Rooster():Animal(){};
void eat()
{
printf("Rooster eats corns.\n");
}
void fun()
{
printf("Rooster crows.\n");
}
~Rooster(){}
};

## main函数：

int main()
{
int a, b;
double c, d;
cin>>a>>b;
Complex<int> c1(a, b);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<c1.getModulus()<<endl;
cin>>c>>d;
Complex<double> c2(c, d);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<c2.getModulus()<<endl;
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
template<class T>
class Complex
{
public:
T r,i;
Complex(T _r,T _i):r(_r),i(_i){}
double getModulus()
{
return sqrt(r*r+i*i);
}
};

## main函数：

int main()
{
int a, b;
cin>>a>>b;
Compute compute(a, b);
compute.showResult();
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
bool isPrime(LL x)
{
if(x<=1)
return false;
if(x==2)
return true;
for(LL i= 2; i*i<=x; i++)
{
if(x%i==0)
return false;
}
return true;
}
class Compute
{

public:
int m,n;
Compute(int _m,int _n):m(_m),n(_n) {};
void showResult()
{
if(n>0)
{
LL num = m;
int k = 0;
while(k<n)
{
if(isPrime(num))
{
if(k==0)
cout << num ;
else
cout << " " << num;
k++;
}
num++;
}
}
else if(n<0)
{
n = abs(n);
LL num = m;
int k = 0;
while(k<n&&num>1)
{
if(isPrime(num))
{
if(k==0)
cout << num ;
else
cout << " " << num;
k++;
}
num--;
}
}
cout << endl;
}
};

## AC代码：

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<LL> vec,vec1;
LL check = 0;
void op()
{
for(int i = 1;i<vec.size()-1;i++)
{
LL num = vec[i-1]+vec[i+1];
LL n = abs(num);
if(num<0)
vec[i] = -(n+1)/2;
else
vec[i] = (n+1)/2;
}
while(vec1!=vec)
{
vec1 = vec;
for(int i = 1;i<vec.size()-1;i++)
{
LL num = vec[i-1]+vec[i+1];
LL n = abs(num);
if(num<0)
vec[i] = -(n+1)/2;
else
vec[i] = (n+1)/2;
}

}
return ;
}
int main()
{
LL num;
while(cin >> num)
{
vec.push_back(num);
vec1.push_back(num);
}
if(vec.size()<=2&&vec.size()>0)
{
cout << vec[0] ;
for(int i = 1;i<vec.size();i++)
cout << " " << vec[i];
cout << endl;
}
else if(vec.size()==0)
return 0;
else
{
check = vec[1];
op();
cout << vec[0];
for(int i = 1;i<vec.size();i++)
cout << " " << vec[i];
cout << endl;
}
return 0;
}

## main函数：

int main()
{
int i;
char ch;
cin>>i>>ch;
Overload t1, t2(i), t3(ch), t4(i, ch);
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
{
public:
int a;
char c;
Overload(){printf("Default constructor is called to make a = 0, c = '0'.\n"); }
Overload(int i):a(i){printf("First constructor is called to make a = %d, c = '0'.\n",i); }
Overload(char _c):c(_c){printf("Second constructor is called to make a = 0, c = '%c'.\n",c); }
Overload(int i,char _c):a(i),c(_c){printf("Third constructor is called to make a = %d, c = '%c'.\n",a,c); }
};

## main函数：

int main()
{
int a;
cin>>a;
Difficult difficult(a);
difficult.show();
return 0;
}

## AC代码：

#include <bits/stdc++.h>
using namespace std;
class Difficult
{
public:
int num;
Difficult(int _n):num(_n){};
void show(){printf("%d\n",abs(num)); }
};

## AC代码：

#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
int main()
{
int num = 0;
double sum = 0;
while(cin >>num)
{
sum+=num;
vec.push_back(num);
}
if(sum==0)
{
printf("%.2f",1.00);
for(int i = 1;i<vec.size();i++)
printf(" %.2f",1.00);
printf("\n");
}
else
{
printf("%.2f",vec[0]/sum);
for(int i =  1;i<vec.size();i++)
printf(" %.2f",vec[i]/sum);
printf("\n");
}
return 0;
}

posted @ 2019-06-27 19:32  浅花迷人  阅读(...)  评论(...编辑  收藏