【问题标题】:xcode scanf not working c [closed]xcode scanf不工作c [关闭]
【发布时间】:2013-02-11 03:52:05
【问题描述】:

为什么这段代码不起作用?

基本思路:

每辆车可以坐 4 个人。 每列火车的第一节车厢长 10 英尺,随后的每节车厢长 8 英尺。 火车只能占总轨道长度的25%。
目标是打印出可以填充火车轨道的最大人数,用户输入最大轨道长度和最大火车长度。

//Roller Coaster Capacity
//Justin Castillo, Section 1, COP 3223, 2/9/2013
//This program calculates the maximum number of people that can be on the
//track at one time.  

int main(void) {

    int max_people_train;
    int num_cars_train;
    int total_num_trains;
    int max_length_track;
    int max_length_train;
    int max_people_track;
    int prev_max_people_track;
    int prev_total_num_trains, i;

    printf("Please enter the track length:\n");
    scanf("%d", &max_length_track);

    printf("Please enter the max length of the train:\n");
    scanf("%d", &max_length_train);

    for (i=10; i<=max_length_train; i+8) {
        num_cars_train = ((i-10)/8 +1);
        max_people_train = (num_cars_train)*4;
        total_num_trains = max_length_track/i;
        max_people_track = total_num_trains*max_people_train;

        prev_max_people_track = prev_total_num_trains * max_people_train;
        prev_total_num_trains = max_length_track/(i-8);

        if (prev_max_people_track > max_people_track)
            max_people_track = prev_max_people_track;
    }



    printf("Your ride can have at most %d people on the track,\n", max_people_track);
    printf("This can be achieved with trains of %d cars.\n", num_cars_train);


    system("pause");
    return 0;
}

【问题讨论】:

    标签: c if-statement for-loop


    【解决方案1】:

    我猜这与此有关:

    for (i=10; i<=max_length_train; i+8) {
    //  ============================^^^ WTF ?
    

    试试:

    for (i=10; i<=max_length_train; i+=8) {
    

    【讨论】:

    • @user2044189 如果此答案解决了您的问题,您应该接受它作为正确答案。常见问题解答How do I ask questions here? 的这一部分,解释了如何做到这一点。
    • @Nocturno 我怀疑他早已不在,但我当然感谢您的支持,先生。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多