csp2019-03
作者:互联网
加油哦~
第一题
201903-1 小中大
这里AC出了70,后来发现是自己审题不够细致,忽略了求中位数的时候,两个数相加除以2,并不一定会都是小数。
修改后:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int n;
cin>>n;
long num;
long top=0,tail=0,m1=0,m2=0;
for(int i=0;i<n;i++) {
cin>>num;
if(i==0) top=num;
else if(i==n-1) tail=num;
else if(i==n/2) m2=num;
else if(i==(n/2-1)) m1=num;
}
long maxn=top;
long minn=tail;
maxn=top>tail?top:tail;
minn=top<tail?top:tail;
if(n%2==0){
if((m1%2==0&&m2%2==1)||(m1%2==1&&m2%2==0)) {
double medium;
medium=(m1+m2)/2.0;
cout<<maxn<<" "<<fixed<<setprecision(1)<<medium<<" "<<minn;
}
else{
long medium;
medium=(m1+m2)/2;
cout<<maxn<<" "<<fixed<<setprecision(1)<<medium<<" "<<minn;
}
}
else cout<<maxn<<" "<<m2<<" "<<minn;
return 0;
}
第二题
csp2019-03-2 二十四点
终于搞完了,卡在这里好久了!T^T我知道很简单,但是我的程序总是有点问题AC不出100分
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int judge(char* s){
vector<int> num;
vector<char> op;
for(int i=0;i<7;i++){
if(s[i]=='-'||s[i]=='+') op.push_back(s[i]);
else if(s[i]=='x'){
int t=num[num.size()-1];
num.pop_back();
num.push_back(t*(s[i+1]-'0'));
i++;
}
else if(s[i]=='/'){
int t=num[num.size()-1];
num.pop_back();
num.push_back(t/(s[i+1]-'0'));
i++;
}
else num.push_back((s[i]-'0'));
}
int ans=0;
if(num.size()==1) ans=num[0];
else{
for(int i=0;i<op.size();i++){
if(op[i]=='+') {
if(i==0) ans=ans+num[i]+num[i+1];
else ans=ans+num[i+1];
}
if(op[i]=='-') {
if(i==0) ans=ans+num[i]-num[i+1];//注意这里计算的区分!!!!!!
else ans=ans-num[i+1];
}
}
}
return ans==24;
}
int main(){
char s[8];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>s;
judge(s);
if(judge(s)==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
标签:03,csp2019,int,top,back,else,num,ans 来源: https://blog.csdn.net/qq_43710001/article/details/121561641