题解:P14495 [NCPC 2025] Arithmetic Adaptation
题解:P14495 [NCPC 2025] Arithmetic Adaptation
本题解使用分类讨论解决。
题意
求出两个非零整数相加等于另一整数的方案(只输出一种)。
思路
因为整数的大小只有几种情况,所以考虑分类讨论。
输入为
if(s==0){
cout<<-999<<" "<<999;
}
输入为正数,需要注意的是,本题要求输出两个非零整数,所以需要特判
if(s==1){
cout<<2<<" "<<-1;
}
else if(s>1){
cout<<1<<" "<<s-1;
}
输入为负数,同理,需要特判
if(s==-1){
cout<<-2<<" "<<1;
}
else{
cout<<-1<<" "<<s+1;
}
完整代码
注意特判的位置,不能放在一般情况之后。
#include<bits/stdc++.h>
using namespace std;
signed main()
{
int s;
cin>>s;
if(s==0){
cout<<-999<<" "<<999;
}
else if(s==1){
cout<<2<<" "<<-1;
}
else if(s>1){
cout<<1<<" "<<s-1;
}
else if(s==-1){
cout<<-2<<" "<<1;
}
else{
cout<<-1<<" "<<s+1;
}
return 0;
}