求助为何代码输入后无输出

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

KRBUG @ 2023-11-08 12:46:25

#include<iostream>
using namespace std;
int main()
{
    int apple[11] ,i,num=0,high;
    for(i=0;i<=10;i++)
    {
        cin >> apple[i];
    }
    cin >> high;
    high = high+30;
    for(i=1;i<=10;i++)
    {
        if(apple[i]<=high)
        {
        num++;  
        }
    }
    cout << num;
    return 0;
 } 

如上代码,输入后无任何输出,貌似进入了死循环?求解


by hmy0213 @ 2023-11-08 12:53:06

@KRBUG 你输入从0-10,那输入了11个数,后面for循环又从1-10,我帮你改了,过了:

#include<iostream>
using namespace std;
int main()
{
    int apple[11] ,i,num=0,high;
    for(i=0;i<=9;i++)
    {
        cin >> apple[i];
    }
    cin >> high;
    high = high+30;
    for(i=0;i<=9;i++)
    {
        if(apple[i]<=high)
        {
        num++;  
        }
    }
    cout << num;
    return 0;
 }

by xian_zhaoyuzhe @ 2024-02-05 19:16:14

#include <bits/stdc++.h>
using namespace std;
int f[11];//数组开到10就行了
int main()
{
    for(int i=1;i<=10;i++)
    {
        cin>>f[i];//连续数十次
    }
    int a=0,sum=0;cin>>a;//定义应用变量,建议都设为0,容易混
    for(int i=1;i<=10;i++)
    {
        if(f[i]<=a+30)
        {
            sum++;//枚举所有相,陶陶是否能拿到
        }
    }
cout<<sum;
}

|