NOIP真题
如图所示,图中每条边上的数字表示该边的长度,则从 A 到 E 的最短距离是______。

#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
输出:____
#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
输出:____
#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.
输出:____
#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
输出:____
(数字删除)下面程序的功能是将字符串中的数字字符删除后输出。请填空。
#include <iostream>
using namespace std;
int delnum(char *s)
{
int i, j;
j = 0;
for(i = 0; s[i] != '\0'; i++)
if(s[i] < '0' ① s[i] > '9')
{
s[j] = s[i];
②;
}
return ③;
}
const int SIZE = 30;
int main()
{
char s[SIZE];
int len, i;
cin.getline(s, sizeof(s));
len = delnum(s);
for(i = 0; i < len; i++)
cout << ④;
cout << endl;
return 0;
}(最大子矩阵和)给出 m 行 n 列的整数矩阵,求最大的子矩阵和(子矩阵不能为空)。
输入第一行包含两个整数 m 和 n,即矩阵的行数和列数。之后 m 行,每行 n 个整数,描述整个矩阵。程序最终输出最大的子矩阵和。
#include <iostream>
using namespace std;
const int SIZE = 100;
int matrix[SIZE + 1][SIZE + 1];
int rowsum[SIZE + 1][SIZE + 1]; //rowsum[i][j]记录第 i 行前 j 个数的和
int m, n, i, j, first, last, area, ans;
int main()
{
cin >> m >> n;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
cin >> matrix[i][j];
ans = matrix ①;
for(i = 1; i <= m; i++)
②
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
rowsum[i][j] = ③;
for(first = 1; first <= n; first++)
for(last = first; last <= n; last++)
{
④;
for(i = 1; i <= m; i++)
{
area += ⑤;
if(area > ans)
ans = area;
if(area < 0)
area = 0;
}
}
cout << ans << endl;
return 0;由数字 1,1,2,4,8,8 所组成的不同的四位数的个数是 _____.
如图所示,图中每条边上的数字表示该边的长度,则从 A 到 E 的最短距离是 _____.

#include<iostream>
using namespace std;
int main(){
int a, b, i, tot, c1, c2;
cin >> a >> b;
tot = 0;
for (i = a; i <= b; i++){
c1 = i / 10;
c2 = i % 10;
if((c1 + c2) % 3 == 0)
tot++;
}
cout << tot << endl;
}输入:
7 31
输出:( )
#include <iostream>
using namespace std;
int fun(int n, int minNum, int maxNum) {
int tot, i;
if(n == 0)
return 1;
tot = 0;
for (i = minNum; i <= maxNum; i++){
tot += fun(n - 1, i + 1, maxNum);
}
return tot;
}
int main(){
int n,m;
cin >> n >> m;
cout << fun(m, 1, n) << endl;
return 0;
}输入:
6 3
输出:( )
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 100;
int main(){
string dict[SIZE];
int rank[SIZE];
int ind[SIZE];
int i, j, n, tmp;
cin >> n;
for (i = 1; i <= n; i++){
rank[i] = i;
ind[i] = i;
cin >> dict[i];
}
for(i = 1; i < n; i++){
for(j = 1; j <= n - i; j++){
if(dict[ind[j]] > dict[ind[j + 1]]){
tmp = ind[j];
ind[j] = ind[j + 1];
ind[j + 1] = tmp;
}
}
}
for(i = 1; i <= n; i++)
rank[ind[i]] = i;
for(i = 1; i <= n; i++)
cout << rank[i] << " ";
cout << endl;
return 0;
}输入:
7
aaa
aba
bbb
aaa
aaa
ccc
aa
输出:( )
#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;
}输入:
11 3
输出:( )
(双栈模拟数组) 只使用两个栈结构 stack1 和 stack2,模拟对数组的随机读取。作为栈 结构, stack1 和 stack2 只能访问栈顶 (最后一个有效元素) 。栈顶指针 top1 和 top2 均指向栈 顶元素的下一个位置。 输入第一行包含的两个整数,分别是数组长度 n 和访问次数 m,中间用单个空格隔开。 第二行包含 n 个整数,一次歌出数组各项(数组下标从 0 到 a-1)。第三行包含 m 个整数, 需要访问的数组下标。对于每次访问,输出对应的数组元素。
#include <iostream>
using namespace std;
const int SIZE = 100;
int stack1[SIZE], stack2[SIZE];
int top1, top2;
int n, m, i, j;
void clearStack(){
int i;
for(i = top1; i < SIZE; i++)
stack1[i] = 0;
for(i = top2; i < SIZE; i++)
stack2[i] = 0;
}
int main(){
cin >> n >>m;
for(i = 0; i < n; i++)
cin >> stack1[i];
top1 = ①;
top2 = ②;
for(j = 0; j < m; j++){
cin >> i;
while (i < top1 - 1){
top1--;
③;
top2++;
}
while(i > top1 - 1){
top2--;
④;
top1++;
}
clearStack();
cout << stack1[⑤] << endl;
}
return 0;
}(最大矩阵和 )给出 M 行 N 列的整数矩阵,就最大的子矩阵和(子矩阵不能为空) 。 输入第一行包含两个整数 M 和 N, 即矩阵的行数和列数。之后 M 行,每行 N 个整数,描述 整个矩阵。程序最终输出最大的子矩阵和。
#include <iostream>
using namespace std;
const int SIZE = 100;
int matrix[SIZE + 1][SIZE + 1];
int rowsum[SIZE + 1][SIZE + 1];
int m, n, i, j, first, last, area, ans;
int main(){
cin >> m >>n;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
cin >> matrix[i][j];
ans = matrix ①;
for(i = 1; i <= m; i++)
②;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
rowsum[i][j] = ③;
for(first = 1; first <= n; first++)
for(last = first; last <= n; last++){
④;
for(i = 1;l i <= m; i++){
area += ⑤ ;
if(area > ans)
ans = area;
if(area < 0)
area = 0;
}
}
cout << ans << endl;
return 0;
}重新排列 1234 使得每一个数字都不在原来的位置上,一共有( )种排法。
一棵结点数为 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;
}输出:( )
#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;
}输出:( )
#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.
输出:( )