有如下程序: #include<iostream> using namespace std; int main(){ int a[6]={23, 15, 64, 33, 40, 58}; int s1, s2; s

admin2017-10-17  24

问题 有如下程序:
   #include<iostream>
   using namespace std;
   int main(){   
   int a[6]={23, 15, 64, 33, 40, 58};
   int s1, s2;
   s1, s2=a[0];
   for(int*p=a+1; p<a+6; p++)  {
   if(s1>*p)s1=*p;
   if(s2<*p)s2=*p;
   }
   cout<<s1+s2<<end1;
   return 0;
   }
   运行时的输出结果是(    )。
A) 23
B) 58
C) 64
D) 79

选项 A、 
B、 
C、 
D、 

答案D

解析 变量s1和s2的初值都等于23,那么在执行第一次for循环时,*p的值等于15,它小于23,所以把15赋给变量s1;执行第二次for循环时,*p的值等于64,它大于23,所以把64赋给变量s2;而数组a中a[2]后面的元素的值都大于15,小于64,因而变量s1,s2的值不会再发生变化,最终值为15+6=79,所以程序输出79。
转载请注明原文地址:https://kaotiyun.com/show/FJAp777K
0

最新回复(0)