北邮c语言编程题答案,北邮考研复试C语言上机题目精选

查找

题目描述:

输入数组长度 n

输入数组 a[1...n]

输入查找个数m

输入查找数字b[1...m]

输出 YES or NO 查找有则YES 否则NO 。

输入:

输入有多组数据。

每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m<=n<=100)。

输出:

如果在n个数组中输出YES否则输出NO。

样例输入:

5

1 5 2 4 3

3

2 5 6

样例输出:

YES

YES

NO

AC代码:

这道题没任何难度,就是两个for循环实现即可

#include

#include

int main()

{

int a[101], b[101];

int n, m, i, j, flag;

while(scanf("%d", &n) != EOF)

{

//接收输入数组

for(i = 0; i < n; i ++)

{

scanf("%d", a + i);

}

//接收查找数组

scanf("%d", &m);

for(j = 0; j < m; j ++)

{

scanf("%d", b + j);

}

//判断查找存在

for(j = 0; j < m; j ++)

{

flag = 0;

for(i = 0; i < n; i ++)

{

if(b[j] == a[i])

{

flag = 1;

break;

}

}

if(flag)

{

printf("YES\n");

}else

{

printf("NO\n");

}

}

}

return 0;

}

如果现在,我绝对会利用Java的HashMap去做这道题目

查找第K小数

题目描述:

查找一个数组的第K小的数,注意同样大小算一样大。

如  2 1 3 4 5 2 第三小数为3。

输入:

输入有多组数据。

每组输入n,然后输入n个整数(1<=n<=1000),再输入k。

输出:

输出第k小的整数。

样例输入:

6

2 1 3 5 2 2

3

样例输出:

3

AC代码:

考察的就是简单的快速排序,上我的AC代码

#include

#include

int partition(int *A, int left, int right);

void quicksort(int *A, int begin, int end);

int main()

{

int i, j, n, k;

int a[1001];

while(scanf("%d",&n) != EOF)

{

//接受stdin输入数据

for(i = 0; i < n; i ++)

{

scanf("%d",a + i);

}

scanf("%d",&k);

//快速排序

quicksort(a, 0, n - 1);

//输出第k小的数

for(i = 0, j = 0; i < n && j < k; i ++)

{

if(a[i] != a[i + 1])

{

if(j == k - 1)

{

printf("%d\n",a[i]);

break;

}else

{

j ++;

}

}

}

}

return 0;

}

void quicksort(int *A, int begin, int end)

{

int pivot;

if(begin < end)

{

pivot = partition(A, begin, end);

quicksort(A, begin, pivot - 1);

quicksort(A, pivot + 1, end);

}

}

int partition(int *A, int left, int right)

{

int stand = A[left];

while(left < right)

{

while(left < right && A[right] >= stand)

{

right --;

}

if(left < right)

{

A[left ++] = A[right];

}

while(left < right && A[left] <= stand)

{

left ++;

}

if(left < right)

{

A[right --] = A[left];

}

}

A[left] = stand;

return left;

}

打牌

题目要求:

题目描述:

牌只有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五个,后面的比前面的均大。

输入:

输入有多组数据。

每组输入两个字符串(字符串大小不超过100)a,b。a字符串代表手中牌,b字符串代表处的牌。

输出:

压过输出YES 否则NO。

样例输入:

12233445566677

33

样例输出:

YES

注意事项:

开始提交了3次,都是wa,发现有一种测试用例我无法通过,也是上个代码错误的地方,测试用例如下:

样例输入:1122335566778899(不连续)12345

样例输出:yes

AC代码:

#include

#include

#include

int main()

{

char a[101];

char b[101];

char ch, key;

int i, lena, lenb, flag;

int count[11];

while(scanf("%s",a) != EOF)

{

//接收回车符

ch = getchar();

//接收出牌

scanf("%s",b);

ch = getchar();

//长度

lena = strlen(a);

lenb = strlen(b);

//初始化

memset(count,0,sizeof(count));

//遍历手牌

for(i = 0; i < lena; i ++)

{

count[a[i] - '0'] ++;

}

//检测对方出牌

switch(lenb)

{

case 1:

case 2:

case 3:

case 4:

flag = 0;

for(key = b[0] - '0' + 1; key <= 9; key ++)

{

if(count[key] >= lenb)

{

flag = 1;

break;

}

}

break;

case 5:

flag = 0;

for(key = b[0] - '0' + 1; key < 9; key ++)

{

if(count[key] > 0 && count[key + 1] > 0 && count[key + 2] > 0 && count[key + 3] > 0 && count[key + 4] > 0)

{

flag = 1;

break;

}

}

break;

}

//打印输出

if(flag)

{

printf("YES\n");

}else

{

printf("NO\n");

}

}

return 0;

}

时间: 2015-08-14

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
期末作业要求如下: Write a program that will help elementary school pupils practice math. a) The program will first ask the user for his/her ID number (including two letters & 4 digits), e.g. Please input your four digit ID no: AB1234 The program should have input validation. Then the program prompts three choices: (1) Start a test (2) Check scores (3) Exit Test: the program will give 10 math problems, e.g.: 12 * 3 = 36 48 + 32 = 80 … 56 / 28 = 2 Note: i) Pupils will answer each problem before the next one is given. ii) The problems should include addition, subtraction, multiplication and division. They are randomly generated. iii) Randomly generates numbers for problems. However, must ensure that both the problem and the result are no larger than two digits. The problem and the result should be greater than or equal to zero. The divisor cannot be zero. iv) After ten problems are finished, record the time used by the student to do the ten problems. v) Gives a score to each student. Saves this student’s ID, his/her score and the time used into a file named ‘record.txt’. vi) Print the following information on the screen: Prob. | Correct Answ. | Ur Answ c) Check scores: Searches the file ‘record.txt’ and lists all the historical scores for this student, e.g.: Your previous records are: AB1234 80 150 seconds AB1234 50 182 seconds AB1234 90 98 seconds You will be marked based on your program’s: (1) Correctiveness (2) Readability (3) Robustness (4) Conciseness 目前本人的程序可以直接使用,并完全满足要求。还是建议学弟学妹在字里行间多多理解,争取自己写出漂亮的代码! 向本人在完成此作业过程中参考的代码表示衷心的感谢!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值