Suggestions for Input type from user
Afficher commentaires plus anciens
I am having issues with the input from the user in a pre-defined format and converting them into required digits, that are to be checked for divisibility of 11.
Cells are used to fetch the data from user. But, the code is failing when the number of test cases are more than 1.
Error - Index exceeds the number of array elements
Could anyone advice me whats the appropriate way to store the input from user.
Elevenagram from Google Kickstart, Round H, 2019 -
It is a well known fact that a number is divisible by 11 if and only if the alternating sum of its digits is equal to 0 modulo 11. For example, 8174958 is a multiple of 11, since 8 - 1 + 7 - 4 + 9 - 5 + 8 = 22.
Given a number that consists of digits from 1-9, can you rearrange the digits to create a number that is divisible by 11?
Since the number might be quite large, you are given integers A1, A2, ..., A9. There are Ai digits i in the number, for all i.
Input
The first line of the input gives the number of test cases, T. T lines follow. Each line contains the nine integers A1, A2, ..., A9.
Input Example -
2 %No. of Test Cases
0 0 2 0 0 1 0 0 0 % the digits are 336
0 0 0 0 0 0 0 0 12 % the digits are 999999999999 = 9 - 12's
Code ( To Convert the user input(s) into actual number(s) only)
n = input ('Enter Test cases : ');
for i=1:n
iter{i} = input('Enter Values :\n','s');
end
for i=1:n
iter{i} = str2num(iter{i});
end
digit = {[1:9]};
% 0 3 0 0 0 0 0 0 0; 1 2 3 4 5 6 7 8 9;
% gives 222 - actual number to be checked later for divisiblity of 11
len_iter=length(iter);
len_iter_digit=length(iter{1});
len_digit = length(digit{1});
ele = zeros(1,len_digit);
for j =1: len_iter
for i = 1:len_iter_digit
ele(i) = sum( 10.^((1:iter{j}(i))-1));
ele(i) = ele(i)*digit{j}(i);
end
end
2 commentaires
Rik
le 10 Sep 2020
What is your code trying to do? Are you trying to convert the input to the actual number?
Akshay Patil
le 10 Sep 2020
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur MATLAB Production Server dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!