| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10850 | license |
|
| 10852 | Simple Addition |
|
Description
There are N license. Each license has four-digit integer, each integer between 1 to 9. For example, if N=7, the input may look like
7
1324
5566
3578
4123
5656
4312
9847
We want to get the licenses that use the same digits and the digits should be sorted from small to large. For example, 1324, 4123, and 4312 are the combination of the same digits (1, 2, 3, and 4). We want to get 1234. Similarly, 5566 and 5656 are the combination of the same digits (5, 5, 6, and 6). We want to get 5566.
For output, we list the modified licenses in an increasing order line by line, so the output will look
1234
5566
<Hint>
You may consider the following algorithm:
0. Create a character array (char str[5];) for storing the input integers and create an array which is initialed to zero (int HIT[10000];) to record the occurrences of input integers.
1. Read the number of input integers and store it in variable N (scanf("%d", &N);).
2. For each of the next N integers, do
2.1 Read the four-digit integer and store it as a four-character string in str.
2.2 Use the bubble sort algorithm to rearrange the four characters.
2.3 Convert the rearranged string to an integer m and increase the value of the corresponding element of the HIT array by 1 (HIT[m]++;).
3. Check each element of the HIT array; if its value is larger than 2, print the corresponding integer.
The bubble sort algorithm:
/* Using bubble sort to rearrange an array A[n] */
for (i = n; i > 0; i--) {
for (j = 1; j < i; j++) {
if (A[j-1] > A[j]) {
/* swap A[j-1] A[j] */
}
}
}
Input
The first line contains a number N (1<=N<=100).
The next N lines provide the N four-digit integers.
Output
Each line shows a modified unique integer as explained in the problem description.
Remember to print a newline at the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Four arrays A, B, C and D with size MxN. B, C, D are stored in an array of pointers, and A is stored in a separated array. The task is to add the designated elements of A and the corresponding element from a chosen array of B, C, D, and output the result.
Take sample input as an example. Size of the arrays are 3 rows by 2 columns, initial values are as the form below. Input number “2” in the sixth line indicates D is chosen array and we want the sum of elements with two indices (1,0), (2,0)
Therefore, the answer is A(1,0)+D(1,0)+A(2,0)+D(2,0)=2+8+4+10=24
Note that
1. This problem involves three files.
- function.h: Function definition of addition.
- function.c: Function describe of addition.
- main.c: A driver program to test your implementation.
You will be provided with main.c and function.h, and asked to implement function.c.
2. For OJ submission:
Step 1. Submit only your function.c into the submission block. (Please choose c compiler)
Step 2. Check the results and debug your program if necessary.
3. If the same index appears n times, just add the values n times.
main.c
#include <stdio.h>
#include ”function.h”
int main(void) {
int a[50][50], b[50][50], c[50][50], d[50][50];
int index_to_add[20];
int *entry[3];
int i, j, m, n, array_num, num_ind;
scanf("%d %d", &m, &n);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf(“%d”, &a[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf(“%d”, &b[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf(“%d”, &c[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf(“%d”, &d[i][j]);
scanf("%d", &array_num);
scanf("%d", &num_ind);
for(i=0; i<num_ind*2; i=i+2)
scanf("%d %d", &index_to_add[i], &index_to_add[i+1]);
entry[0] = &b[0][0];
entry[1] = &c[0][0];
entry[2] = &d[0][0];
/*
&a[0][0]: Address of A’s first element
array_num: Index of chosen array. 0: B, 1:C, 2:D.
entry: An array of pointers, stores {&b[0][0], &c[0][0], &d[0][0]}
index_to_add: Locations to be summed. Value would be {1,0,2,0,…} in sample case.
num_ind: Number of locations to be summed.
*/
printf("%d\n", addition(&a[0][0], array_num, entry, index_to_add, num_ind));
return 0;
}
function.h
#ifndef FUNCTION_H #define FUNCTION_H int addition(int*, int, int*[], int*, int);
#endif
Input
The first line is the size of arrays, M rows by N columns. The following four lines are initial values arranged by row major. The sixth line tells B, C or D is chose. The next line is the number of elements k to be summed. And the last k lines are the row and column indices of elements to be add. 0 < M, N < 50. 0 < k < 10. Index starts from 0.
Output
An integer, sum of desired elements.