NC14650. 开心的cc
描述
输入描述
Input contains multiple test cases.
The first line contains an integer T (1<=T<=20), which is the number of test cases.
Then the first line of each test case contains an integer n (1<=n<=100000), which is the length of “feeling repetend”.
The second line of each test case contains n integers, each integer is either 0 or 1, standing for unhappy or happy.
输出描述
output a integer represents the answer.
示例1
输入:
2 5 1 0 1 1 0 5 1 1 1 1 1
输出:
1 5
说明:
For the sample,the days from the third day as a starting point are as follows:C++14(g++5.4) 解法, 执行用时: 188ms, 内存消耗: 2316K, 提交时间: 2020-04-02 14:31:24
#include<iostream> using namespace std; int main() { int n, t; cin >> t; while (t--) { int a, x = 0, y = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (a == 1)x++; else y++; } if (x - y > 0)cout << x - y << endl; else cout << "0" << endl; } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 108ms, 内存消耗: 268K, 提交时间: 2017-12-19 20:36:02
#include<cstdio> int main(){ int t,n; scanf("%d",&t); while(t--){ int a,x=0,y=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a); if(a==1) x++; else y++; } if(x-y>0) printf("%d\n",x-y); else printf("0\n"); } return 0; }
C 解法, 执行用时: 77ms, 内存消耗: 320K, 提交时间: 2022-04-11 20:14:00
int c,t,n; main(){ scanf("%d",&t); while(t--){ int a=0,b=0; scanf("%d",&n); while(n--){scanf("%d",&c);if(c)++a;else++b;} printf("%d\n",a-b>0?a-b:0); } }