#CS401. 阅读程序-模拟策略
阅读程序-模拟策略
阅读程序
注意:切勿用电脑直接运行代码得出答案,请用大脑+笔+纸运行代码答题,否则是在浪费你的时间。
第一节:模拟策略
第1题【NOIP】2015
#include <iostream>
using namespace std;
int main() {
int a, b, c; a = 1;
b = 2;
c = 3;
if(a > b)
if(a > c)
cout << a << ' ';
else
cout << b << ' ';
cout << c << endl;
return 0;
}
●判断题
(1)把第1行iostream改为cstdio,程序仍然可以正常运行。
{{ select(1-1) }}
- 对
- 错
(2)如果把a的初值改为一1,结果不会改变。
{{ select(1-2) }}
- 对
- 错
(3)程序结果为3。
{{ select(1-3) }}
- 对
- 错
(4)如果把c的初值改为5,结果不会改变。
{{ select(1-4) }}
- 对
- 错
●选择题
(5)把b的的初值改为4,结果为( )。
{{ select(1-5) }}
- 1
- 2
- 3
- 4
(6)该程序的最坏时间复杂度为( )。
{{ select(1-6) }}
- O(1)
- O(n)
- O(n*m)
- O(2")
第2题【NOIP】2013
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a << "+" << b << "=" << a + b << endl;
}
●判断题
(1)当a,b为负数且(a+b)在int范围内,输出的等式在数学上也成立。
{{ select(2-1) }}
- 对
- 错
(2)当输入为0 0
时,输出为0 + 0 = 0
。
{{ select(2-2) }}
- 对
- 错
(3)将03行的int改为signed程序不会出错。
{{ select(2-3) }}
- 对
- 错
(4)当输出为“-1+1=0”时候,输入可以为“2 -2”。
{{ select(2-4) }}
- 对
- 错
●选择题
(5)输入为“114514 1919810”时,输出为( )。
{{ select(2-5) }}
- 114514+1919810=2034324
- 114514+1919810=2034314
- 114514+1919810=2024324
- 114514+1919810=1034324
(6)时间复杂度为( )。
{{ select(2-6) }}
- O((a+b)ln(a-b))
- O(1)
第3题【NOIP】2014
#include <iostream>
using namespace std;
int main(){
int a, b, c, d, ans;
cin >> a >> b >> c;
d = a - b;
a = d + c;
ans = a * b;
cout << "Ans=" << ans << endl;
return(0);
}
●判断题
(1)ans一定为c的倍数。
{{ select(3-1) }}
- 对
- 错
(2)输入为“8 8 8”,输出为“64”。
{{ select(3-2) }}
- 对
- 错
(3)将04行移动到02 03行之间,程序不会发生错误。
{{ select(3-3) }}
- 对
- 错
(4)将09行的ans改为a*b,输出结果不会发生改变。
{{ select(3-4) }}
- 对
- 错
●选择题
(5)输入为“2 3 4”时,答案为( )。
{{ select(3-5) }}
- Ans=8
- Ans=9
- Ans=10
- 9
(6)将06的“-”改为“+”,(5)的输出为( )。
{{ select(3-6) }}
- Ans=27
- Ans=36
- Ans=54
- Ans=81
第4题【NOIP】2012
#include <iostream>
using namespace std;
int a,b,c,d,e,ans;
int main(){
cin>>a>>b>>c;
d=a+b;
e=b+c;
ans=d+e;
cout<<ans<<endl;
return 0;
}
●判断题
(1)当输入为“11 45 14”时,输出为“70”。 ( )
{{ select(4-1) }}
- 对
- 错
(2)输入的数都为负数时,输出可能为正数。 ( )
{{ select(4-2) }}
- 对
- 错
(3)输入的数都为正数时,输出可能为负数。 ( )
{{ select(4-3) }}
- 对
- 错
(4)将ans的类型改为char,输出结果不会改变。 ( )
{{ select(4-4) }}
- 对
- 错
●选择题
(5)输入为19 19 810
时,输出为( )。
{{ select(4-5) }}
- 867
- 857
- 967
- 767
(6)输出为“28”时,输入可以为( )。
{{ select(4-6) }}
- 5 8 7
- 1 5 7
- 1 1 25
- 1 1 4
第5题【NOIP】2011
#include<iostream>
using namespace std;
int main(){
int i,n,m,ans;
cin>>n>>m;
i=n;
ans=0;
while(i<=m){
ans+=i;
i++;
}
cout<<ans<<endl;
return 0;
}
●判断题
(1)删去第7行,运行结果不变。
{{ select(5-1) }}
- 对
- 错
(2)将第8行的<=改为<,输出减小n。
{{ select(5-2) }}
- 对
- 错
(3)可以实现一个复杂度为O(1)的代码,效果与上述代码等价。
{{ select(5-3) }}
- 对
- 错
(4)当m<n时,程序不会运行错误。
{{ select(5-4) }}
- 对
- 错
●选择题
(5)输入10 20,输出( )。
{{ select(5-5) }}
- 5
- 165
- 20
- 10
(6)时间复杂度为( )。
{{ select(5-6) }}
- O(max{m-n,0})
- O(n)
- O()
- O(mlogn)
第6题【NOIP】2015
#include <iostream>
using namespace std;
struct point{
int x;
int y;
};
int main(){
struct EX {
int a;
int b;
point c;
} e;
e.a = 1;
e.b = 2;
e.c.x= e.a + e.b;
e.c.y= e.a * e.b;
cout << e.c.x << ',' << e.c.y << endl;
return 0;
}
●判断题
(1)输出结果为“3 2”。
{{ select(6-1) }}
- 对
- 错
(2)将08~12行移到06~07行之间,程序运行结果不会发生改变。
{{ select(6-2) }}
- 对
- 错
(3)将01行的iostream改为cstdio程序运行结果不会发生改变。
{{ select(6-3) }}
- 对
- 错
(4)程序的时间复杂度为O(1)。
{{ select(6-4) }}
- 对
- 错
●选择题
(5)该程序会输出( )个数。
{{ select(6-5) }}
- 1
- 2
- 3
- 0
(6)将13行改为e.a=4;输出将为( )。
{{ select(6-6) }}
- 6,8
- 6,7
- 7,8
- 8,6
第7题【NOIP】2009
#include <iostream>
using namespace std;
int main()
{
int a[3],b[3];
int i,j,tmp;
for (i=0;i<3;i++)
cin >> b[i];
for (i=0;i<3;i++)
{
a[i]=0;
for (j=0;j<=i;j++)
{
a[i]+=b[j];
b[a[i]%3]+=a[j];
}
}
tmp=1;
for (i=0;i<3;i++)
{
a[i]%=10;
b[i]%=10;
tmp*=a[i]+b[i];
}
cout << tmp << endl;
return 0;
}
●判断题
(1)输入的3个数越大,则输出的结果也越大。 ( )
{{ select(7-1) }}
- 对
- 错
(2)执行完第17行后,b[i]比a[i]相对应的值要大。 ( )
{{ select(7-2) }}
- 对
- 错
(3)将第18行的tmp=1改为tmp=0,不论输入什么数据,输出结果都是0。 ( )
{{ select(7-3) }}
- 对
- 错
(4)若输入数据中包含字母,如 3 3 a 则程序会出错。 ( )
{{ select(7-4) }}
- 对
- 错
●选择题
(5)输入2 3 5,输出的结果是( )。
{{ select(7-5) }}
- 414
- 415
- 416
- 417
(6)输入1 1 1,输出的结果是( )。
{{ select(7-6) }}
- 36
- 48
- 72
- 108
第8题【NOIP】2009
#include <iostream>
using namespace std;
const int c=2009;
int main()
{
int n,p,s,i,j,t;
cin >> n >> p;
s=0;t=1;
for(i=1;i<=n;i++)
{
t=t*p%c;
for(j=1;j<=i;j++)
s=(s+t)%c;
}
cout << s << endl;
return 0;
}
●判断题
(1)将12 13行改为s=(s+111*t*i)%c;
,程序输出不会改变。
( )
{{ select(8-1) }}
- 对
- 错
(2)将15行改为printf(“%d\n”,s);
程序输出不会改变。( )
{{ select(8-2) }}
- 对
- 错
(3)将08行的s=0;去掉,程序输出不会改变。( )
{{ select(8-3) }}
- 对
- 错
(4)将03行const去掉,程序输出不会发生变化。( )
{{ select(8-4) }}
- 对
- 错
●选择题
(5)输入为“11 2”,输出为( )。
{{ select(8-5) }}
- 782
- 762
- 802
- 114
(6)该算法的时间复杂度为( )。
{{ select(8-6) }}
- O(1)
- O(n)
- O()
- O(nlogn)
第9题【NOIP】2008
#include<iostream>
using namespace std;
int main()
{
int i, a, b, c, d, f[4];
for(i = 0; i < 4; i++) cin >> f[i];
a = f[0] + f[1] + f[2] + f[3];
a = a / f[0];
b = f[0] + f[2] + f[3];
b = b / a;
c = (b * f[1] + a) / f[2];
d = f[(b / c ) % 4];
if(f[(a + b + c + d) % 4] > f[2])
cout << a + b<< endl;
else
cout << c + d << endl;
return 0;
}
●判断题
(1)将05行移到02 03之间,程序不会出错。
{{ select(9-1) }}
- 对
- 错
(2)将13行的“>”改为“>=”,输出不会发生改变。
{{ select(9-2) }}
- 对
- 错
(3)将10行改为b/=a;输出不会发生改变。
{{ select(9-3) }}
- 对
- 错
(4)输出只会有一行。
{{ select(9-4) }}
- 对
- 错
●选择题
(5)当输入为“9 19 29 39”,输出为( )。
{{ select(9-5) }}
- 21
- 22
- 23
- 24
(6)时间复杂度为( )。
{{ select(9-6) }}
第10题【NOIP】2014
#include<iostream>
using namespace std;
const int SIZE = 100;
int alive[SIZE];
int n;
int next( int num ){
do {
num++;
if ( num > n )num = 1;
}while ( alive[num] == 0 );
return num;
}
int main(){
int m, i, j, num;
cin>>n>>m;
for ( i = 1; i <= n; i++ )
alive[i] = 1;
num = 1;
for ( i = 1; i <= n; i++ ){
for ( j = 1; j < m; j++ )
num = next( num );
cout<<num<<" ";
alive[num] = 0;
if ( i < n )num = next( num );
}
cout<<endl;
return 0;
}
●判断题
(1)若输入100 0,该程序会运行错误。
{{ select(10-1) }}
- 对
- 错
(2)若把17行去掉,程序结果不会发生改变。
{{ select(10-2) }}
- 对
- 错
(3)如果把24行的“<”改为“<=”,则程序结果发生改变。
{{ select(10-3) }}
- 对
- 错
(4)若输入11 3
,则输出3 6 9 1 5 10 4 11 8 2 7
。
{{ select(10-4) }}
- 对
- 错
●选择题
(5)该程序的最坏时间复杂度为( )。
{{ select(10-5) }}
- O(1)
- O(n)
- O(n*m)
- O()
(6)若输入为10 4,则第4个数为( )。
{{ select(10-6) }}
- 5
- 6
- 7
- 8
第11题【NOIP】2017
#include <iostream>
using namespace std;
int main() {
int n, i, j, x, y, nx, ny;
int a[40][40];
for (i = 0; i < 40; i++)
for (j = 0; j < 40; j++)
a[i][j] = 0;
cin >> n;
y = 0; x = n - 1;
n = 2 * n - 1;
for (i = 1; i <= n * n; i++) {
a[y][x] = i;
ny = (y - 1 + n) % n;
nx = (x + 1) % n;
if ((y == 0 && x == n - 1) || a[ny][nx] != 0)
y = y + 1;
else { y = ny; x = nx; }
}
for (j = 0; j < n; j++)
cout << a[0][j] << " ";
cout << endl;
return 0;
}
●判断题
(1)11行改为n=(n<<1)-1;程序运行结果不变。
( )
{{ select(11-1) }}
- 对
- 错
(2)输出的数字有n(n是最初输入的数值)个。
( )
{{ select(11-2) }}
- 对
- 错
(3)将12行的i++改为++i程序运行结果不变。
( )
{{ select(11-3) }}
- 对
- 错
(4)将06~08行去掉,程序运行结果不变。
{{ select(11-4) }}
- 对
- 错
●选择题
(5)输入为3时,输出为( )。
{{ select(11-5) }}
- 17 24 1 8 15
- 17 21 3 5 2
- 1 5 36 7 5
- 17 2 8 3 1
(6)输入为4时,输出为( )。
{{ select(11-6) }}
- 30 39 48 1 10 19 28
- 24 39 48 1 10 19 28
- 30 39 48 1 5 19 14
- 30 39 24 1 10 19 28