主机A向主机B连续发送了两个TCP报文段,其序号分别是70和100。
试问:
(1)第一个报文段携带了多少字节的数据?
(2)主机B收到第一个报文段后发回的确认中的确认号应当是多少?
(3)如果B收到第二个报文段后发回的确认中的确认号是180,试问A发送 的第二个报文段中的数据有多少字节?
(4)如果A发送的第一个报文段丢失了,但第二个报文段到达了B。B在第二 个报文段到达后向A发送确认。试问这个确认号应为多少?
解析 :
第一个报文段的数据序号是 70 − 99 70-99 70−99,共 30 30 30字节数据
B B B期望收到下一个报文段的第一个数据字节的序号是 100 100 100,则确认号是 100 100 100
如果第二个报文段中的数据中的字节数是 180 180 180,那么第二个报文段的数据有 180 − 100 = 80 180-100=80 180−100=80字节
B在第二个报文段到达后向 A A A发送确认,其确认号为 70 70 70
凑篇幅
// Problem: C - ±1 Operation 1
// Contest: AtCoder - Aising Programming Contest 2022(AtCoder Beginner Contest 255)
// URL: https://atcoder.jp/contests/abc255/tasks/abc255_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define IOS ios::sync_with_stdio(false);
#define CIT cin.tie(0);
#define COT cout.tie(0);
#define ll long long
#define x first
#define y second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(),x.end()
#define Fup(i,a,b) for(int i=a;i<=b;i++)
#define Fde(i,a,b) for(int i=a;i>=b;i--)
#define cer(a) cerr<<#a<<'='<<(a)<<" @ line "<<__LINE__<<" "<<endl
typedef priority_queue<int,vector<int>,greater<int>> Pri_m;
typedef pair<int,int> pii;
typedef vector<int> VI;
map<int,int> mp;
const int N = 2e5+10,INF = 0x3f3f3f3f;
const double eps = 1e-5;
struct node{
int to,val;
};
ll x,a,d,n;
void solve(){
cin>>x>>a>>d>>n;
ll _an = a + (n-1) * d;
if(d < 0 ){
swap(_an,a);
d = -d;
}
if(x > _an) cout<<abs(x - _an)<<endl;
else if(x < a) cout<<abs(a - x)<<endl;
else{
if(!d) cout<<0<<endl;
else{
ll t = (x - a)%d;
cout<<min(t,d-t)<<endl;
}
}
}
int main(){
//int t;cin>>t;while(t--)
solve();
return 0 ;
}