C/C++ - Problem Statement: Constraints
C/C++ - Problem Statement: Constraints
NCE College of Technology is a popular college in the city. In their library department they were facing some problems in issuing and returning the books. They decided to make some changes in the books order and their unique number. So they are in need of developing new system for avoiding their problems. They will give the existing book number; you have to give the new number such a way that, a new book numbers the minimal next one with the same sum of digits and change the alphabet according to the sum and give the new book numbers. Constraints: The input is a combination of one alphabet and numerical values The input should always start with an alphabet. The output is INVALID if there is more than one alphabet or no alphabet. The output numerical combinations should be greater than the input numerical combinations and the output consists equal number of digits as in the input including the alphabet.
Examples
Input1 = J0921 Ouput1 = M0930. As the sum of the digits in the input is 12 the output is the next nearest digit whose sum is 12 The alphabet is obtained in a such a way that J(0+9+2+1)=J(12) J(1+2)=J(3)=J+3=M. Therefore the output is M0930.
Input2 = 67867 Ouput2 = INVALID Input3 = U992 Ouput3 = INVALID Input4 = U0992 Ouput4 =
W1199
C/C++ Solution
#include <stdio.h> #include <string.h> #include "Booknumber.h" char* getNewNumber(char* number){ char* alpha = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; char result[20]; char valpha, ralpha[10]; char temp[10]; int len,rlen,i,j,val, rval; int sum=0, tsum=0, vtmp,flag=0; len = strlen(number); for(i=0;i<26;i++) { if(number[0] == alpha[i]) flag = 1; } if(flag == 0) return "INVALID"; for(i=1,j=0;i<len;i++,j++) temp[j] = number[i]; } val = atoi(temp); vtmp = val; for(;vtmp>0;) { sum = sum + (vtmp%10); vtmp = vtmp / 10; } if(sum == 0) return "INVALID"; for(i=val+1;;i++) { vtmp = i; tsum = 0; for(;vtmp >0;) { tsum = tsum + (vtmp%10); {
if(sum > 9) { vtmp = sum; tsum = 0; for(;vtmp > 0;) { tsum = tsum + (vtmp%10); vtmp = vtmp / 10; } sum = tsum; } flag=0; for(i=0,j=0;i<26,j<sum;i++) if(flag == 1) j++; if(alpha[i] == valpha) flag = 1; if(i == 26) i=0; } itoa(rval, result, 10); rlen = strlen(result) + 1; rlen = len - rlen; if(rlen == -1) return "INVALID"; ralpha[0] = alpha[i-1]; ralpha[1] = '\0'; for(i=1;i<=rlen;i++) { ralpha[i] = '0'; ralpha[i+1] = '\0'; } strcat(ralpha, result); char* ptr = (char*)malloc(sizeof(ralpha));; {
int main() { char number[] = "U0992"; char* newNo = getNewNumber(number); printf ("\nNew Number : %s",newNo); }