C++信息学奥赛题目归类:初赛普及组阅读程序写结果题

NOIP阅读程序写结果题

初赛阅读程序写结果部分(共4题,每题8分,共计32分。)
注重培养学生的数学运算和归纳能力
这类题型主要是读程序写结果。
其中前两题为简单题,应该是必得分,第三题属于中等题,第四题稍微有点难度,但是对于学的很好的同学应该也么有问题。

做这种题的基本思路: 解决这类问题的关键在于能够分析程序的结构,清楚循环、条件判断的层次关系,充分结合学过的知识(四则运算和逻辑表达式),这类题是有一定综合能力的。

2007年NOIP初赛普及组 1 - 4

2008年NOIP初赛普及组 1 - 4

题目三 <中等>
#include 
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循环给数组a[ ] 赋值,然后数组和m都带入func的函数中,当循环条件满足时(i < j),本题的层级是两层,外面一层while循环包裹里面的while循环和if,注意ary[i++]和ary[++i]的区别。

题目四
#include
#include
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

考察

2009

题目一 <简单>
#include 
using namespace std;

int a,b;

int work(int a,int b){
	if (a%b)
		return work(b,a%b);
	return b;
}

int main(){
	cin >> a >> b;
	cout << work(a,b) << endl;
	return 0;
}

输入:20 12

输出:4

考察:考察递归调用,可参考2008年题目二

题目二 <简单>
#include 
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;
}

输入:2 3 5

输出:416

考察:数组和基本的四则运算,for循环

题目三 <简单>
#include 
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;
}

输入:11 2

输出:782

考察:基本四则运算,理清关系很简单,参考上一题。

题目四
#include 
using namespace std;

const int maxn=50;
void getnext(char str[])
{
	int l=strlen(str),i,j,k,temp;
	k=l-2;
	while(k>=0&&str[k]>str[k+1]) k--;
	i=k+1;
	while(i<l&&str[i]>str[k]) i++;
	temp=str[k];
	str[k]=str[i-1];
	str[i-1]=temp;
	for(i=l-1;i>k;i--)
		for(j=k+1;j<i;j++)
			if(str[j]>str[j+1])
			{
				temp=str[j];
				str[j]=str[j+1];
				str[j+1]=temp;
			}
	return ;
}

int main()
{
	char a[maxn];
	int n;
	cin >> a >> n;
	while(n>0)
	{
		getnext(a);
		n--;
	}
	cout << a << endl;
	return 0;
}

输入:NOIP 3

输出:NPOI

考察

2010

题目一
#include 
using namespace std;

void swap(int & a, int & b)
{
    int t;
    t = a;
    a = b;
    b = t;
}

int main()
{
    int a1, a2, a3, x;
        
    cin>>a1>>a2>>a3;
    if (a1 > a2)
        swap(a1, a2);
    if (a2 > a3)
        swap(a2, a3);
    if (a1 > a2)
        swap(a1, a2);
    
    cin>>x;
    if (x < a2)
        if (x < a1)
            cout<<x<<' '<<a1<<' '<<a2<<' '<<a3<<endl;
        else
            cout<<a1<<' '<<x<<' '<<a2<<' '<<a3<<endl;
    else
        if (x < a3)
            cout<<a1<<' '<<a2<<' '<<x<<' '<<a3<<endl;
        else
            cout<<a1<<' '<<a2<<' '<<a3<<' '<<x<<endl;    
    return 0;
}

输入:91 2 20 77

输出:2 20 77 91

考察

题目二
#include 
using namespace std;

int rSum(int j)
{
    int sum = 0;
    while (j != 0) {
        sum = sum * 10 + (j % 10);
        j = j / 10;
    }
    return sum;
}

int main()
{
    int n, m, i;
        
    cin>>n>>m;
    for (i = n; i < m; i++)
        if (i == rSum(i))
            cout<<i<<' ';
    return 0;
}

输入:90 120

输出: 99 101 111

考察

题目三
#include 
#include 
using namespace std;

int main()
{
    string s;
    char m1, m2;
    int i;
    
    getline(cin, s);
    m1 = ' ';
    m2 = ' ';
    for (i = 0; i < s.length(); i++)
        if (s[i] > m1) {
            m2 = m1;
            m1 = s[i];
        }
        else if (s[i] > m2)
            m2 = s[i];
    cout<<int(m1)<<' '<<int(m2)<<endl;
    return 0;
} 

输入:Expo 2010 Shanghai China

提示:
字符 空格 '0' 'A' 'a'
ASCII码 32 48 65 97

输出:120 112

考察

题目四
#include 
using namespace std;

const int NUM = 5;

int r(int n)
{
    int i;
    if (n <= NUM)
        return n;
    for (i = 1; i <= NUM; i++)
        if (r(n - i) < 0)
            return i;
    return -1;
}

int main()
{
    int n;
        
    cin>>n;
    cout<<r(n)<<endl;
    return 0;
}

输入:7
输出:1

输入:16
输出:4

考察

2011

题目一
#include
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;
}

输入:10 20

输出:165

考察

题目二
#include
#include
using namespace std;

int main()
{
    string map= "2223334445556667778889999";
    string tel;
    int i;
    cin>>tel;
    for(i=0;i<tel.length();i++)
       if((tel[i]>='0') && (tel[i]<='9') )
           cout<<tel[i];
       else if( (tel[i]>='A') && (tel[i]<='Z'))
           cout<<map[tel[i]-'A'];
    cout<<endl;
    return 0;
}

输入:CCF-NOIP-2011
输出:22366472011

考察

题目三
#include
#include
using namespace std;

const int SIZE = 100;

int main()
{
    int n,i,sum,x,a[SIZE];
    
    cin>>n;
    memset(a,0,sizeof(a));
    
    for(i=1;i<=n;i++){
        cin>>x;
        a[x]++;
    }
    i=0;
    sum=0;
    while(sum<(n/2+1)){
        i++;
        sum+=a[i];
    }
    cout<<i<<endl;
    return 0;
}

输入:
11
4 5 6 6 4 3 3 2 3 2 1

输出:3

考察

题目四
#include
using namespace std;

int solve(int n,int m)
{
    int i,sum;
    if(m==1) return 1;
    sum=0;
    for(i=1;i<n;i++)
       sum+= solve(i,m-1);
    return sum;
}

int main()
{
    int n,m;
    cin>>n>>m;
    cout<<solve(n,m)<<endl;
    return 0;
}

输入:7 4

输出:20

考察

2012

题目一
#include 
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 2 5

输出:10

考察

题目二
#include 
using namespace std;
int n,i,ans;
int main()
{
	cin>>n;
	ans=0;
	for(i=1;i<=n;i++)
		if(n%i==0) ans++;
	cout<<ans<<endl;
	return 0;	
}

输入:18

输出:6

考察

题目三
#include 
using namespace std;
int n,i,j,a[100][100];
int solve(int x,int y)
{
	int u,v;
	if(x==n) return a[x][y];
	u=solve(x+1,y);
	v=solve(x+1,y+1);
	if(u>v) return a[x][y]+u;
	else return a[x][y]+v;	
}
int main()
{
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=i;j++) cin>>a[i][j];
	cout<<solve(1,1)<<endl;
	return 0;	
}

输入:
5
2
-1 4
2 -1 -2
-1 6 4 0
3 2 -1 5 8

输出:14

考察

题目四
#include 
#include 
using namespace std;
int n,i,j,ans;
string s;
char get(int i)
{
	if(i<n) return s[i];
	else return s[i-n];	
}
int main()
{
	cin>>s;
	n=s.size();
	ans=0;
	for(i=1;i<=n-1;i++)
	{
		for(j=0;j<=n-1;j++)
			if(get(i+j)<get(ans+j))
			{
				ans=i;
				break;	
			}
			else if(get(i+j)>get(ans+j)) break;
	}
	for(j=0;j<=n-1;j++) cout<<get(ans+j);
	cout<<endl;
	return 0;	
}

输入:CBBADADA

输出:ACBBADAD

考察

2013

题目一
#include 
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	cout << a << "+" << b << "=" << a + b << endl;
}

输入: 3 5
输出:3+5=8

考察

题目二
#include  
using namespace std;
int main()
{
int a, b, u, i, num;
cin>>a>>b>>u; num = 0;
for (i = a; i <= b; i++) if ((i % u) == 0)
num++;
cout<<num<<endl; return 0;
}

输入: 1 100 15
输出:6
考察

题目三
#include  
using namespace std;
int main()
{
const int SIZE = 100;
int n, f, i, left, right, middle, a[SIZE];
cin>>n>>f;
for (i = 1; i <= n; i++)
cin>>a[i]; left = 1;
right = n; 
do {
middle = (left + right) / 2; 
if (f <= a[middle])
right = middle;
else
left = middle + 1; 
} while (left < right); 
cout<<left<<endl;
return 0;
}

输入:
12 17
2 4 6 9 11 15 17 18 19 20 21 25
输出: 7
考察

题目四
#include  
using namespace std;
int main()
{
const int SIZE = 100;
int height[SIZE], num[SIZE], n, ans;
cin>>n;
for (int i = 0; i < n; i++) 
{ 
cin>>height[i]; num[i] = 1;
for (int j = 0; j < i; j++) 
{
if ((height[j] < height[i]) && (num[j] >= num[i]))
num[i] = num[j]+1;
}
}
ans = 0;
for (int i = 0; i < n; i++)
 { 
 if (num[i] > ans) ans = num[i];
}
cout<<ans<<endl;
}

输入:
6
2 5 3 11 12 4
输出:4

考察

2014

题目一
#include 
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);
}
  

输入:2 3 4
输出:Ans = 9
考察

题目二
#include    
using namespace std;   
int fun(int n)    
{  
if(n == 1)        
return 1;      
if(n == 2)        
return 2;  
return fun(n -2) - fun(n - 1);    
}   
int main()    
{  
int n;      
cin >> n;  
cout << fun(n) << endl;     
 return 0;    
} 

输入:7
输出: -11
考察

题目三
#include 
#include 
using namespace std;
int main()
{
	string	st;
	int	i, len;
	getline( cin, st );
	len = st.size();
	for ( i = 0; i < len; i++ )
		if ( st[i] >= 'a' && st[i] <= 'z' )
			st[i] = st[i] - 'a' + 'A';
	cout << st << endl;
	return(0);
}

输入:Hello, my name is Lostmonkey.
输出:HELLO, MY NAME IS LOSTMONKEY.
考察

题目四
#include 
using namespace std;
const int SIZE = 100;
int main()
{
	int	p[SIZE];
	int	n, tot, i, cn;
	tot = 0;
	cin >> n;
	for ( i = 1; i <= n; i++ )
		p[i] = 1;
	for ( i = 2; i <= n; i++ )
	{
		if ( p[i] == 1 )
			tot++;
		cn = i * 2;
		while ( cn <= n )
		{
			p[cn] = 0;
			cn += i;
		}
	}
	cout << tot << endl;
	return(0);
}

输入:30
输出:10
考察

2015

题目一
#include
using namespace std;
int main() {
	
	int a,b,c;
	a=1;
	b=2;
	c=3;
	if (a > c){
		if(a>c) 
			cout << a <<" ";
		else 
			cout << b <<" ";
	}
	cout << c << endl;
	return 0;
}

输出:3

考察选择结构 if else的配对原则

题目二
#include 
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;
}

输出:3,2

考察结构体成员变量的变化

题目三
#include 
#include 
using namespace std; 
int main() {
	string str;
	int i;
	int count;
	count = 0;
	getline(cin,str);
	for (i = 0; i < str.length(); i++) {
		if(str[i] >= 'a' && str[i] <= 'z')
		count++;
	}
	cout << "It has " << count << " lowercases" << endl;
	return 0;
}

输入:NOI2016 will be held in Mian Yang.
输出:It has 18 lowercases

考察for循环遍历

题目四
#include 
using namespace std;
void fun(char *a, char *b) {
	a = b;
	(*a)++;
}
int main() {
	char cl, c2, *p1, *p2;
	cl = 'A';
	c2 = 'a'; 
	p1 = &cl;
	p2 = &c2;
	fun(p1, p2);
	cout << cl << c2 << endl;
	return 0;
}

输出:Ab

考察指针变化

2016

题目一
#include 
using namespace std;
int main() {
	int max, min, sum, count = 0;
	int tmp;
	cin >> tmp;
	if (tmp == 0)
		return 0;
	max = min = sum = tmp;
	count++;
	while (tmp != 0) {
		cin >> tmp;
		if (tmp != 0) { 
			sum += tmp;
			count++;
		if (tmp > max) 
			max = tmp; 
			if (tmp < min)
				min = tmp;
		}
	}
	cout << max << "," << min << "," << sum / count << endl;
	return 0;
}

输入:1 2 3 4 5 6 0 7
输出:6,1,3

考察

题目二
#include 
using namespace std;
int main() {
	int i = 100, x = 0, y = 0; 
	while (i > 0) {
		i--;
		x = i % 8;
		if (x == 1)
			y++;
	}
	cout << y << endl; 
	return 0;
}

输出:13

考察

题目三
#include 
using namespace std;
int main() {

	int a[6] = {1, 2, 3, 4, 5, 6};
	int pi = 0;
	int pj = 5;
	int t , i;
	while (pi < pj){
		t = a[pi];
		a[pi] = a[pj];
		a[pj] = t; pi++;
		pj--;
	}
	for (i = 0; i < 6; i++)
		cout << a[i] << ",";
	cout << endl;
	return 0;
}

输出:6,5,4,3,2,1

考察

题目四
#include  
using namespace std;
int main() {
	int i, length1, length2;
	string s1, s2;
	s1 = "I have a dream.";
	s2 = "I Have A Dream.";
	length1 = s1.size();
	length2 = s2.size();
	for (i = 0; i < length1; i++)
		if (s1[i] >= 'a' && s1[i] <= 'z') 
			s1[i] -= 'a' - 'A';
	for (i = 0; i < length2; i++)
		if (s2[i] >= 'a' && s2[i] <= 'z')
			s2[i] -= 'a' - 'A';
	if (s1 == s2)
		cout << "=" << endl; 
	else if (s1 > s2)
		cout << ">" << endl;
	else
		cout << "<" << endl; 
	return 0;
}

输出:=

考察

2017

题目一
#include 
using namespace std;
int main() { 

	int t[256];
	string s;
	int i;
	cin >> s;
for (i = 0; i < 256; i++)
	t[i] = 0;
for (i = 0; i < s.length(); i++) 
	t[s[i]]++;
for (i = 0; i < s.length(); i++)
	if (t[s[i]] == 1) {
		cout << s[i] << endl;
		return 0;
	}
	cout << "no" << endl; 
	return 0;
}

输入:xyzxyw
输出:3

考察

题目二
#include  
using namespace std;
int g(int m, int n, int x){
int ans = 0;
int i;
if (n == 1) return 1;
for (i = x; i <= m / n; i++) ans += g(m - i, n - 1, i);
return ans;
}

int main() {
	int t, m, n;
	cin >> m >> n;
	cout << g(m, n, 0) << endl;
	return 0;
}

输入:7 3
输出:3

考察

题目三
#include 
using namespace std;
int main() {

int a[6] = {1, 2, 3, 4, 5, 6};
int pi = 0;
int pj = 5;
int t , i;
while (pi < pj){
	t = a[pi];
	a[pi] = a[pj];
	a[pj] = t; pi++;
	pj--;
}
for (i = 0; i < 6; i++)
	cout << a[i] << ",";
cout << endl;
return 0;
}

输出:3
输出:3

考察

题目四
#include  
using namespace std;
int main() {
int i, length1, length2;
string s1, s2;
s1 = "I have a dream.";
s2 = "I Have A Dream.";
length1 = s1.size();
length2 = s2.size();
for (i = 0; i < length1; i++)
	if (s1[i] >= 'a' && s1[i] <= 'z') 
		s1[i] -= 'a' - 'A';
for (i = 0; i < length2; i++)
	if (s2[i] >= 'a' && s2[i] <= 'z')
		s2[i] -= 'a' - 'A';
if (s1 == s2)
	cout << "=" << endl; 
else if (s1 > s2)
	cout << ">" << endl;
else
	cout << "<" << endl; 
return 0;
}

输出:3

考察

2018

题目一
#include 
char st[100];

int main() {
	scanf("%s", st);
	for (int i = 0; st[i]; ++i) {
		if (‘A’ <= st[i] && st[i] <= ‘Z’)
		st[i] += 1;
	}
	printf("%s\n", st);
	return 0;
}

输入:QuanGuoLianSai

输出:RuanHuoMianTai

点评:字符串数组的循环,还有字符串中符合条件的ASCLL值的变化

题目二
#include 
int main() {
	int x;
    scanf("%d", &x);
    int res = 0;
    for (int i = 0; i < x; ++i) {
      	if (i * i % x == 1) {
      		++res;
      	}
    }
    printf("%d", res);
    return 0;
}

输入:15

输入:4

考察

题目三
#include 
using namespace std;
int n, m;

int findans(int n, int m) {
    if (n == 0) return m;
    if (m == 0) return n % 3;
    return findans(n - 1, m) - findans(n, m - 1) + findans(n - 1, m - 1);
}

int main(){
    cin >> n >> m;
    cout << findans(n, m) << endl;
    return 0;
}

输入:5 6
输出:

考察

题目四
#include 
int n, d[100];
bool v[100];

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", d + i);
        v[i] = false;
    }
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (!v[i]) {
        	for (int j = i; !v[j]; j = d[j]) {
        		v[j] = true;
        	}
        	++cnt;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

输入:10 7 1 4 3 2 5 9 8 0 6

考察

你可能感兴趣的:(C++信息学奥赛题目归类:初赛普及组阅读程序写结果题)