首页 > 试题广场 >

好多鱼!

[编程题]好多鱼!
  • 热度指数:8410 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛有一个鱼缸。鱼缸里面已经有n条鱼,每条鱼的大小为fishSize[i] (1 ≤ i ≤ n,均为正整数),牛牛现在想把新捕捉的鱼放入鱼缸。鱼缸内存在着大鱼吃小鱼的定律。经过观察,牛牛发现一条鱼A的大小为另外一条鱼B大小的2倍到10倍(包括2倍大小和10倍大小),鱼A会吃掉鱼B。考虑到这个,牛牛要放入的鱼就需要保证:
1、放进去的鱼是安全的,不会被其他鱼吃掉
2、这条鱼放进去也不能吃掉其他鱼
鱼缸里面已经存在的鱼已经相处了很久,不考虑他们互相捕食。放入的新鱼之间也不会相互捕食。现在知道新放入鱼的大小范围[minSize,maxSize](考虑鱼的大小都是整数表示),牛牛想知道有多少种大小的鱼可以放入这个鱼缸。

输入描述:
输入数据包括3行. 第一行为新放入鱼的尺寸范围minSize,maxSize(1 ≤ minSize,maxSize ≤ 1000),以空格分隔。
第二行为鱼缸里面已经有鱼的数量n(1 ≤ n ≤ 50)
第三行为已经有的鱼的大小fishSize[i](1 ≤ fishSize[i] ≤ 1000),以空格分隔。


输出描述:
输出有多少种大小的鱼可以放入这个鱼缸。考虑鱼的大小都是整数表示
示例1

输入

1 12 1 1

输出

3
#include<iostream>
#include<cmath>
using namespace std;

int main(){
	int minSize, maxSize, n, fishsize, count = 0;
	int size[1001] = { 0 };
	cin >> minSize >> maxSize >> n;
	for (int i = 1; i <= n; i++){
		cin >> fishsize;
		for (int j = 2 * fishsize; j <= maxSize && j <= 10 * fishsize; j++)
			size[j] = 1;
		for (int j = fishsize / 2; j >= minSize && j >= ceil(fishsize / 10.0); j--)
			size[j] = 1;
	}
	for (int i = minSize; i <= maxSize; i++)
	    if (size[i] == 0)
		    count++;
	cout << count;
	return 0;
}

编辑于 2017-03-10 18:36:09 回复(7)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        //接受输入参数
        Scanner scanner = new Scanner(System.in);
        int minSize = scanner.nextInt(); //新放入鱼范围的最小值
        int maxSize = scanner.nextInt();//新放入鱼范围的最大值
        int inNumber = scanner.nextInt();//鱼缸里面已经有鱼的数量
        int[] inFishSize = new int[inNumber]; //定义一个数组
        for (int i = 0; i < inNumber; i ++) {
            inFishSize[i] = scanner.nextInt();//初始化已经有的鱼的大小inFishSize[i]
        }

        int canPut = 0;//可以放进鱼的种类数

        for (int i = minSize; i <= maxSize; i ++) { //遍历输入的范围,逐个查看每中大小的鱼是否可以放进去
            boolean willBeEaten = false; //是否被吃,true 被吃。
            for (int j = 0; j < inNumber; j ++) {
                if ((10 * i >= inFishSize[j] && 2 * i <= inFishSize[j])
                        || (i >= inFishSize[j] * 2 && i <= inFishSize[j] * 10)) {
                    willBeEaten = true;
                    break; //一旦发现此鱼不能放进去,break,跳出循环,避免多余的检查
                }
            }
            //如果这条鱼可以不被鱼缸中其他鱼吃。或者被吃。则canPut++
            if (!willBeEaten) {
                canPut ++;
            }
        }
        System.out.println(canPut);
    }
}


发表于 2017-03-12 10:51:53 回复(8)
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int minSize = in.nextInt();
		int maxSize = in.nextInt();
		int n = in.nextInt();
		int[] fishSize = new int[n];
		for (int i = 0; i < n; i++) {
			fishSize[i] = in.nextInt();
		}
		int res=func(minSize,maxSize,fishSize);
		System.out.println(res);
	}
	private static int func(int min, int max, int[] src){
		if(src==null || src.length==0) return max-min+1;
		if(max*10<src[0] || src[src.length-1]*10<min) return max-min+1;

		int res=0;
		int[] tmp=new int[max-min+1];
		Arrays.sort(src);
		int left1=src[0]/2;
		int left2=(int)(Math.ceil(src[0]/10.0));
		int right1=src[0]*2;
		int right2=src[0]*10;
		
		for(int i=0;i<src.length;i++){
			left2=(int)(Math.ceil(src[i]/10.0));
			left1=src[i]/2;
			right1=src[i]*2;
			right2=src[i]*10;
			
			mark(tmp,min,left2,left1,right1,right2);
		}
		for(int val: tmp){
			if(val==0) res++;
		}
		return res;
	}
	//将[a1,a2] && [b1,b2]在src中出现的mark
	private static void mark(int[] src, int base, int a1, int a2, int b1, int b2){
		for(int i=0;i<src.length;i++){
			int pos=i+base;
			if(a1<=pos && pos<=a2){
				src[i]++;
			}else if(b1<=pos && pos<=b2){
				src[i]++;
			}
		}
	}
} 
发表于 2017-03-08 14:05:51 回复(0)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
  
public class Main {
  
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        Set<Integer> set = new HashSet<>();
        for(int i = a ; i <= b ; i++) {
            set.add(i);
        }
        for(int i = 0; i < c;i++) {
            inte = sc.nextInt();
            for(int j = e * 2; j <= e * 10;j++) {
                if(set.contains(j)) {
                    set.remove(j);
                }
            }
            intx = 0;
            if(e == (int)(e / 10) * 10) {
                x = e / 10;
            }else{
                x = e / 10+ 1;
            }
            for(int j = x ; j <= e / 2;j++) {
                if(set.contains(j)) {
                    set.remove(j);
                }
            }
        }
        sc.close();
        System.out.println(set.size());
    }
}

发表于 2018-10-11 16:31:18 回复(0)
import java.util.*;
public class Main{
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
        int minsize=sc.nextInt();//小鱼体型
        int maxsize=sc.nextInt();//大鱼体型
        int hasCount=sc.nextInt();//鱼缸已经有的条数
        int hasSize[]=new int[hasCount];//这么多体型各异的鱼
        for(int i=0;i<hasCount;i++)
            hasSize[i]=sc.nextInt();
        
        int result=0;//计数

		for(int j=minsize;j<=maxsize;j++){
           for(int k=0;k<hasSize.length;k++){
              if(j>=2*hasSize[k]&&j<=10*hasSize[k]||hasSize[k]>=2*j&&hasSize[k]<=10*j){
                 result++;//会被吃掉的
                  break;
              }
           }
        }
        System.out.println(maxsi敏感词size+1-result);//总体型数-被吃掉的=没被吃掉的
	}
}


发表于 2017-09-14 00:36:37 回复(1)
a1=raw_input()
a2=a1.split(' ')
a3=list(map(int,a2))
a_min=min(a3)
a_max=max(a3)
b1=raw_input()
b=int(b1)
c1=raw_input()
c2=c1.split(' ')
c3=list(map(int,c2))
c=list(c3)
d=list(range(a_min,a_max+1))
e=[]
for x in range(a_min,a_max+1):
    for y in c:
        if 2*x <= y <= 10* x or 2*y <= x <= 10*y:
            e.append(x)
        else:          
            pass            
f=set(d)
g=set(e)
h=len(f-g)
print h
        
发表于 2017-03-20 19:48:30 回复(0)
//此题用的是暴力枚举
//要点:从可能的结果集合中依次扫描,然后判断每个情况,如果满足条件,就加1
#include <iostream>
#include <ctime>
using namespace std;

int main(){
int minSize;
int maxSize;
int n;
int count = 0;
cin >> minSize >> maxSize;
cin >> n;
int *fishSize = new int[n];
for (int i = 0; i<n; i++){
cin >> fishSize[i];
}
//minSize~maxSize为可能的结果的集合
for (int i = minSize; i <= maxSize; i++){
bool flag = true;
for (int j = 0; j<n; j++){
//用来判断条件的【此地方为从判断违反条件就跳出判断,继续判断集合中下一个可能的大小】
if (((i >= 2 * fishSize[j] && i <= 10 * fishSize[j])) ||
((fishSize[j] >= 2 * i && fishSize[j] <= 10 * i))){
flag = false;
break;
}
}
if (flag){
count++;
}
}
cout << count << endl;

return 0;

}

发表于 2017-07-24 16:50:22 回复(0)
/***************************
这题目有问题,是同时放入多少条还是分别可以放多少种,同时放需要考虑放入的鱼的影响,分开放就不需要,本方法是同时放的方法。
***************************/
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
vector<int> fishSize(1000, 0);
int n;
int minSize;
int maxSize;
bool in;
bool inf[2000] = {false};
//memset(inf, false, 1000);
cin >> minSize >> maxSize;
cin >> n;
int t;
int m=0;
for (int i = 0; i < n; i++)
{
cin >> t;
if (!inf[t])
{
fishSize[i] = t;
inf[fishSize[i]] = true;
m++;
}
}
for (int j = minSize; j <= maxSize; j++)
{
for (int i = 0; i < 1000; i++)
{
if (j == fishSize[i] || j >= fishSize[i] * 2 && j <= fishSize[i] * 10 || fishSize[i] >= j * 2 && fishSize[i] <= j * 10)
{
in = false;
break;
}
in = true;
}
if (in)
{
fishSize[j]=j;
m++;
}
}
cout << m << endl;
return 0;
}
发表于 2017-03-16 14:18:43 回复(3)
美运行。
最开始把题想复杂了,实际上出题的意图没那么复杂。
简单说下思路,根据已经在的鱼大小,找出需要排除的范围,然后在根据投入的范围,去掉之前排除的范围,剩下的就都是可以放入的大小种类了。
就这么简单,你们不要想复杂了哦。
<?php
// 获取输入
$in_size  = trim(fgets(STDIN)); // 最小、最大尺寸
$in_has   = trim(fgets(STDIN)); // 已有数量
$in_hsize = trim(fgets(STDIN)); // 已有尺寸
// 验证输入-省略
// 排除范围
list($min, $max) = explode(' ', $in_size);
$sizes           = explode(' ', $in_hsize);
$except          = [];
for ($i = 0; $i < $in_has; $i++) {
    $size = $sizes[$i];
    for ($n = max(ceil($size / 10), $min); $n <= min(floor($size / 2), $max); $n++) {
        $except[] = $n;
    }
    for ($m = max($size * 2, $min); $m <= min($size * 10, $max); $m++) {
        $except[] = $m;
    }
}
$except = array_unique($except);
$new = 0;
for ($s = $min; $s <= $max; $s++) {
    if (!in_array($s, $except)) {
        $new++;
    }
}
echo $new . PHP_EOL;
编辑于 2017-08-12 01:56:52 回复(1)
#include <bits/stdc++.h> using namespace std; int a[55]; int mi,ma; int n; int main(){ cin >> mi >> ma; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; int ans = 0; for(int i = mi; i <= ma; i++){ int flag = 1; for(int j = 0; j < n; j++){ if( (i >= a[j] * 2 && i <= a[j] * 10) || (i * 2 <= a[j] && i * 10 >= a[j]) ){ flag = 0; } } if(flag) ans++; } cout << ans << endl; }
发表于 2017-03-08 11:02:13 回复(0)
//java
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner console=new Scanner(System.in);
		int MinSize=console.nextInt();
		int MaxSize=console.nextInt();
		int n=console.nextInt();
		int count=0;
		int[] fishSize=new int[n];
		for(int i=0;i<n;i++) {
			fishSize[i]=console.nextInt();
		}
		for(int i=MinSize;i<=MaxSize;i++) {
			int flag=0;
			for(int j=0;j<n;j++) {
				if((2*i<=fishSize[j]&&10*i>=fishSize[j])||(fishSize[j]*2<=i&&fishSize[j]*10>=i)) {
					flag=1;
					break;
				} 
			}
			if(flag==0) {
				count++;
			}
		}
		
		System.out.println(count);
	}

}

发表于 2020-04-14 19:10:19 回复(0)
感觉我这个思路应该特别简单。。
import sys
lines = sys.stdin.readlines()
minsize,maxsize =[int(i) for i in lines[0].split()]
n = int(lines[1])
fishsize = list(map(int, lines[2].split()))
k=0;
for i in range(minsize,maxsize+1):
    for j in range(0,len(fishsize)):
        if (i>=2*fishsize[j] and i<=10*fishsize[j]) or (i<=1/2*fishsize[j] and i>=1/10*fishsize[j]):
            k=k+1
            break
num=maxsi***size+1-k
print(int(num))
发表于 2020-03-30 16:16:05 回复(0)
const willBeEaten = (size, before) => {
    for(let i = 0; i < before.length; i++){
        if(before[i]/size < 2 || before[i]/size >10)return false
    }
    return true
}

const willEatOthers = (size, before) => {
    for(let i = 0; i < before.length; i++){
        if(size/before[i] < 2 || size/before[i] >10)return false
    }
    return true
}

const main = (min, max, num, size) => {
    var count = 0;
    for(let i = min; i <= max; i++){
        if(!willBeEaten(i, size) && !willEatOthers(i, size))count++
    }
    return count
}

console.log(main(1, 36, 1, [3]))//expect: 10

发表于 2020-03-20 19:35:59 回复(0)
#include <iostream>
using namespace std;
int main() {
    int flag,max,min,num,count = 0;
    cin>>min>>max>>num;
    int* list = new int[num];
    for(int i=0 ; i<num ; i++){
        cin>>list[i];
    }
    for(int i=min ; i<=max ; i++){
        flag = 1;
        for(int j=0; j<num ; j++){
            if(!(list[j]<0.1*i || list[j]>10*i || (list[j]>0.5*i &&list[j]<2*i)))
                flag = 0;
        }
        if(flag==1) count++;
    }
    cout<<count;
    return 0;
}
发表于 2020-02-14 18:28:37 回复(0)
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
 int minSize;
 int maxSize;
 scanf("%d %d", &minSize, &maxSize);
 int size = maxSi***Size + 1;
 int* s = new int[size] {};
 int n;
 scanf("%d", &n);
 int fish;
 while (n--) {
  scanf("%d", &fish);
  for (int i = minSize; i <= maxSize; ++i) {
   if(i>=2*fish && i<=10*fish || i>=ceil(double(fish)/10)&&
    i<=floor(double(fish)/2))
    s[i - minSize] = 1;
  }
 }
 int sum = 0;
 for (int i = 0; i < size; ++i) {
  if (s[i] == 0)
   ++sum;
 }
 delete[] s;
 printf("%d", sum);
}

发表于 2019-05-02 19:49:37 回复(0)
class Solution(object):  def solution(self, min_size, max_size, had_num, fish_sizes):  res = [] for i in range(min_size, max_size + 1):  for j in fish_sizes:  if 2 <= i / j <= 10:  continue  else:  res.append(i) return len(res)

发表于 2019-01-28 10:36:11 回复(0)
inp=input().split() inp=[int(i) for i in inp] minSize=inp[0] maxSize=inp[1] n=int(input()) fishSize=input().split() fishSize=[int(i) for i in fishSize] count=0 for i in range(minSize,maxSize+1):     beE=False     for j in fishSize:         if i >=2*j and i<=10*j or j>=2*i and j<=10*i:             beE=True     if not beE:         count+=1 print(count)

发表于 2018-08-08 20:25:37 回复(0)
import sys


class Fish:
    def get_rs(self, origin, arr):
        fishes = origin

        count = 0
        for fish in arr:
            flag = True
            for each in fishes:
                if fish == 81:
                    print(2 * each <= fish <= 10 * each, each)
                    print(2 * fish <= each <= 10 * each, each)
                if 2 * each <= fish <= 10 * each or 2 * fish <= each <= 10 * fish:
                    flag = False
                    break
            if flag:
                count += 1
        print(count)


if __name__ == '__main__':
    min_s, max_s = map(int, sys.stdin.readline().split())
    n = int(sys.stdin.readline())
    all_fish = list(map(int, sys.stdin.readline().split()))
    f = Fish()
    f.get_rs(all_fish, range(min_s, max_s + 1))
发表于 2018-07-30 21:58:53 回复(0)
写得很烂

#include <iostream>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#include <set>

using namespace std;

int main()
{
    int minsize, maxsize;
    cin >> minsize >> maxsize;

    std::set<int> results;
    for (int i = minsize; i <= maxsize; ++i)
        results.insert(i);

    set<int> fishset;
    int n, fishsize;
    cin >> n;
    for (int i = 0; i<n; ++i)
    {
        cin >> fishsize;
        fishset.insert(fishsize);
    }

    for (auto it:fishset)
    {
        int eatmin = it / 10, eatmax = it / 2;
        if (eatmin * 10 < it) eatmin++;
        for (int i = MAX(minsize, eatmin); i <= MIN(maxsize, eatmax); ++i)
            results.erase(i);

        int eatedmin = it * 2, eatedmax = it * 10;
        if (eatedmin<=maxsize)
            for (int i = eatedmin; i <= MIN(maxsize, eatedmax); ++i)
                results.erase(i);
    }

    cout << results.size();

    return 0;
}

发表于 2018-04-22 01:21:50 回复(0)
#include<stdio.h>
int main(){
    int mini, maxi, n;
    int i, j, count = 0;
    int flag = 0;
    int fishSize[52] = { 0 };
    scanf("%d%d%d", &mini, &maxi, &n);
    for (i = 0; i < n; ++i){
        scanf("%d", &fishSize[i]);
    }
    for (i = mini; i <= maxi; ++i){
        flag = 1;
        for (j = 0; j < n; ++j){
            // i eat j
            if (fishSize[j] * 2 <= i && i <= fishSize[j] * 10){
                flag = 0;
                break;
            }
            // j eat i
            if (i * 2 <= fishSize[j] && fishSize[j] <= i * 10){
                flag = 0;
                break;
            }
        }// for
        if (flag == 1){
            count += 1;
        }
    }
    printf("%d\n", count);
    return 0;
}

发表于 2018-04-19 08:46:48 回复(0)

热门推荐

通过挑战的用户

好多鱼!