Parity 题解
这道题,十分让人无奈,第一次提交,用For,得了80,还好老师讲了以后,幡然醒悟,没有用循环,哈哈,AC了! 代码如下:
Code100%
#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
unsigned long long n,m,c;
int main(){cin>>n>>m;if(m%2==0&&n%2==0) cout<<"-"<<n+(m-n+1)/2;if(m%2==1&&n%2==1) cout<<n+(m-n+1)/2;if(m%2==0&&n%2==1) cout<<"-"<<(m-n+1)/2;if(m%2==1&&n%2==0) cout<<(m-n+1)/2;return 0;
}
大家可以把万能头改成iostream, 对了,这是我的第一次代码:
Code 80%
#include<iostream>
#include<cstdio>
using namespace std;
long long n,m,js,os;
int main(){freopen("Parity.in","r",stdin);freopen("Parity.out","w",stdout);cin>>n>>m;if(n>m) swap(n,m);for(long long i=n;i<=m;i++){if(i%2==0) os+=i;else js+=i; }cout<<js-os;fclose(stdin);fclose(stdout);return 0;
}
这个是80%的代码,去掉文件输入即可!
源网站:T102507 1.奇偶数之差 (Parity.cpp)