嘻嘻,我又回来了!
本来想着数模之后写一个数模比赛的总结,但是总是乱七八糟的事情很多,我这matlab只入了个门,写不写的吧哈哈哈哈哈,关于数模中用到的优化代码和好的文章,都在我的收藏夹“优化算法”里面,随时查看就好。
今儿打算刷乙级,但是好像认可度不高,鼓起勇气刷开了甲级,因为很久不接触了,怕自己不会了,就从上次没有满分的那道A1002开始,优化到满分为止!也算预热一下。
https://blog.csdn.net/weixin_44412218/article/details/95319598
这是我之前写的,分数只达到了15/25,今天下午自己刷到了23/25,最后一个测试点嘻嘻参考别人的代码查出来的!
题目很简单吧,可是就是有一些细节找不到……
首先,我发现是输出格式有问题,比如系数是2.0,输出就会变成2,明明设置了精度,还是不行,后来百度了一种强制显示小数点的形式
cout<<setiosflags(ios::showpoint)<<setprecision(2)<<
上次用的
cout<<fixed<<setpresicion(1)<<
不是很管用,就很神奇……不知道是不是编译器bug……
总之这么操作之后,能刷到23/25了
最后一个我实在不知道哪里错了,参考了别人的代码,试了最后多项式为 0系数 0项数的形式,人家的输出是“0”而我的设置为“ 0 0 0.0”所以,emmmmm这个问题题目没有说明白嘛……
改了一下就通过啦!贴代码
#include<iostream>
#include<iomanip>
#include <vector>
using namespace std;
int main()
{
float pol1[1005] = { 0 };
float pol2[1005] = { 0 };
float ans[2011];
int i, j, a1, a2, a, c, d;
float b; int count = 0;
cin >> a1;//非零项个数
while (cin >> a >> b)
{
pol1[a] += b;
count++;
if (count == a1)
break;
}
cin >> a2; count = 0;
while (cin >> a >> b)
{
pol2[a] += b;
count++;
if (count == a2)
break;
}
count = 0;
vector<int>num;
for (j = 0; j<1005; j++)
{
ans[j] = pol1[j] + pol2[j];
if (ans[j] != 0)
num.push_back(j);
}
int l = num.size();
if (l == 0 )
cout << "0";
else if (l == 1)
{
cout << "1 ";
cout << num[0] << " " << setiosflags(ios::showpoint) << setprecision(2) << ans[num[0]];
}
else
{
cout << l << " ";
for (i = l - 1; i>0; i--)
{
c = ans[num[i]];
if (ans[num[i]] == 0)//判断是否是非零项
continue;
cout << num[i] << " " << setiosflags(ios::showpoint) << setprecision(2) << ans[num[i]] << " ";
}
c = ans[num[0]];
cout << num[0] << " " << fixed << setprecision(1) << ans[num[0]];
}
system("PAUSE");
return 0;
}
还有就是关于简化这道题的,可能应对PAT这种,用scanf和printf比较好,看人家的代码,简洁明了……哭辽
#include<cstdio>
int main(){
double a[1010]={};
int m,n;
int e=0;
double k;
int count=0;
scanf("%d",&m);
while(m--){
scanf("%d %lf",&e,&k);
a[e]+=k;
}
scanf("%d",&n);
while(n--){
scanf("%d %lf",&e,&k);
a[e]+=k;
}
for(int i=0;i<=1000;i++){
if(a[i]!=0)
count++;
}
printf("%d",count);
for(int i=1000;i>=0;i--){
if(a[i]!=0){
printf(" %d %.1f",i,a[i]);
}
}
return 0;
}