Seven-Segment Display
C++ Solution:
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int T;
- cin>>T;
- while(T--)
- {
- string L;
- cin>>L;
- int m=0;
- for(int i=0; i<L.size(); i++){
- if(L[i]=='1'){
- m=m+2;
- }
- else if(L[i]=='7')
- {
- m=m+3;
- }
- else if(L[i]=='4')
- {
- m=m+4;
- }
- else if(L[i]=='2' || L[i]=='3' || L[i]=='5')
- {
- m=m+5;
- }
- else if(L[i]=='8')
- {
- m=m+7;
- }
- else
- {
- m=m+6;
- }
- }
- if(m%2==1)
- {
- cout<<7;
- m=m-3;
- }
- for(int i=0; i<m/2; i++)
- {
- cout<<1;
- }
- cout<<endl;
- }
- return 0;
- }
Java Solution:
- import java.util.*;
- class TestClass {
- public static void main(String args[] ) throws Exception {
- Scanner s = new Scanner(System.in);
- int t = s.nextInt();
- for (int i = 0; i < t; i++) {
- String num = s.next();
- int matches = findNumberOfMatches(num); //to find the number of matches
- System.out.println(findMaxNumber(matches)); //to find the max number that
- }
- }
- static int findNumberOfMatches(String num){
- int sum=0;
- int a[] = {6,2,5,5,4,5,6,3,7,6};//array containing the number of matches for each digit.
- for (int i =0; i<num.length(); i++){
- sum = sum + a[num.charAt(i)-'0'];
- }
- return sum;
- }
- static String findMaxNumber(int matches){
- String sum="";
- if(matches%2==0){
- while(matches>0){
- sum = sum + "1";
- matches = matches-2;
- }
- }
- else{
- sum="7";
- while(matches>3){
- sum = sum + "1";
- matches = matches-2;
- }
- }
- return sum;
- }
- }
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.
Post a Comment
Post a Comment
Learn, Share and Enjoy