首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求赐教,请各位看看这道题,在国外孤立无援

2012-08-14 
求指教,请各位看看这道题,在国外孤立无援。Question:generate two different random integers, s and t, wh

求指教,请各位看看这道题,在国外孤立无援。
Question:

generate two different random integers, s and t, which are different for each message. s is called the start code and t is called the end code.
The value of t is transmitted as the first integer in the encoded output. Then for every integer i in the message, the integer s is sent followed by a
randomly generated sequence L of 1 or more integers that sum to i with the constraint that neither s nor t appears in the sequence.

The entire message is terminated by a single t value. For example, if the message is 3, 21, 5 and the start and end codes are 11 and -91, the message might be transmitted as -91, 11, -1, 5, 2, -3, 11, 21, 11, 4, 0, 1 , -91 (where 3 is represented as -1 + 5 + 2 - 3, 21 is represented as itself and 5 is represented as 4 + 0 + 1).

Non-functional requirements: your program may not use arrays!

Input 
-91 11 -1 5 2 -3 11 21 11 4 0 1 -91

Output
3
21
5
Input
0 1 2 3 4 1 -1 -2 -3 0 

output
9
-6

拜托各位帮帮忙吧,我人在澳洲,刚到这边,没几个会编程的朋友,大部分都是学商科的,这个是学校的作业,我实在是不太明白怎么做。只能求教于网络了。拜托大家.

[解决办法]

C/C++ code
#include <stdio.h>int main(){    int startcode,endcode,nTmp,nRes;    while(scanf("%d",&nTmp)!=EOF)    {        endcode = nTmp;        scanf("%d",&startcode);        nRes=0;        while(scanf("%d",&nTmp)!=EOF)        {            if (nTmp==endcode)            {                printf("%d\n",nRes);                break;            }            else if (nTmp==startcode )            {                printf("%d\n",nRes);                nRes=0;            }            else            {                nRes += nTmp;            }        }    }    return 0;} 

热点排行