In: Computer Science
A) Many test cases.
For each case, the first line is the number N that indicates the amount of the students in the database.
Next to several N lines are the information of each student, include student ID, name, gender, and age.
And then will receive a number M that indicates there will be M search.
For each search line is an ID number, you need to compare to the database if there is any student with that ID. If yes, print ID, name, gender, and age of this student (if the ID is repeated, just print the previous one); if not, print "No Answer!".
You need to do this question by defining the data structure otherwise you can't real get the points.
Note: need can accept Chinese word! (take care of your array size)
Sample Input
4
1 Anna G 22
2 Teddy B 19
3 Joseph B 21
4 Rita G 22
2
1
5
2
20 阿王 男 16
23 阿美 女 15
1
23
Sample Output
1 Anna G 22
No Answer!
23 阿美 女 15
B)
Use stack to implement postfix arithmetic operation.
Input
Many test cases until EOF.
Each line is a postfix arithmetic operation formula.
There is no blank between the numbers, and the number only is 0-9.
Output
The result of each line.
But if the postfix arithmetic operation formula is wrong (it means you can't get the correct answer), please output "Input Error".
Sample Input
35+
3+5
63/14-*3+8-
Sample Output
8
Input Error
-11
C) There are several block stacks. The number of blocks for each stack may not be same, so your task is to move the block to let them be the same and calculate the minimum moves.
Example,
1 2 3
move stack 3 one block to the stack 1, so it will be:
2 2 2
Input
Many test cases.
For each case, first line is the number N(N<=50) of block stacks.
Next several N numbers are the number of blocks for each stack.
When N=0, the test is end.
Output
The case number and the minimum moves of each case. All the test are legal.
Sample Input
5
1 2 3 4 5
3
9 4 2
0
Sample Output
Set #1
The minimum number of moves is 3.
Set #2
The minimum number of moves is 4.
D)
Naming is important in the code. We always choose the relational vocabulary to name a variable. When we have two or more vocabularies to name, we are used to let the first letter of each vocabulary to be capitalized and connect all the vocabulary.
Take example, room number => RoomNuber sky color => SkyColor
Input
Many test cases until EOF.
Each line is many vocabularies.
Output
Naming result of each vocabulary.
Sample Input
aaaa bbbb cccc
ABCD BCDF FGHRTH
Sample Output
AaaaBbbbCccc
AbcdBcdfFghrth
Programming language: | C++ |
Ans..(B) #include<stdio.h> #include<ctype.h> # define MAXSTACK 100 /* for max size of stack */ # define POSTFIXSIZE 100 /* define max number of charcters in postfix expression */ /* declare stack and its top pointer to be used during postfix expression evaluation*/ int stack[MAXSTACK]; int top = -1 ; /* because array index in C begins at 0 */ /* can be do this initialization somewhere else */ /* define push operation */ void push(int item) { if(top >= MAXSTACK -1) { printf("stack over flow"); return; } else { top = top + 1 ; stack[top]= item; } } /* define pop operation */ int pop() { int item; if(top <0) { printf("stack under flow"); } else { item = stack[top]; top = top - 1; return item; } } /* define function that is used to input postfix expression and to evaluate it */ void EvalPostfix(char postfix[]) { int i ; char ch; int val; int A, B ; /* evaluate postfix expression */ for (i = 0 ; postfix[i] != ')'; i++) { ch = postfix[i]; if (isdigit(ch)) { /* we saw an operand,push the digit onto stack ch - '0' is used for getting digit rather than ASCII code of digit */ push(ch - '0'); } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { /* we saw an operator * pop top element A and next-to-top elemnet B * from stack and compute B operator A */ A = pop(); B = pop(); switch (ch) /* ch is an operator */ { case '*': val = B * A; break; case '/': val = B / A; break; case '+': val = B + A; break; case '-': val = B - A; break; } /* push the value obtained above onto the stack */ push(val); } } printf( " \n Result of expression evaluation : %d \n", pop()) ; } int main() { int i ; /* declare character array to store postfix expression */ char postfix[POSTFIXSIZE]; printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is single digit only.\n"); printf( " \nEnter postfix expression,\npress right parenthesis ')' for end expression : "); /* take input of postfix expression from user */ for (i = 0 ; i <= POSTFIXSIZE - 1 ; i++) { scanf("%c", &postfix[i]); if ( postfix[i] == ')' ) /* is there any way to eliminate this if */ { break; } /* and break statement */ } /* call function to evaluate postfix expression */ EvalPostfix(postfix); return 0; }