思路
Part 1
思路
暴力枚举。
双层循环遍历两边 到 的每个数,特判是否为零,如果找到一种解,可以直接输出。
AC Code
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
ll s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s;
for(ll i=-999;i<=999;i++)
{
for(ll j=-999;j<=999;j++)
{
if(i+j==s&&i!=0&&j!=0)//特判
{
cout<<i<<" "<<j;
return 0;
}
}
}
return 0;
}Part 2
思路
随机数。
确定一个随机数 ,使 和另一个随机数 合法。
AC Code
#include<bits/stdc++.h>
#include<chrono>
#define ll long long
#define ull unsigned long long
using namespace std;
ll s,x;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
unsigned seed=chrono::steady_clock::now().time_since_epoch().count();
mt19937 rd(seed);
uniform_int_distribution<long long>rds(-999,999);//限定范围
cin>>s;
x=rds(rd);
while(x==0||x==s||x<-999||x>999||s-x<-999||s-x>999)
{
x=rds(rd);
}
cout<<x<<" "<<s-x;
return 0;
}Part 3
思路
分类讨论。
分 种情况:
AC Code
#include<bits/stdc++.h>
#include<chrono>
#define ll long long
#define ull unsigned long long
using namespace std;
ll s,x;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s;
if(s==0)
{
cout<<"1 -1";
}
if(s==1)
{
ll x=-1,y=2;
cout<<x<<" "<<y;
}
if(s>1&&s!=999)
{
ll x=1,y=s-1;
cout<<x<<" "<<y;
}
if(s==999)
{
cout<<"1 998";
}
if(s==-1)
{
ll x=-2,y=1;
cout<<x<<" "<<y;
}
if(s<-1&&s!=-999)
{
ll x=1,y=s-1;
cout<<x<<" "<<y;
}
if(s==-999)
{
cout<<"-998 -1";
}
return 0;
}