Postgraduate review machine questions (2009)

Source: Internet
Author: User
Tags return tag




Problem A: write a program that provides all the final numbers in the specified integer range [a, B]. If a number is equal to the sum of all factors except itself, this number is called the end number. For example, 6 is the end number because 6 is 1 + 2 + 3.

Input Description: a group of data is composed of two positive integers, A and B (1 <A <B <10 ^ 5 ).

Output Description: specifies the number of all finishes in the range. Each number occupies one row.

Input sample

1 100

Output sample

6

28


Answer:

/** Description: machine question a answer * Author: Zhang yachao * blog: dunny column http://blog.csdn.net/u012027907 * Date: */# include "stdio. H "int main () {int A, B; int sum = 0; scanf (" % d ", & A, & B); For (INT num =; num <= B; num ++) {sum = num; For (INT I = 1; I <num/2 + 1; I ++) {// process each number if (Num % I = 0) {// sum-= I can be divisible by I; // subtract a factor }}if (sum = 0) {printf ("% d \ n", num); // number of outputs }}return 0 ;}







Problem B: write a program. For the square matrix of a column (1 <m <10) in m rows, calculate the sum of each row, column, and main diagonal element, finally, output the data in ascending order.

Input Description: there is a set of data. The first input behavior is a positive integer, indicating m, and the next m rows. Each row has an integer representing a matrix element.

Output Description: a line of integers arranged in ascending order. Each integer is followed by a space and the last line is wrapped.

Input sample:

4

15 8-26

31 24 18 71

-3-9 27 13

17 21 38 69

Output sample:

159 145 144 13581 60 44 32 2827


Answer:

# Include "stdio. H "# define max 25 void sort (INT store [], int count) {// bubble sort for (INT I = 0; I <count; I ++) for (Int J = I + 1; j <count; j ++) {If (store [I] <store [J]) {int temp = store [I]; store [I] = store [J]; store [J] = temp ;}} void print (INT store [], int count) {// print the output for (INT I = 0; I <count; I ++) printf ("% d", store [I]); printf ("\ n") ;}int main () {int m; int st [10] [10], Res [Max]; int COUNT = 0; scanf ("% d", & M); // counts the sum of each row and for (INT I = 0; I <m; I ++) {int sum = 0; for (Int J = 0; j <m; j ++) {scanf ("% d", & St [I] [J]); sum + = sT [I] [J];} res [count ++] = sum;} // calculate the sum of each column and for (I = 0; I <m; I ++) {int sum = 0; For (Int J = 0; j <m; j ++) {sum + = sT [J] [I];} res [count ++] = sum;} int sum1, sum2; sum1 = sum2 = 0; for (I = 0; I <m; I ++) {// calculate the sum of the diagonal lines of the primary pair and sum1 + = sT [I] [I]; sum2 + = sT [I] [m-i-1];} res [count ++] = sum1; Res [count ++] = sum2; sort (Res, count); print (Res, count); Return 0 ;}


Problem C: for a given Character Sequence, extract all numeric characters from left to right and splice them into an unsigned integer (the sequence length is less than 100, the spliced integer is less than 2 ^ 31,), calculates and outputs the maximum factor of the integer (if it is a prime number, its maximum factor is itself)

Input Description: there are multiple groups of data. The first behavior of the input data is a positive integer, indicating the number of character sequences, each of which is a one-line character sequence.

Output Description: For each character sequence, obtain the maximum factor of the obtained integer. If there is no number in the Character Sequence or the integer found is 0, the output is 0, and each integer occupies one line of output.

Input sample:

3

Sdf0ejg3. F? 9f

? 4afd0s & amp; 2d79 * (G

ABCDE

Output sample:

13

857

0


# Include "stdio. H "# define Max 100int maxprimefactor (int n) {int I, ANS = 0; for (I = 2; I * I <= N; I ++) {While (N % I = 0) {ans = I; N/= I ;}} if (n> 1) ans = N; return ans ;} bool isdigit (char ch) {// determines whether it is a number. If (CH> = '0' & Ch <= '9') return true; elsereturn false;} int main () {char STR [Max] [Max], TMP; int res [Max]; int N; scanf ("% d", & N); For (Int J = 0; j <n; j ++) {scanf ("% s", STR [J]); int sum = 0; int I = 0; Res [J] = 0; while (STR [J] [I]! = '\ 0') {TMP = STR [J] [I]; If (isdigit (TMP) {sum = sum * 10 + (TMP-'0 ');} I ++;} res [J] = maxprimefactor (SUM) ;}for (INT I = 0; I <n; I ++) printf ("% d \ n ", res [I]); Return 0 ;}



Problem D: The first and middle sequence of a binary tree are known, and the post sequence of the binary tree is calculated and output.

Input Description: only one group of data is input in two rows. The first line indicates the first sequence of the specified binary tree, and the second line indicates the middle sequence of the binary tree. The sequence elements are uppercase English characters, the node of the binary tree.

Output Description: output the post sequence of the binary tree on one row.

Input sample:

Abdgcefh

Dgbaechf

Output sample:

Gdbehfca


#include <stdio.h>#include <string.h>struct Node{Node *lchild;Node *rchild;char c;}Tree[50];int loc;Node *creat(){Tree[loc].lchild = Tree[loc].rchild = NULL;return &Tree[loc++];}char str1[30],str2[30];void PostOrder(Node *T){if(T->lchild != NULL){PostOrder(T->lchild);}if(T->rchild != NULL){PostOrder(T->rchild);}printf("%c",T->c);}Node *build(int s1,int e1,int s2,int e2){Node *ret = creat();ret->c = str1[s1];int rootIdx;for(int i = s2; i <= e2; i++){if(str2[i] == str1[s1]){rootIdx = i;break;}}if(rootIdx != s2){ret->lchild = build(s1+1,s1+(rootIdx-s2),s2,rootIdx-1);}if(rootIdx != e2){ret->rchild = build(s1+(rootIdx-s2)+1,e1,rootIdx+1,e2);}return ret;}int main(){while(scanf("%s",str1) != EOF){scanf("%s",str2);loc = 0;int L1 = strlen(str1);int L2 = strlen(str2);Node *T = build(0,L1-1,0,L2-1);PostOrder(T);printf("\n");}return 0;}


Problem E: write a program to determine whether the parentheses in the given expression match. The valid parentheses in the expression are "(", ")", "[", "]", "{", "}", which can be nested in any order.

Input Description: there are multiple expressions. the first line of the input data is the number of expressions, and each expression occupies one row.

Output Description: For each expression, if the brackets in it match, "yes" is output; otherwise, "no" is output ".

Input sample:

4

[(D + F) * {}]

[(2 + 3 ))

()}

[4 (6] 7) 9

Output sample:

Yes

No

No

No

#include <stdio.h>#include <string.h>#include <stack>using namespace std;#define N 100int ans[N];char buf[N][N];int match(char exp[],int n){stack<char> s;int i = 0,tag = 1;while(i < n && tag == 1){if(exp[i] == '(' || exp[i] == '[' || exp[i] == '{'){s.push(exp[i]);}else if(exp[i] == ')'){if(s.empty()){tag = 0;break;}if(s.top() == '('){s.pop();}elsetag = 0;}else if(exp[i] == ']'){if(s.empty()){tag = 0;break;}if(s.top() == '['){s.pop();}elsetag = 0;}else if(exp[i] == '}'){if(s.empty()){tag = 0;break;}if(s.top() == '{'){s.pop();}elsetag = 0;}i++;}if(s.empty() == false)tag = 0;return tag;}int main(){int n;while(scanf("%d",&n) != EOF){for(int i = 0; i < n; i++)scanf("%s",buf[i]);    int cnt = 0;for(i = 0; i < n; i++){int len = strlen(buf[i]);if(match(buf[i],len))ans[cnt++] = 1;elseans[cnt++] = 0;}for(i = 0; i < cnt; i++)if(ans[i])printf("yes\n");elseprintf("no\n");}return 0;}



Postgraduate review machine questions (2009)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.