全部知识点
对于一个 1 到 n 的排列 P(即 1 到 n 中每一个数在 P 中出现了恰好一次),令 qi 为第 i 个位置之后第一个比 Pi 值更大的位置,如果不存在这样的位置,则 qi=n+1。举例来说,如果n=5 且 P 为 15423,则 q 为 2, 6, 6, 5, 6
下列程序读入了排列 P,使用双向链表求解了答案。试补全程序。
数据范围 1≤n≤105。
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int L[N], R[N], a[N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
① ;
}
for (int i = 1; i <= n; ++i) {
R[i] = ② ;
L[i] = i - 1;
}
for (int i = 1; i <= n; ++i) {
L[ ③ ] = L[a[i]];
R[L[a[i]]] = R[ ④ ];
}
for (int i = 1; i <= n; ++i) {
cout << ⑤ << " ";
}
cout << endl;
return 0;
}甲乙丙丁四人在考虑周末要不要外出郊游。
已知
①如果周末下雨,并且乙不去,则甲一定不去;
②如果乙去,则丁一定去;
③如果丙去,则丁一定不去;
④如果丁不去,而且甲不去,则丙一定不
去。
如果周末丙去了,则甲________(去了/没去),乙________(去了/没去),丁________(去了/没去),周末________(下雨/
没下雨)。
方程 a*b = (a or b) *(a and b),在 a,b 都取 [0, 31]中的整数时,共有_____组解。(*∗ 表示乘法;or 表示按位或运算;and 表示按位与运算)
#include <cstdio>
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
输出 :________
#include <cstdio>
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
", cnt);
return 0;
}输入 :
10 7 1 4 3 2 5 9 8 0 6
输出 :________
#include <iostream>
using namespace std;
string s;
long long magic(int l, int r){
long long ans = 0;
for (int i = l; i <= r; ++i){
ans = ans * 4 + s[i] - 'a' + 1;
}
return ans;
}
int main(){
cin >> s;
int len = s.length();
int ans = 0;
for (int l1 = 0; l1 < len; ++l1){
for (int r1 = l1; r1 < len; ++r1){
bool bo = true;
for (int l2 = 0; l2 < len; ++l2){
for (int r2 = l2; r2 < len; ++r2){
if (magic(l1, r1) == magic(l2, r2) && (l1 != l2 || r1 != r2)){
bo = false;
}
}
}
if (bo){
ans += 1;
}
}
}
cout << ans << endl;
return 0;
}输入 :
abacaba
输出 :________
#include <cstdio>
using namespace std;
const int N = 110;
bool isUse[N];
int n, t;
int a[N], b[N];
bool isSmall(){
for (int i = 1; i <= n; ++i)
if (a[i] != b[i]) return a[i] < b[i];
return false;
}
bool getPermutation(int pos){
if (pos > n){
return isSmall();
}
for (int i = 1; i <= n; ++i){
if (!isUse[i]){
b[pos] = i; isUse[i] = true;
if (getPermutation(pos + 1)){
return true;
}
isUse[i] = false;
}
}
return false;
}
void getNext(){
for (int i = 1; i <= n; ++i){
isUse[i] = false;
}
getPermutation(1);
for (int i = 1; i <= n; ++i){
a[i] = b[i];
}
}
int main(){
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
}
for (int i = 1; i <= t; ++i){
getNext();
}
for (int i = 1; i <= n; ++i){
printf("%d", a[i]);
if (i == n) putchar('
'); else putchar(' ');
}
return 0;
}输入1:
6 10 1 6 4 5 3 2
输出1:________
输入2:
6 200 1 5 3 4 2 6
输出2:________
对于一个 1 到 n 的排列 P(即 1 到 n 中每一个数在 P 中出现了恰好一次),令 qi 为第 i 个位置之后第一个比 Pi 值更大的位置,如果不存在这样的位置,则 qi=n+1。举例来说,如果 n=5 且 P 为 1 5 4 2 3,则 q 为2 6 6 5 6 。
下列程序读入了排列 P,使用双向链表求解了答案。试补全程序。
数据范围1≤n≤105。
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int L[N], R[N], a[N];
int main(){
cin >> n;
for (int i = 1; i <= n; ++i){
int x;
cin >> x;
____(1)____;
}
for (int i = 1; i <= n; ++i){
R[i] = ____(2)____;
L[i] = i - 1;
}
for (int i = 1; i <= n; ++i){
L[____(3)____] = L[a[i]];
R[L[a[i]]] = R[____(4)____];
}
for (int i = 1; i <= n; ++i){
cout << ____(5)____ << " ";
}
cout << endl;
return 0;
}一只小猪要买 N 件物品 (N 不超过 1000)。
它要买的所有物品在两家商店里都有卖。第 i 件物品在第一家商店的价格是 a[i],在第二家商店的价格是 b[i],两个价格都不小于 0 且不超过 10000。如果在第一家商店买的物品的总额不少于50000,那么在第一家店买的物品都可以打95 折(价格变为原来的 0.95 倍)。
求小猪买齐所有物品所需最少的总额。
输入:第一行一个数 N。接下来 N 行,每行两个数。第 i 行的两个数分别代表 a[i], b[i]。
输出:输出一行一个数,表示最少需要的总额,保留两位小数。
试补全程序。
#include <cstdio>
#include <algorithm>
using namespace std;
const int Inf = 1000000000;
const int threshold = 50000;
const int maxn = 1000;
int n, a[maxn], b[maxn];
bool put_a[maxn];
int total_a, total_b;
double ans;
int f[threshold];
int main() {
scanf("%d", &n);
total_a = total_b = 0;
for (int i = 0; i < n; ++i) {
scanf("%d%d", a + i, b + i);
if (a[i] <= b[i]) total_a += a[i];
else total_b += b[i];
}
ans = total_a + total_b;
total_a = total_b = 0;
for (int i = 0; i < n; ++i) {
if (____(1)____) {
put_a[i] = true;
total_a += a[i];
}
else{
put_a[i] = false;
total_b += b[i];
}
}
if (____(2)____) {
printf("%.2f", total_a * 0.95 + total_b);
return 0;
}
f[0] = 0;
for (int i = 1; i < threshold; ++i)
f[i] = Inf;
int total_b_prefix = 0;
for (int i = 0; i < n; ++i) {
if (!put_a[i]) {
total_b_prefix += b[i];
for (int j = threshold - 1; j >= 0; --j) {
if (____(3)____ >= threshold && f[j] != Inf)
ans = min(ans, (total_a + j + a[i]) * 0.95 + ____(4)____);
f[j] = min(f[j] + b[i], j >= a[i] ? ____(5)____ : Inf);
}
}
}
printf("%.2f", ans);
return 0;
}函数fun()的功能是:在有n个元素的结构体数组std中,查找有不及格科目的学生,找到后输出学生的学号;函数的返回值是有不及格科目的学生人数。例如,主函数中给出了4名学生的数据,则程序运行的结果为:
学号:N1002 学号:N1006
共有2位学生有不及格科目
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char num[8];
double score[2];
/**********found**********/
} __(1)__;
int fun(STU std[ ], int n)
{
int i,k=0;
for(i=0; i<n;i++)
{
/**********found**********/
if(std[i].score[0]<60 __(2)__ std[i].score[1]<60)
{
k++;
printf("学号:%s ",std[i].num);
}
}
/**********found**********/
return __(3)__;
}
main()
{
STU std[4]={"N1001",76.5,82.0,"N1002",53.5,73.0,
"N1005",80.5,66.0,"N1006",81.0,56.0};
printf("\n共有%d位学生有不及格科目\n", fun(std,4));
system("pause");
}函数fun()的功能是:判断整数n是否是“完数”。当一个数的因子之和恰好等于这个数本身时,就称这个数为“完数"。例如:6的因子包括1、2、3,而6=1+2+3,所以6是完数。如果是完数,函数返回值为1,否则函数返回值为0。
数组a中存放的是找到的因子,变量k中存放的是因子的个数。请改正函数fun中指定部位的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include <stdio.h>
#include <stdlib.h>
int fun(int n, int a[], int *k)
{
int m=0, i, t;
t=n;
for(i=1; i<n; i++)
{
if(n%i==0)
{
a[m]=i;
m++;
t=t-i;
}
}
/**********found**********/
k=m;
/**********found**********/
if(t=0)
{
return 1;
}
else
{
return 0;
}
}
main()
{
int n, a[10], flag, i, k;
printf("请输入一个整数:");
scanf("%d",&n);
flag=fun(n,a,&k);
if(flag)
{
printf("%d是完数,其因子是:",n);
for(i=0;i<k;i++)
{
printf(" %d ",a[i]);
}
printf("\n");
}
else
{
printf("%d不是完数\n",n);
}
system("pause");
}请编写函数fun(),其功能是:在形参指针所指的4个整数中找出最大值和最小值,最大的放在a中,最小的放在d中。
注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括中的标号处填入所编写的若干语句。
#include <stdio.h>
#include <stdlib.h>
void NONO();
void fun(int *a,int *b,int *c,int *d)
{
int t;
if(1)
{
2;3;4;
}
if(5)
{
6;7;8;
}
if(9)
{
10;11;12;
}
if(13)
{
14;15;16;
}
if(17)
{
18;19;20;
}
if(21)
{
22;23;24;
}
}
main()
{
int a, b, c, d;
printf("请输入4个整数:");
scanf("%d %d %d %d",&a,&b,&c,&d);
printf("原始顺序:%d,%d,%d,%d\n",a,b,c,d);
fun(&a,&b,&c,&d);
printf("处理后顺序:%d,%d,%d,%d\n",a,b,c,d);
NONO();
}
void NONO()
{/*本函数用于打开文件,输入数据,
调用函数,输出数据,关闭文件。*/
FILE *rf,*wf;
int i, a, b, c, d;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0; i<8; i++)
{
fscanf(rf,"%d %d %d %d",&a,&b,&c,&d);
fun(&a,&b,&c,&d);
fprintf(wf,"a=%d,d=%d\n",a,d);
}
fclose(rf);
fclose(wf);
system("pause");
}函数fun的功能是:根据所给的年、月、日,计算出该日是这一年的第几天,并作为函数值返回。其中函数isleap用来判别某一年是否为闰年。
例如,若输入:200851,则程序输出:2008年5月1日是该年的第122天。
请在程序的下划线处填入正确的内容
#include <stdio.h>
#include <stdlib.h>
int isleap(int year)
{
int leap;
leap= (year%4==0 && year%100!=0 || year%400==0);
/**********found**********/
return __(1)__;
}
int fun(int year, int month, int day)
{
int table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int days=0,i;
for(i=1; i<month; i++)
{
days=days + table[i];
}
/**********found**********/
days=days+__(2)__;
if(isleap(year) && month>2)
{
/**********found**********/
days=days+__(3)__;
}
return days;
}
main()
{
int year, month, day,days;
printf("请输入年、月、日:");
scanf("%d%d%d", &year, &month, &day);
days = fun(year, month, day);
printf("%d年%d月%d日是该年的第%d天\n", year, month, day, days);
system("pause");
}函数fun的功能是:在有n名学生,2门课成绩的结构体数组std中,计算出第1门课程的平均分,作为函数值返回。
例如,主函数中给出了4名学生的数据,则程序运行的结果为:第1门课程的平均分是:76.125000。
请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char num[8];
double score[2];
} STU;
double fun(STU std[],int n)
{
int i;
/**********found**********/
double sum;
for(i=0; i<n; i++)
{
/**********found**********/
sum += std[i].score[1];
}
return sum/n;
}
main()
{
STU std[]={"N1001",76.5,82.0 ,"N1002",66.5,73.0,
"N1005",80.5,66.0,"N1006",81.0,56.0};
printf("第1门课程的平均分是:%lf\n", fun(std,4));
system("pause");
}请编写函数proc,其功能是:判断形参n中的正整数是几位数(输入数据的位数不超过4位),并将结果通过函数值返回。
例如:若输入的数据为123,则输出结果为:输入的数字是3位。
#include <stdio.h>
#include <stdlib.h>
void NONO();
int proc(int n)
{
int t=0;
if(①)
{
②;
}
else if(③)
{
④;
}
else if(⑤)
{
⑥;
}
else
{
⑦;
}
return t;
}
main()
{
int n, place;
do
{
printf("请输入一个4位以内的正整数:");
scanf("%d", &n);
}
while (n<0 || n>9999);
place=proc(n);
printf("输入的数字是%d位\n", place);
NONO();
system("pause");
}
void NONO()
{/* 本函数用于打开文件,输入数据,
调用函数,输出数据,关闭文件。*/
FILE *rf,*wf;
int i, n, place;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for( i=0; i<8; i++ )
{
fscanf(rf, "%d ", &n);
place=proc(n);
fprintf(wf, "%d\n", place);
}
fclose(rf);
fclose(wf);
}给定程序中,函数fun的功能是:不断从终端读入整数,由变量a统计大于0的个数,用变量c来统计小于0的个数,当输入O时结束输入,并通过形参pa和pb把统计的数据传回主函数进行输出。
#include <stdio.h>
#include <stdlib.h>
void fun(int *px,int *py)
{
/**********found**********/
int __(1)__;
scanf("%d",&k);
/**********found**********/
while __(2)__
{
if(k>0)
{
a++;
}
if(k<0)
{
c++;
}
/**********found**********/
__(3)__;
}
*px=a;
*py=c;
}
main()
{
int x, y;
fun(&x,&y);
printf("x=%d y=%d\n", x, y);
system("pause");
}将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun的作用是:累加链表结点数据域中的数据作为函数值返回。
请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *next;
} LIST;
int fun(LIST *h)
{
LIST *p;
int t=0;
p=h;
/**********found**********/
while(*p)
{
/**********found**********/
t=t+p.data;
p=(*p).next;
}
return t;
}
main()
{
LIST a,b,c,*h;
a.data=34;
b.data=51;
c.data=87;
c.next='\0';
h=&a;
a.next=&b;
b.next=&c;
printf("总和 = %d\n",fun(h));
system("pause");
}请编写函数fun,其功能是分别统计形参t所指二维数组中字母A和C的个数。
#include <stdio.h>
#include <stdlib.h>
#define M 14
void fun(char (*t)[M],int *a,int *c)
{
int i,j;
*a=0;*c=0;
for(①)
{
for(②)
{
if(③)
{
④;
}
else if(⑤)
{
⑥;
}
}
}
}
void get(char (*s)[M])
{
int i, j;
for(i=0; i<M; i++)
{
for(j=0; j<M; j++)
{
s[i][j]=65+rand()%12;
printf("%c", s[i][j]);
}
printf("\n");
}
}
main()
{
void NONO();
char a[M][M];
int x,y;
get(a);
fun(a, &x, &y);
printf("A的个数为%d C的个数为 %d\n", x, y);
NONO();
}
void NONO()
{/* 本函数用于打开文件,输入数据,调用函数,
输出数据,关闭文件。 */
FILE *rf, *wf;
int i, j, x, y;
char a[M][M];
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for( i=0; i<M; i++ )
{
for( j=0; j<M; j++)
{
fscanf(rf,"%c", &a[i][j]);
}
}
fun (a, &x, &y);
fprintf(wf, "A=%d\n", x);
fprintf(wf, "C=%d\n", y);
fclose(rf);
fclose(wf);
system("pause");
}给定程序中,函数fun的功能是建立一个NxN的矩阵。矩阵元素的构成规律是:最外层元素的值全部为1;从外向内第2层元素的值全部为2;第3层元素的值全部为3,.…依次类推。
例如,若N=5,生成的矩阵为:
11111
12221
12321
12221
11111
#include <stdio.h>
#include <stdlib.h>
#define N 5
/**********found**********/
void fun(int (*a)__(1)__)
{
int i, j, k, m;
if(N%2==0)
{
m=N/2 ;
}
else
{
m=N/2+1;
}
for(i=0; i<m; i++)
{
/**********found**********/
for(j=__(2)_ ; j<N-i; j++)
{
a[i][j]=a[N-i-1][j]=i+1;
}
for(k=i+1; k<N-i; k++)
{
/**********found**********/
a[k][i]=a[k][N-i-1]=__(3)__;
}
}
}
main()
{
int x[N][N]={0},i,j;
fun(x);
printf("\nThe result is:\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",x[i][j]);
}
printf("\n");
}
system("pause");
}函数fun的功能是:将十进制正整数m转换成k(2sks9)进制数,并按高位到低位顺序输出。
例如,若输入8和2,则应输出1000(即+进制数8转换成二进制表示是1000)。
请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
#include <stdlib.h>
void fun(int m, int k);
{
int aa[20], i;
for(i = 0; m; i++)
{
/**********found**********/
aa[i] = m/k;
m /= k;
}
for(; i; i--)
{
/**********found**********/
printf("%d",aa[i]);
}
}
main()
{
int b, n;
printf("\nPlease enter a number and a base:\n");
scanf("%d %d", &n, &b);
fun(n, b);
printf("\n");
system("pause");
}