Categories: C/C++ | Tags: | Views: 710

  

写一个关于滚梯的程序:滚梯的台阶一个接一个的出现,人们可以站在滚梯的台阶上。
通过键盘登记在滚梯台阶上的人的数量。在滚梯台阶上的人数(#<Anzahl>),还有当前的新出现的滚梯台阶在每次的运行中被告知。在每个时刻只能看到此刻前5个滚梯台阶(及站在台阶上的人)。
一旦输入一个负数,程序结束。
 
提示:
编译程序,请您使用子程序 zustandTreppe 只输出或者读取下一个台阶的信息。在子程序中输入输出指令被使用。
例如:int person=zustandTreppe( <台阶的阶数>,人数,台阶数); 请在主程序中使用一个do循环。
 
程序输出举例:
 
0 0 0 0 0 – #0 – 1. 台阶? 1
1 0 0 0 0 – #1 – 2. 台阶? 2
2 1 0 0 0 – #3 – 3. 台阶? 0
0 2 1 0 0 – #3 – 4. 台阶? 1
1 0 2 1 0 – #4 – 5. 台阶? 0
0 1 0 2 1 – #4 – 6. 台阶? 0
0 0 1 0 2 – #3 – 7. 台阶? 3
3 0 0 1 0 – #4 – 8. 台阶? -1

给出的答案:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
 
int rolltreppe(int t[5], int personen, int iteration){
    printf("%d %d %d %d %d - # %d &ndash;第 %d 个台阶: ", 
        t[0], t[1], t[2], t[3], t[4], personen, iteration);
    int input; //下一个台阶上将上来的人数
    scanf("%d", &input);
    return input;
}
 
int main(){
    int personen, t[5], iter=0;
    do{
        personen = rolltreppe(t,t[0]+t[1]+t[2]+t[3]+t[4], iter);
        iter=iter+1; //记录滚动了多少次
        t[4] = t[3]; 
        t[3] = t[2]; 
        t[2] = t[1]; 
        t[1] = t[0]; 
        t[0] = personen; 
    } while(personen >= 0);  
}

 

我写的代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#define N 5
 
int rolltreppe(int t[N], int personen, int iteration){
    int i;
    for(i=0;i<N;i++)
        printf("%d ", t[i]);
    printf("- # %d &ndash;第 %d 个台阶: ", personen, iteration);
    int input;
    scanf("%d", &input);
    return input;
}
 
int main(){
    int personen, t[N]={0}, iter=0;
    int i,total;
    do{
        total = 0;
        for( i=0; i<N; i++ ) total += t[i];
        personen = rolltreppe(t,total, iter);
        iter=iter+1; 
        for( i=N-1;i>=0;i-- ) t[i] = t[i-1];
        t[0] = personen;
    } while(personen >= 0);  
    system("pause");
}

 

 

这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明

  1. November 14th, 2009 at 14:11
    Reply | Quote | #1

    过来学习一下,欢迎回访~~

;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).