Codeforces 109A. Lucky Sum of Digits Solution

Solution in c++:
     ///**********ALLAH IS ALMIGHTY************///
     ///AH Tonmoy
    ///Department of CSE,23rd batch
    ///Islamic University,Bangladesh
#include<iostream>
using namespace std;
int  main()
{
    int sum,a,b,i,j;
    while(cin>>sum)
    {
        a=b=0;
        while(sum>0)
        {

            if(sum%7==0)
            {
                b++;
                sum-=7;
            }
            else if(sum%4==0)
            {
                a++;
                sum-=4;
            }
            else
            {
                a++;
                sum-=4;
            }
        }
        if (sum < 0)
        {
            printf("-1\n");
            break;
        }

        for (int i=0; i<a; i++)
            printf("4");

        for (int i=0; i<b; i++)
            printf("7");

        cout<<endl;

    }
}

No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.