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;
}
输入:9 19 29 39
输出:23
解析:
主要考察数组、for循环、if条件判断
本题中的for循环是为了给数组f[4]赋值,赋值为键盘输入9 19 29 39 ,
通过四则运算求出a,b,c,x的值
a的值是 a = (9+19+29+39)/9 = 10
b的值是 b = (9+29+39)/10 = 7
c的值是 c =(7 * 19 + 10)/29 = 4
d的值是 d =f((7 /2)%4) = f(3) = 19
此时进行条件判断,f[(a + b + c + d) % 4] = f[40%4] = f[0] = 9 < f[2] = 29
最后输出的是else下的语句,c + d = 23
2008题目二 <简单>
#include<iostream>
using namespace std;
void foo(int a, int b, int c)
{
if(a > b)
foo(c, a, b);
else
cout<<a<<','<<b<<','<<c<<endl;
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
foo(a, b, c);
return 0;
}
输入: 3 1 2
解析:
主要考察函数的递归调用
foo(3,1,2)中
第一次调用 a > b 执行 foo(2,3,1)
第二次调用 a < b 输出 2,3,1
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];
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;
}
输入:5 4 -6 -11 6 -59 22 -6 1 10
输出: 5 4 10 1 6 22 -59 -6 -11 -6
解析:
主要考察数组、函数调用、for循环、if条件判断、while循环
本题中首先利用for循环给数组a[ ] 赋值,然后数组和m都带入func的函数中,当循环条件满足时(i < j),本题的层级是两层,外面一层while循环包裹里面的while循环和if,注意ary[i++]和ary[++i]的区别。
2008题目四 <中等>
#include<iostream>
#include<cstring>
using namespace std;
#define MAX 100
void solve(char first[], int spos_f, int epos_f, char mid[], int spos_m, int epos_m)
{
int i, root_m;
if(spos_f > epos_f)
return;
for(i = spos_m; i <= epos_m; i++)
if(first[spos_f] == mid[i])
{
root_m = i;
break;
}
solve(first, spos_f + 1, spos_f + (root_m - spos_m), mid, spos_m, root_m - 1);
solve(first, spos_f + (root_m - spos_m) + 1, epos_f, mid, root_m + 1, epos_m);
cout << first[spos_f];
}
int main()
{
char first[MAX], mid[MAX];
int len;
cin >> len;
cin >> first >> mid;
solve(first, 0, len - 1, mid , 0, len - 1);
cout << endl;
return 0;
}
输入:7
ABDCEGF
BDAGECF
输出:DBGEFCA
考察: