注册 登录
编程论坛 C++教室

请大神帮忙看看哪里出问题了,谢谢

xjf120018 发布于 2018-08-17 16:13, 1262 次点击
https://www.
只得了80分,不知道哪里出问题了
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long a,b,s,i,x,z,h;
    s=0,z=0;
    for(i=1;i<=12;i++)
    {
        cin>>a;
        s=s+300-a;
        if(s>=100)
        {
            x=s-s%100;
            z=z+x;
            s=s-x;
        }
        else if(s<0) h=i;
        if(i==12)
        {
            s=s+z*1.2;
            z=0;
            if(h<1|h>=12) cout<<s;
        }
    }
    if(h>=1&&h<12) cout<<"-"<<h;
    return 0;
}
2 回复
#2
rjsp2018-08-17 16:45
这什么代码呀,那么多变量,还挤在一块儿,故意弄得晦涩难懂?
假如 h=i 从未执行过,那么后来执行 if(h<1|h>=12) 或 if(h>=1&&h<12) 时,你认为h应该何值?
假如 h=i 执行过多次,你怎么复原题目要求的第一次不够用的月份?

程序代码:
#include <cstdio>

int main( void )
{
    unsigned buf[12];
    for( size_t i=0; i!=12; ++i )
        scanf( "%u", &buf[i] );

    unsigned cash=0, save=0;
    for( size_t i=0; i!=12; ++i )
    {
        if( cash+300 < buf[i] )
        {
            printf( "-%zu\n", i+1 );
            return 0;
        }

        cash = cash+300-buf[i];
        save += cash/100*100;
        cash %= 100;
    }
    printf( "%u\n", cash + save + save/5 );
}

#3
xjf1200182018-08-18 18:31
Thank you.
1