保证正确率60%以上
NOIP 2016(第二十二届)
1、//考察基本的while循环,求最大和最小值
#include <iostream>
using namespace std;
int main(){
int max,min,sum,count=0;
int 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
输出:_____________________
2、//while循环,求100以内模8余1的数的个数
#include <iostream>
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;
}
输出:_____________
3、//首尾交换数组内容
#include <iostream>
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;
}
输出: _____________
4、//简单的字符串操作
#include <iostream>
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;
}
输出:______________
NOIP 2015(第二十一届)
1、{注:考察简单的if结构}
#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;
}
输出:_________
2、{注:对记录结构熟悉程序的考察}
#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;
}
输出:_________________
3、{注:统计字符串中小写字符的个数}
#include <iostream>
#include <string>
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
输出:_I________________
4、{注:对指针概念的考察}
#include <iostream>
using namespace std;
void fun(char *a,char *b){
a = b;
(*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;
}
输出:___________________
NOIP 2014(第二十届)
1、{注:基本运算能力的考察}
#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;
}
输入:2 3 4
输出:_ _________
2、{对递归函数理解的考察}
#include <iostream>
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
输出:_______________
3、{注:考察对字符的操作,ord和chr函数的理解}
#include <iostream>
#include <string>
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.
输出:_ ___________________
4、{注:本题的实质是用筛选法求n以内的素数的个数}
#include <iostream>
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
输出:_________________
NOIP 2013(第十九届)
1. {注:送分题}
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a<<"+"<<b<<"="<<a+b<<endl;
}
输入:3 5
输出: ____________
2. {注:循环加条件判断的题目}
#include <iostream>
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
输出:___________
NOIP 2012(第十八届)
1、{注:送命题}
#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 2 5
输出:________
2、{注:考察循环加条件判断}
#include <iostream>
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
输出:_________
3、{注:递归过程的理解}
#include <iostream>
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]; //递归终止项!若x=5,也即为第5行,那么直接返回二维数组中对应的a[x][y]值
u=solve(x+1,y);
v=solve(x+1,y+1);
if(u>v) return a[x][y]+u;//比较solve[x+1][y]和solve[x+1][y+1]的大小,取大的和a[x,y]相加
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
输出:_______________
{注:从下往上算出solve(x,y)的值,第5行和a的第5行一样}
14
9 12
10 8 7
2 8 9 8
3 2 -1 5 8
4、{注:for循环和自定义函数的手工模拟操作}
#include <iostream>
#include <string>
using namespace std;
int n,i,j,ans;
string s;
char get(int i)//返回字符串s中第i个字符,如果i大于字符串长度,那么返回第i-n个字符
{
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))// 如果当前i+j对应的字符小于ans+j对应的字符,那么更新ans为i,并且退出内层循环,开始判断下一个i+1值
{
ans=i;
break;
}
else if(get(i+j)>get(ans+j)) break;// 如果当前i+j对应的字符大于ans+j对应的字符,那么退出内层循环,开始判断下一个i+1值
}
for(j=0;j<=n-1;j++) cout<<get(ans+j);
cout<<endl;
return 0;
}
输入:CBBADADA
输出: ____________
NOIP 2008(第十四届)
1.{注:考察基本功和细心,整数整除和整数求余%的使用}
#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
输出:__________________________
2.{注:送分题,考察基本的递归调用,注意递归过程中传递的参数值}
#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
输出:_________________________
3.{注:本题考查while的熟练掌握程度以及数组内容变化的人工模拟正确性}
#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++;//从左开始找到第一个负数,i为该负数的位置
while (i<j&&ary[j]<0) j--;//从右开始找到第一个整数,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
输出:__________________________________________
4.{注:本题的实质是给定前序遍历序列和中序编列序列,求树的后续遍历,从first、mid中可以大胆猜测!}
#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
输出:___ _____________________________
NOIP 2007(第十三届)
1、{注:本题考查细心、运算符号的优先级}
#include <iostream.h>
void main()
{int i,p[5],a,b,c,x,y=20;
for(i=0;i<=4;i++) cin>>p[i];
a=(p[0]+p[1])+(p[2]+p[3]+p[4])/7;
b=p[0]+p[1]/((p[2]+p[3])/p[4]);
c=p[0]*p[1]/p[2];
x=a+b-p[(p[3]+3)%4];
if(x>10)
y+= (b*100-a)/(p[p[4]%3]*5);
else
y+=20+(b*100-c)/(p[p[4]%3]*5);
cout<<x<<","<<y<<endl;
}
// 注:本例中,给定的输入数据可以避免分母为0或数组元素下标越界。
输入:6 6 5 5 3
输出: ______________________
2、{注:本题考查对指针的理解和过程参数类型的理解}
#include <iostream.h>
void fun(int *a,int *b)
{int *k;
k=a; a=b; b=k;
}
void main( )
{int a=3, b=6, *x=&a, *y=&b;
fun(x,y);
cout<<a<<","<<b<<endl;
}
输出: ______________________________
3、{注:本题的本质是用筛选法求50以内的素数!}
#include <iostream.h>
#include <iomanip.h>
#include "math.h"
void main()
{int a1[51]={0};
int i,j,t,t2,n=50;
for (i=2;i<=sqrt(n);i++)
if(a1[i]==0)
{t2=n/i;
for(j=2;j<=t2;j++) a1[i*j]=1;
}
t=0;
for (i=2;i<=n;i++)
if(a1[i]==0)
{cout<<setw(4)<<i; t++;
if(t%10==0) cout<<endl;
}
cout<<endl;
}
输出:
________________________________________
4、{注:本题考查综合能力,包括while循环、字符比较等}
#include <iostream.h>
#include "ctype.h"
void expand(char s1[],char s2[])
{ int i,j,a,b,c;
j=0;
for(i=0;(c=s1[i])!='\0';i++)
if(c=='-')
{ a=s1[i-1]; b=s1[i+1];
if ( isalpha(a)&&isalpha(b) || isdigit(a)&&isdigit(b) )
//函数isalpha(a)用于判断字符a是否为字母,isdigit(b) 用于判断字符b是否为数
//字,如果是,返回1,否则返回0
{ j--;
do{
s2[j++]=a++;
}while(tolower(a)<tolower(s1[i+1]));
}
else
s2[j++]=c;
}
else s2[j++]=c;
s2[j]='\0';
}
void main()
{ char s1[100],s2[300];
cin>>s1;
expand(s1,s2);
cout<<s2<<endl;
}
输入:wer2345d-h454-82qqq
输出: _________________________