PAT/B/1020
这下面的代码是编译不通过的,因为其实是不用创建构造函数的,我看了liuchuo大神的代码才反应过来,直接赋值就可以了
#include <iostream>
#include <vector>
using namespace std;
class bin{
public:
int num;
double price;
double danjia;
bin(int n1,double n2):num(n1),price(n2){
danjia=price/num;
}
};
bool cmp(bin a,bin b)
{
return a>b;
}
int main()
{
int N,sum=0,p;
int xuqiu;
vector <int> tem(N);
vector <bin> a(N);
for(int i=0;i<N;++i)
{
cin>>tem[i];
sum+=tem[i];
}
for(int i=0;i<N;++i)
{
cin>>p;
bin t(tem[i],p);
a[i]=t;
}
vector <int> num(sum);
}
liuchuo大神:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct mooncake{
float mount, price, unit;
};
int cmp(mooncake a, mooncake b) {
return a.unit > b.unit;
}
int main() {
int n, need;
cin >> n >> need;
vector<mooncake> a(n);
for (int i = 0; i < n; i++) scanf("%f", &a[i].mount);
for (int i = 0; i < n; i++) scanf("%f", &a[i].price);
for (int i = 0; i < n; i++) a[i].unit = a[i].price / a[i].mount;
sort(a.begin(), a.end(), cmp);
float result = 0.0;
for (int i = 0; i < n; i++) {
if (a[i].mount <= need) {
result = result + a[i].price;
} else {
result = result + a[i].unit * need;
break;
}
need = need - a[i].mount;
}
printf("%.2f",result);
return 0;
}
我最初提交的时候并没有考虑完善,虽然意识中还是只告诉自己仔细看,但还是忽视了下面的量可能是浮点数,导致只对了80%
以下是被点醒之后自己的代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
class bin{
public:
double num;
double price;
double danjia;
};
bool cmp(bin a,bin b)
{
return a.danjia >b.danjia ;
}
int main()
{
int N,sum=0;
int xuqiu;
double total=0.0;
cin>>N>>xuqiu;
vector <bin> a(N);
for(int i=0;i<N;++i)
{
cin>>a[i].num;
}
for(int i=0;i<N;++i)
{
cin>>a[i].price;
}
for(int i=0;i<N;++i) a[i].danjia=a[i].price/a[i].num;
sort(a.begin() ,a.end(),cmp);
for(int i=0;i<N;++i){
if(sum+a[i].num>xuqiu) {
total+=(xuqiu-sum)*a[i].danjia;
break;
}
sum+=a[i].num;
total+=a[i].price;
}
cout<<fixed<<setprecision(2)<<total;
return 0;
}
PAT/B/1019
这道题中的Tostring 函数是我自己写的但是,不知道为什么单独使用就可以,但是放到这个文件中就不可以用了。
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
bool cmp(char a,char b) {
return a>b;
}
string Tostring(int n) {
string tem;
int t;
for(int i=0; i<4; ++i) {
tem.insert(tem.begin(),t%10+'0');
t/=10;
}
cout<<tem<<' ';
return tem;
}
int main() {
string a,b;
cin>>a;
b=a;
sort(a.begin(),a.end());
sort(b.begin(),b.end(),cmp);
int result=stoi(b)-stoi(a);
if(result==0) {
cout<<b<<" - "<<a<<" = 0000";
return 0;
}
else cout<<b<<" - "<<a<<" = "<<setw(4)<<setfill('0')<<result<<endl;
while(result!=6174) {
a=Tostring(result);
//cout<<a<<' ';
b=a;
sort(a.begin(),a.end());
sort(b.begin(),b.end(),cmp);
result=stoi(b)-stoi(a);
cout<<b<<" - "<<a<<" = "<<setw(4)<<setfill('0')<<result<<endl;
}
return 0;
}
后面凡发现是自己形参给错了,哈哈笑死,以下为改正的
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
bool cmp(char a,char b) {
return a>b;
}
string Tostring(int t) {
string tem;
for(int i=0; i<4; ++i) {
tem.insert(tem.begin(),t%10+'0');//就是这里直接黏贴过来,形参是不一样的
t/=10;
}
return tem;
}
int main() {
string a,b;
cin>>a;
a.insert(0,4-a.size(),'0');
b=a;
sort(a.begin(),a.end());
sort(b.begin(),b.end(),cmp);
int result=stoi(b)-stoi(a);
if(result==0) {
cout<<b<<" - "<<a<<" = 0000";
return 0;
}
else cout<<b<<" - "<<a<<" = "<<setw(4)<<setfill('0')<<result;
while(result!=6174) {
a=Tostring(result);
//cout<<a<<' ';
b=a;
sort(a.begin(),a.end());
sort(b.begin(),b.end(),cmp);
result=stoi(b)-stoi(a);cout<<endl;
cout<<b<<" - "<<a<<" = "<<setw(4)<<setfill('0')<<result;
}
return 0;
}
看了以下liuchuo大神的代码,就是这么简洁高效,发现自己真是一个实足的小菜鸡,要继续努力啊
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(char a, char b) {return a > b;}
int main() {
string s;
cin >> s;
s.insert(0, 4 - s.length(), '0');
do {
string a = s, b = s;
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end());
int result = stoi(a) - stoi(b);
s = to_string(result);
s.insert(0, 4 - s.length(), '0');
cout << a << " - " << b << " = " << s << endl;
} while (s != "6174" && s != "0000");
return 0;
}