沐里纷纷


私信TA

用户名:Epoch

访问量:63715

签 名:

我不会算法

等  级
排  名 37
经  验 12910
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

注意事项:

参考代码:

#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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区