工作之余 编了编复试题 都是用很简单的思路编的 高手请指教
第一题:查找
输入数组长度 n
输入数组 a[1...n]
输入查找个数m
输入查找数字b[1...m]
输出 YES or NO 查找有则YES 否则NO
如(括号内容为注释)
输入:
5(数组长度)
1 5 2 4 3(数组)
3(查找个数)
2 5 6(查找具体数字)
输出:
YES
YES
NO
#include<stdio.h>
#include<malloc.h>
void main()
{
int * a;
int * b;
int n,m;
int i;
printf("please input vector length");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("please input the %d value",i);
scanf("%d",&a[i]);
}
printf("please input search length");
scanf("%d",&m);
b=(int *)malloc(m*sizeof(int));
for(int j=0;j<m;j++)
{
printf("please input di %d ge yao zhao de",j);
scanf("%d",&b[j]);
for(i=0;i<n;i++)
{
if (a[i]==b[j])
{
break;
}
}
if (i==n)
{
printf("no\n");
}
else printf("YES\n");
}
}
第二题:查找第K小数
查找一个数组的第K小的数,注意同样大小算一样大
如 2 1 3 4 5 2 第三小数为3
如(括号内容为注释)
输入:
6(数组长度n)
2 1 3 5 2 2(数组)
3(K 即为第三小数)
输出:
3
Code
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
int main()
{
int *a;
int i,n,k,min;
char c;
printf("please input vector length");
scanf("%d",&n);
a=(int *)malloc((n+1)*sizeof(int));
for(i=1;i<=n;i++)
{
printf("please input di %d ge shu",i);
scanf("%d",&a[i]);
}
printf("please input k");
scanf("%d",&k);
if (k>n)
return -1;
for(int j=1;j<=k;j++)
for(i=j;i<=n;i++)
{
min=j;
if (a[i]<a[min])
{
min=i;
}
}
printf("%d",a[min]);
system("pause");
}
第三题:打牌
牌只有1到9,手里拿着已经排好序的牌a,对方出牌b,用程序判断手中牌是否能够压过对方出牌
规则:出牌牌型有5种
[1]一张 如4 则5...9可压过
[2]两张 如44 则55,66,77,...,99可压过
[3]三张 如444 规则如[2]
[4]四张 如4444 规则如[2]
[5]五张 牌型只有12345 23456 34567 45678 56789五个,后面的比前面的均大
压过输出YES 否则NO
如(括号内容为注释)
输入:
12233445566677(手中牌)
33(出牌)
输出:
YES
}
第四题:树 查找
题目描述:
有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY。该树是完全二叉树。
输入:
输入有多组数据。
每组输入一个n(1<=n<=1000),然后将树中的这n个节点依次输入,再输入一个d代表深度。
输出:
输出该树中第d层得所有节点,节点间用空格隔开,最后一个节点后没有空格。
样例输入:
4
1 2 3 4
2
样例输出:
2 3
code
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include <malloc.h>
#include <math.h>
using namespace std;
void main()
{
int * *a;
int m,count=0,d,mult=1;
char b;
a=(int* *)malloc(10*sizeof(int *));
do
{
printf("please input data length");
cin>>m;
*(a+count)=(int *)malloc(m*sizeof(int));
for(int i=0;i<m;i++)
{
scanf("%d",&((*(a+count))[i]));
}
cout<<"input high ";
cin>>d;
mult=pow(2.0,d-1);
// for (int i=0;i<d-1;i++)
// {
// mult*=2;
// }
if (mult-1>m)
{
cout<<"EMPTY"<<endl;
}
for (int i=mult-1;(i<m)&&(i<=(mult-1)*2);i++)
{
cout<<(*(a+count))[i]<<endl;
}
count++;
cout<<"if you want to input again";
cin>>b;
} while (b=='y');
system("pause");
}
//// malloc之后都么有加free。