解题思路:
注意事项:
参考代码:
#include <iostream> #include <vector> #include <stdio.h> using namespace std; vector< pair<bool, bool> > ants; // loc, dir int main() { int n = 0, infector = 0; cin >> n; for (int i = 0; i < n; i++) { int temp = 0; cin >> temp; if (i == 0) infector = temp; else { bool loc, dir; dir = temp * infector > 0 ? true : false; if (dir) loc = temp - infector > 0 ? true : false; else loc = temp + infector < 0 ? true : false; ants.push_back(make_pair(loc, dir)); } } int ans = 0; bool susp = false; for (int i = 0; i < ants.size(); i++) { if (ants.at(i).first == true && ants.at(i).second == false) { ans += 1; susp = true; } } for (int i = 0; i < ants.size(); i++) if (ants.at(i).first == false && ants.at(i).second == true && susp) ans += 1; ans += 1; // Add infector cout << ans << endl; return 0; } 【思路二】 #include <iostream> #include <algorithm> #include <math.h> using namespace std; int a[60]; int n; int cnt; bool cmp(int a, int b){ return abs(a) < abs(b); } int main(void){ cin >> n; int first = 0; bool flag = false; for(int i = 0; i < n; i++){ cin >> a[i]; if(i == 0) first = a[i]; } sort(a, a + n, cmp); // for(int i = 0; i < n; i++){ // cout << a[i] << " "; // } // cout << endl; if(first > 0){ for(int i = 0; i < n; i++){ if(abs(a[i]) > first && a[i] < 0){ cnt++; flag = true; } } for(int i = 0; abs(a[i]) < first ; i++){ if(flag && a[i] > 0) cnt++; } } else if(first < 0){ for(int i = 0; abs(a[i]) < -first; i++){ if(a[i] > 0){ cnt++; flag = true; } } for(int i = 0; i < n; i++){ if(abs(a[i]) > -first && flag && a[i] < 0) cnt++; } } cout << cnt+1; return 0; }
0.0分
0 人评分
IP判断 (C语言代码)浏览:924 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:562 |
C语言训练-斐波纳契数列 (C语言代码)浏览:2826 |
高精度加法 (C++代码)(大数加法)浏览:934 |
C语言程序设计教程(第三版)课后习题9.8 (Java代码)浏览:1642 |
三角形 (C++代码)递归(存在大量重复计算,容易出现时间超限)浏览:781 |
A+B for Input-Output Practice (V) (C语言代码)浏览:469 |
单词个数统计 (C语言代码)浏览:1012 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:519 |
1231题解(注意理解“输入多个测试实例”)浏览:789 |