Seven-Segment Display


C++ Solution:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int T;
  6. cin>>T;
  7. while(T--)
  8. {
  9. string L;
  10. cin>>L;
  11. int m=0;
  12. for(int i=0; i<L.size(); i++){
  13. if(L[i]=='1'){
  14. m=m+2;
  15. }
  16. else if(L[i]=='7')
  17. {
  18. m=m+3;
  19. }
  20. else if(L[i]=='4')
  21. {
  22. m=m+4;
  23. }
  24. else if(L[i]=='2' || L[i]=='3' || L[i]=='5')
  25. {
  26. m=m+5;
  27. }
  28. else if(L[i]=='8')
  29. {
  30. m=m+7;
  31. }
  32. else
  33. {
  34. m=m+6;
  35. }
  36. }
  37. if(m%2==1)
  38. {
  39. cout<<7;
  40. m=m-3;
  41. }
  42. for(int i=0; i<m/2; i++)
  43. {
  44. cout<<1;
  45. }
  46. cout<<endl;
  47. }
  48. return 0;
  49. }
Java Solution:

  1. import java.util.*;
  2. class TestClass {
  3. public static void main(String args[] ) throws Exception {
  4. Scanner s = new Scanner(System.in);
  5. int t = s.nextInt();
  6. for (int i = 0; i < t; i++) {
  7. String num = s.next();
  8. int matches = findNumberOfMatches(num); //to find the number of matches
  9. System.out.println(findMaxNumber(matches)); //to find the max number that
  10. }

  11. }
  12. static int findNumberOfMatches(String num){
  13. int sum=0;
  14. int a[] = {6,2,5,5,4,5,6,3,7,6};//array containing the number of matches for each digit.
  15. for (int i =0; i<num.length(); i++){
  16. sum = sum + a[num.charAt(i)-'0'];
  17. }
  18. return sum;
  19. }
  20. static String findMaxNumber(int matches){
  21. String sum="";
  22. if(matches%2==0){
  23. while(matches>0){
  24. sum = sum + "1";
  25. matches = matches-2;
  26. }

  27. }
  28. else{
  29. sum="7";
  30. while(matches>3){
  31. sum = sum + "1";
  32. matches = matches-2;
  33. }

  34. }
  35. return sum;
  36. }
  37. }

Note:
       We Are Providing Every Content in this Blog Inspired From Many People and Provide it Open To All People Who Are Willing Gain Some Stuff in Computer Science.

Comment Below👇 If You Find Better Solution Than Above Solution.