#CS406. 阅读程序-指针和引用

阅读程序-指针和引用

阅读程序

注意:切勿用电脑直接运行代码得出答案,请用大脑+笔+纸运行代码答题,否则是在浪费你的时间。

第6节: 指针和引用

第1题【NOIP】2008

#include <iostream>
using namespace std;
void func(int ary[], int n )
{
	int i=0, j, x;
	j=n-1;
	while(i<j)
	{
		while (i<j&&ary[i]>0) i++;
		while (i<j&&ary[j]<0) j--;
		if (i<j){
			x=ary[i];
13			ary[i++]=ary[j];
			ary[j--]=x;
		}
	}
}

int main()
{
	int a[20], i, m;
	m=10;
	for(i=0; i<m; i++)
	{
		cin>>a[i];
	}
	func(a, m);
	for (i=0; i<m; i++)
		cout<<a[i]<<" ";
	cout<< endl;
	return 0;
}

●判断题

(1)将第13行的ary[i++]=ary[j]改为ary[++i]=ary[i],程序输出结果不变。

{{ select(1-1) }}

  • 正确
  • 错误

(2)输入5 4 -6 -11 6 -59 22 -6 1 10,输出结果为5 4 10 16 22 -59 -11 -6 -6

{{ select(1-2) }}

  • 正确
  • 错误

(3)将func函数中的所有变量n全部改成a,程序能通过编译且输出结果不变。

{{ select(1-3) }}

  • 正确
  • 错误

●选择题

(4)该代码时间复杂度为()

{{ select(1-4) }}

  • O(nlogn)
  • O(n2n^2)
  • O(n)
  • O(logn)

第2题【NOIP】2015

#include <iostream> 
using namespace std; 
void fun(char *a, char *b) { 
04    a = b; 
05    (*a)++; 
} 
int main() { 
    char c1, c2, *p1, *p2; 
    c1 = 'A'; 
    c2 = 'a'; 
    p1 = &c1; 
    p2 = &c2; 
    fun(p1, p2); 
    cout << c1 << c2 << endl; 
    return 0; 
}

●判断题

(1)将第5行的(*a)++;改为a+ +;,程序输出结果不变。

{{ select(2-1) }}

  • 正确
  • 错误

(2)*为取地址符。

{{ select(2-2) }}

  • 正确
  • 错误

(3)执行完第4行后c1=c2。

{{ select(2-3) }}

  • 正确
  • 错误

●选择题

(4)输出结果为()。

{{ select(2-4) }}

  • Ab
  • Aa
  • Ba
  • Bb