C. Counting Pair

题目链接:戳这里

题意:男生编号1~N,女生编号1~M,给一个sum,问有多少对男女编号加起来等于sum
思路:若sum≤min(n,m),那么有sum-1对(1&sum-1、2&sum-2……);
若sum>max(n,m),那么有N+M-sum+1对(N&sum-N、N-1&sum-N+1……sum-M&M);
否则有min(n,m)对(设n≤m,1&N-1、2&n-2……)

AC Code:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int main() {
 6     int T, n, m, Q, x, ans;
 7     scanf("%d", &T);
 8     for(int t = 1; t <= T; ++t) {
 9         scanf("%d%d%d", &n, &m, &Q);
10         printf("Case #%d:
", t);
11         for(int i = 0; i < Q; ++i) {
12             scanf("%d", &x);
13             if(x <= n && x <= m) {
14                 ans = x - 1;
15             }
16             else if(x > n && x > m) {
17                 ans = n + m - x + 1;
18                 if(ans < 0) ans = 0;
19             }
20             else {
21                 ans = min(n, m);
22             }
23             printf("%d
", ans);
24         }
25     }
26 }

By 区彦开

原文地址:https://www.cnblogs.com/scnuacm/p/3221280.html