全部知识点
#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.
输出:( )
#include<iostream>
using namespace std;
int 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;
}输出:( )
(打印月历)输入月份 m(1≤m≤12),按一定格式打印 2015 第 m 月的月历。
例如,2015 年一月的月历打印效果如下(第一列为周日):

#include<iostream>
using namespace std;
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int m, offset, i;
int main()
{
cin >> m;
cout <<"S M T W T F S"<<endl;//' '为tab制表符
①;
for (i = 1; i < m; i++)
offset = ②;
for (i = 0; i < offset; i++)
cout <<' ';
for (i = 1; i <= ③;i++)
{
cout << ④;
if(i==dayNum[m]||⑤==0)
cout << endl;
else
cout << ' ';
}
return 0;
}(中位数)给定 n(n 为奇数且小于1000)个整数,整数的范围在0~m(0
#include <iostream>
using namespace std;
const int MAXN = 1000;
int n,i,lbound,rbound,mid,m,count;
int x[MAXN];
int main()
{
cin >> n >> m;
for(i = 0; i < n; i++)
cin >> x[i];
lbound = 0;rbound = m;
while(①) {
mid=(lbound+rbound)/2;
②;
for(i = 0; i < n; i++)
{
if(③)
④;
}
if(count > n/2)
lbound = mid + 1;
else
⑤;
}
cout << rbound << endl;
return 0;
}在 1 和 2015 之间(包括 1 和 2015 在内)不能被 4、5、6 三个数任意一个数整除的数有_____个。
结点数为 5 的不同形态的二叉树一共有_____种。(结点数为 2 的二叉树一共有 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;
}输出:( )
#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;
}输出:( )
#include <iostream>
using namespace std;
int main()
{
int len,maxlen;
string s,ss;
maxlen=0;
do
{
cin>>ss;
len=ss.length();
if(ss[0]=='#')break;
if(len>maxlen)
{
s=ss;
maxlen=len;
}
}while(true);
cout<<s<<endl;
return 0;
}输入:
I
am
a
citizen
of
China
#
输出:( )
#include <iostream>
using namespace std;
int fun(int n,int fromPos,int toPos)
{
int t,tot;
if(n==0)return 0;
for(t=1;t<=3;t++)
if(t!=fromPos && t != toPos)break;
tot=0;
tot+=fun(n-1,fromPos,t);
tot++;
tot+=fun(n-1,t,toPos);
return tot;
}
int main()
{
int n;
cin>>n;
cout<<fun(n,1,3)<<endl;
return 0;
}输入:5
输出:( )
(双子序列最大和)给定一个长度为n(3≤n≤1000) 的整数序列,要求从中选出两个连续子序列,使得这两个连续子序列的序列和之和最大,最终只需输出这个最大和。一个连续子序列的序列和为该连续子序列中所有数之和。要求:每个连续子序列长度至少为 1,且两个连续子序列之间至少间隔 1 个数。
#include <iostrea m>
using namespace std;
const int MAXN = 1000;
int n, i, ans, sum;
int x[MAXN];
int lmax[MAXN];
// lmax[i] 为仅含 x[i] 及 x[i] 左侧整数的连续子序列的序列和中,最大的序列和
int rmax[MAXN];
// rmax[i] 为仅含 x[i] 及 x[i] 右侧整数的连续子序列的序列和中,最大的序列和
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> x[i];
lmax[0] = x[0] ;
for (i = 1; i < n; i++)
if (lmax[i - 1] <= 0)
lmax[i] = x[i];
else
lmax[i] = lmax[i - 1] + x[i];
for (i = 1; i < n; i++)
if (lmax[i] < lmax[i - 1])
lmax[i] = lmax[i - 1];
①;
for (i = n - 2; i >= 0; i --)
if (rmax[i + 1] <= 0)
②;
else
③;
for (i = n - 2; i >= 0; i --)
if (rmax[i] < rmax[i + 1])
④;
ans = x[ 0] + x [2];
for (i = 1; i < n - 1; i++) {
sum = ⑤;
if (sum > ans)
ans = sum;
}
cout << ans << endl;
return 0;
}