为啥输出结果不对

P1046 [NOIP2005 普及组] 陶陶摘苹果

CodingTim @ 2024-09-28 21:43:57

#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector<int> apples;
    int tall, total = 0;
    for (int i = 0; i < 10; i++)
    {
        int appleTall = 0;
        cin >> appleTall;
        apples.push_back(appleTall);
    }
    cin >> tall;
    for (int i : apples)
    {
        if (i == tall || i == tall + 30)
        {
            total++;
        }
    }
    cout << total << endl;
    return 0;
}

输入: 100 200 150 140 129 134 167 198 200 111 110 输出为什么是1而不是5


by YINANWU @ 2024-09-28 21:46:16

哪道题?


by CodingTim @ 2024-09-28 21:48:03

@YINANWU P1046 [NOIP2005 普及组] 陶陶摘苹果


by CodingTim @ 2024-09-28 21:49:12

#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector<int> apples;
    int tall, total = 0;
    for (int i = 0; i < 10; i++)
    {
        int appleTall = 0;
        cin >> appleTall;
        apples.push_back(appleTall);
    }
    cin >> tall;
    for (int i : apples)
    {
        if (i >= tall || i >= tall + 30)
        {
            total++;
        }
    }
    cout << total << endl;
    return 0;
}

为什么输入: 100 200 150 140 129 134 167 198 200 111 110 输出是9


by YINANWU @ 2024-09-28 21:51:21

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e2+10;
int a[maxn],cnt;
int main(){
    ios::sync_with_stdio(false);
    for (int i=1;i<=10;i++)
        cin>>a[i];
    int m;cin>>m;
    m+=30;
    for (int i=1;i<=10;i++)
        if (m>=a[i])
            cnt++;
    cout << cnt;
    return 0;
}

by YINANWU @ 2024-09-28 21:51:40

自己理解理解,思密达


by YINANWU @ 2024-09-28 21:52:30

2.5年前写的,格式不好不要在意


by laotingrui @ 2024-10-06 21:43:46

@CodingTim \ 'if (i == tall || i == tall + 30)' 中两个'==' 改为 '>='\ 求关~~


by laotingrui @ 2024-10-06 21:48:22

你看看我的

#include<iostream>
using namespace std;
int a[15],k,cnt;
int main(){
    for(int i=1;i<=10;i++)
        scanf("%d",&a[i]);
    scanf("%d",&k);
    k+=30;//总高度
    for(int i=1;i<=10;i++)
        if(k>=a[i]) cnt++;
    printf("%d",cnt);
    return 0;
}

@CodingTim 求关~~


|