若有以下程序: #include using namespace std; int fun() { static int i=0: int s=1: s+=i: 1++: return S:

admin2013-09-11  7

问题 若有以下程序:
#include
    using namespace std;
    int fun()
    {
    static int i=0:
    int s=1:
    s+=i:
    1++:
    return S:
    }
    int main()
    {
    jnt i,a=0;
    for(i=0;i<5;i++)
    a+=fun();
    cout<    return 0:
    }
程序运行后,输出的结果是(    )。

选项 A、20
B、24
C、25
D、15

答案D

解析 由主函数入手,在for循环中调用fun函数,其中调用次数为5。fun中i为静态变量,有效到文件结束。第一次调用fun后为s=1,a=1;第二次调用后s=2,a=3第三次s=3,a=6;第四次s=4,a=10;第五次s=5,a=15。
转载请注明原文地址:https://jikaoti.com/ti/deXiFFFM
0

最新回复(0)