URI Online Judge Solution 1557 Square Matrix III Using C++, Python Programming Language.
Write a program that read an integer number N (0 ≤ N ≤ 15) that correspont to the order of a bidimentional array of integers, and build the array according to the above example.
Input
The input consists of several integers numbers, one per line, corresponding to orders from arrays to be built. The end of input is indicated by zero (0).
Output
For each integer number of input, print the corresponding array according to the example. The values of the array must be formatted in a field of size T right justified and separated by a space, where T is equal to the number of digits of the biggest number in the array. None space must be printed after the last character of each row of the array. A blank line must be printed after each array.
Input
The input consists of several integers numbers, one per line, corresponding to orders from arrays to be built. The end of input is indicated by zero (0).
Output
For each integer number of input, print the corresponding array according to the example. The values of the array must be formatted in a field of size T right justified and separated by a space, where T is equal to the number of digits of the biggest number in the array. None space must be printed after the last character of each row of the array. A blank line must be printed after each array.
Sample Input | Sample Output |
1 2 3 4 5 0 | 1 |
Solution Using C++:
#include <bits/stdc++.h>
using namespace std;
int n,T,t,p[31],d[31];
int main(){
p[0] = 1;
for(int i=1; i<31; i++) p[i] = p[i-1] << 1;
for(int i=0; i<31; i++) d[i] = (int) ceil(log10(p[i]+1));
while(scanf("%d",&n) and n){
T = d[2*n-2];
for(int i=0; i<n; i++){
for(int j=0; j<n-1; j++){
t = T - d[i+j];
while(t--) printf(" ");
printf("%d ",p[i+j]);
}
t = T - d[i+n-1];
while(t--) printf(" ");
printf("%d\n",p[i+n-1]);
}
printf("\n");
}
return 0;
}
Comments
Post a Comment