C language switch-case: avoiding a case after used once - c

I'm trying a challenge where I need to get a random number, and print the sum of the digits inside the number without duplicates:
for example, 123 will print 6 ( 1 + 2 + 3 ), and 32111 will do the same ( because we don't add duplicates to our sum, the sum of this number is similar to the sum of 123. )
In my solution I thought about using switch case for each number, and use a flag that its value is one, than in each case I add 1 to the flag, and when the flag is 2 I add the number to the sum, but I don't know how to avoid a case after it happend, which if I see it correctly will avoid me using multiple flags for each number (because if we could avoid a case after it happend, i just could set the flag back to one after the switch and do all the process again )
can you help me out? thanks a lot!
#include <stdio.h>
#define TEN 10
#define NINE 9
#define EIGHT 8
#define SEVEN 7
#define SIX 6
#define FIVE 5
#define FOUR 4
#define THREE 3
#define TWO 2
#define ONE 1
int main(void)
{
int answer = 0, i = 0, remain = 0, sum = 0, flag = 1;
printf("Enter a number: ");
scanf("%d", &answer);
while(answer >= ONE)
{
remain = answer % TEN;
answer /= TEN;
printf("%d\n", remain);
switch (remain)
{
case ONE:
{
flag++;
if (flag == TWO)
{
sum = sum + ONE;
}
break;
}
case TWO:
{
flag++;
if (flag == TWO)
{
sum = sum + TWO;
}
break;
}
case THREE:
{
flag++;
if (flag == TWO)
{
sum = sum + THREE;
}
break;
}
case FOUR:
{
flag++;
if (flag == TWO)
{
sum = sum + FOUR;
}
break;
}
case FIVE:
{
flag++;
if (flag == TWO)
{
sum = sum + FIVE;
}
break;
}
case SIX:
{
flag++;
if (flag == TWO)
{
sum = sum + SIX;
}
break;
}
case SEVEN:
{
flag++;
if (flag == TWO)
{
sum = sum + SEVEN;
}
break;
}
case EIGHT:
{
flag++;
if (flag == TWO)
{
sum = sum + EIGHT;
}
break;
}
case NINE:
{
flag++;
if (flag == TWO)
{
sum = sum + NINE;
}
break;
}
default:
{
}
}
}
printf("The sum of the number is: %d", sum);
return 0;
}

Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.

A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...

Related

CS50 Problem Set 1 (Credit) 2020 help needed

I am trying to prompt the user for a credit card number and determine whether it is a real credit card number or not, and if so what type of credit card number.
I thought I'd finally got it however when doing check50 the following two inputs produce no output:
1234567890
4111111111111113
They should be giving INVALID but I can't figure out why they aren't giving any output.
This is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
long Card_Number;
int Digit_Number = 0, Current_Digit = 0, Even_x2_Product = 0, Even_Digits = 0, Odd_Digits = 0,
Total_Digit_Sum = 0;
bool is_even = false;
// Prompt User for Credit Card Number
do
{
Card_Number = get_long("Card Number: ");
}
while (Card_Number < 0);
// Check First Digits of Number
int Digits_MstrCrd = Card_Number / pow(10, 14);
int Digits_Visa_16 = Card_Number / pow(10, 15);
int Digits_AmEx = Card_Number / pow(10, 13);
int Digits_Visa_13 = Card_Number / (pow(10, 12));
// Loop to determine identity of each digit
while (Card_Number != 0)
{
// Get Last Digit of Number
Current_Digit = (Card_Number % 10);
// Increase Digit Number by 1
Digit_Number += 1;
// Check if Current Digit is at Odd or Even Position in Card Number
if (is_even == true)
{
// Multiply Digit by 2
Even_x2_Product = Current_Digit * 2;
// Add Digits of Multiplication Product
while (Even_x2_Product != 0)
{
Even_Digits += Even_x2_Product % 10;
Even_x2_Product /= 10;
}
// Tell Program Next Digit is Odd
is_even = false;
}
else
{
// Add Odd Digits
Odd_Digits += Current_Digit;
// Tell Program Next Number is Even
is_even = true;
}
// Remove Last Digit and Repeat
Card_Number /= 10;
}
// Add Odd and Even Digits Together
Total_Digit_Sum = Even_Digits + Odd_Digits;
// Loop to Check if Card Number is Valid
if (Total_Digit_Sum % 10 == 0)
{
// Check Mastercard
if (Digit_Number == 16)
{
if (Digits_MstrCrd <= 55 && Digits_MstrCrd >= 51)
{
printf("MASTERCARD\n");
}
// Check Visa 16
else if (Digits_Visa_16 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
// Check American Express
else if (Digit_Number == 15)
{
if (Digits_AmEx == 34 || Digits_AmEx == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
// Check Visa 13
else if (Digit_Number == 13)
{
if (Digits_Visa_13 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
}
What does the program print if Total_Digit_Sum % 10 is not equal to 0? It has no else; there are no commands after the closing } of the block.
I'll show you in your program (with embedded multiline comments) all the objecttionable things I've seen by simple inspection (I've had to modify it a little, as you don't provide an implementation of get_long(char *prompt) function, and some other files you also don't provide. Later I give you a better solution, that doesn't have the problem of the integer limit, as it uses strings to calculate the checksum.
At the end there's a reference to a github repository where all versions of the solution are considered (including a DFA ---Deterministic Finite Automaton--- probably the fastest solution to the problem)
/* sorry, I need to comment this, as you have not provided this
* file. */
//#include <cs50.h>
/* you don't need math.h if you are using only integers */
//#include <math.h>
/* what is needed is stdbool.h, to use booleans in C */
#include <stdbool.h>
#include <stdio.h> /* and stdio, of course */
unsigned long long get_long(char *prmpt)
{
unsigned long long result;
fprintf(stderr, "%s> ", prmpt);
/* this loop is not protected against EOF, so you will have
* to interrupt the program if you reach the end of file
* here. */
while (scanf("%llu", &result) != 1)
fprintf(stderr, "?? >");
return result;
}
int main(void)
{
/* you need a 64bit number, so better use a long long here
* 32bit integers range only up to 4294967296, which is too
* short to use in your problem.
* on dividing your card number by 100000000000000 you'll
* allways get 0.
*/
long Card_Number;
int Digit_Number = 0, Current_Digit = 0, Even_x2_Product = 0, Even_Digits = 0, Odd_Digits = 0,
Total_Digit_Sum = 0;
bool is_even = false;
// Prompt User for Credit Card Number
do
{
Card_Number = get_long("Card Number: ");
}
while (Card_Number < 0);
// Check First Digits of Number
/* don't use pow(3) to produce a constant to divide
* in floating point by a power of ten. It allways
* produces inexact results, ad 1/10 cannot be represented
* as a finite number of digits in base 2. Just use
* 100000000000000LL, instead.
* In order to get the ttype of card, it is better to compare
* the number, as in
* // number is 15digits, at least
* if (Card_number >= 1000000000000000ULL) {
* Digit_number = 15;
* } else if (Card_number >= 10000000000000ULL) {
* Digit number = 14;
* } else if (Card_number >= 1000000000000ULL) {
* Digit_number = 13;
* ...
*/
int Digits_MstrCrd = Card_Number / pow(10, 14);
int Digits_Visa_16 = Card_Number / pow(10, 15);
int Digits_AmEx = Card_Number / pow(10, 13);
int Digits_Visa_13 = Card_Number / (pow(10, 12));
// Loop to determine identity of each digit
while (Card_Number != 0)
{
// Get Last Digit of Number
Current_Digit = (Card_Number % 10);
// Increase Digit Number by 1
/* why do you increment the digit by one, the digit value
* is just that, the remainder of the integer division.
*/
Digit_Number += 1;
// Check if Current Digit is at Odd or Even Position in Card Number
/* better use if (is_even) as is_even is already a
* boolean */
if (is_even == true)
{
// Multiply Digit by 2
Even_x2_Product = Current_Digit * 2;
// Add Digits of Multiplication Product
/* Even_x2_Product cannot be higher that 18,
* so why not just check if it is greater than 10
* and then subtract 10 and add 1 (or better,
* just subtract 9), as in:
if (Even_x2_Product >= 10)
Even_x2_product -= 9;
*/
while (Even_x2_Product != 0)
{
Even_Digits += Even_x2_Product % 10;
Even_x2_Product /= 10;
}
// Tell Program Next Digit is Odd
/* Shouldn't we add this result somewhere,
* mod 10 ??? Like in:
accumulated_checksum += Even_x2_Product;
Note: you do in the odd part.
*/
is_even = false;
}
else
{
/* I suggest you to add all digits together.
* As in:
accumulated_checksum += Current_digit;
*/
// Add Odd Digits
Odd_Digits += Current_Digit;
// Tell Program Next Number is Even
is_even = true;
}
/* if we have added two digits (the accumulated_checksum
* and the calculated one, no possibility of having more
* than 18 as the sum is possible, so check if the result
* is 10 or more, and subtract 10 to eliminate the carry.
if (accumulated_checksum >= 10)
accumulated_checksum -= 10;
*/
// Remove Last Digit and Repeat
Card_Number /= 10;
}
/* you can use only one sum. Both are digits... and if you
* have made the checks suggested above, it is already a number
* modulo 10. */
// Add Odd and Even Digits Together
/* this is not necessary */
Total_Digit_Sum = Even_Digits + Odd_Digits;
// Loop to Check if Card Number is Valid
/* you don't need to calculate the modulo 10 here, as you
* have eliminated all the higher digits in the last loop.
*/
if (Total_Digit_Sum % 10 == 0)
if (Total_Digit_Sum % 10 == 0)
{
// Check Mastercard
/* this is not the number of digits you have, this is the
* integer result of the division by a huge number...
* most of the times this will be zero, but it never be
* 16, with the numbers you are giving for the cards. */
if (Digit_Number == 16)
{
if (Digits_MstrCrd <= 55 && Digits_MstrCrd >= 51)
{
printf("MASTERCARD\n");
}
// Check Visa 16
else if (Digits_Visa_16 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
// Check American Express
/* also this is not true, by the same reason above. */
else if (Digit_Number == 15)
{
if (Digits_AmEx == 34 || Digits_AmEx == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
// Check Visa 13
/* same as above */
else if (Digit_Number == 13)
{
if (Digits_Visa_13 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else
{
/* so you always end here */
printf("INVALID\n");
}
}
}
There's no need to convert the string of digits into a number... this will make your processing more complicated, and you will need to swith to long long numbers to use it on the longest card numbers.
I have developed this routine:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "proc.h"
int process(const char *str)
{
int l = strlen(str);
const char *p = str + l;
int res = 0;
enum {
ODD_DIGIT,
EVEN_DIGIT,
} pos = ODD_DIGIT;
DEB("processing: [%s]\n", str);
while (--p >= str) {
if (!isdigit(*p)) {
WARN("%s\n", str);
WARN("%*s^: is not a digit\n", (int)(p-str), "");
return -1;
}
int dig = *p - '0';
switch (pos) {
case ODD_DIGIT: pos = EVEN_DIGIT;
DEB("Add dig(%d) to res(%d)\n", dig, res);
res += dig; break;
case EVEN_DIGIT: pos = ODD_DIGIT;
DEB("Add double(dig(%d)) to res(%d)\n", dig, res);
dig <<= 1;
if (dig >= 10)
dig -= 9;
res += dig; break;
}
if (res >= 10)
res -= 10;
DEB("res <= %d\n", res);
}
DEB("Returning => %d\n", res);
if ((flags & FLAG_QUIET) == 0) {
printf("%s: %d\n", str, res);
}
return res;
}
that uses a string of digits, and processes it from right to left (beginning on the end of the string) It is part of this code, published in github and that you can download the complete program from here. You'll find there the version published here, if you checkout the version tagged as SO_60424279, and in the branch master you'll get a table driven DFA implementation that should run faster than this one.
To compile, just execute
make
in the directory you extracted the source.
#include <stdio.h>
#include<cs50.h>
#include <math.h>
int main(void)
{
long x = get_long("enter the credit card number");
int digit = 0, sum = 0;
//digit is used for odd and even checker.
long y = x;
//checksum card digits
while (y != 0)
{
int sumeven = 0, sumodd = 0;
int rem = y % 10;
digit++;
if (digit % 2 == 0) //if digit is even
{
int multiply = rem * 2;
if (multiply == 0)
{
sumeven += multiply;
}
else
{
while (multiply != 0) //adding all digits after
{
sumeven += multiply % 10;
multiply /= 10; //minus last digit of multiply
}
}
}
else //if digit is odd
{
sumodd += rem;
}
y /= 10; //minus last digit from y
sum += sumeven + sumodd;
}
//check for valid credit card
if (digit != 13 && digit != 15 && digit != 16) //for first if
{
printf("INVALID\n");
}
else if (sum % 10 == 0)
{
if (digit == 16) //if digit is 16
{
if (x / 100000000000000 >= 51 && x / 100000000000000 <= 55)
{
printf("MASTERCARD\n");
}
else if (x / 1000000000000000 == 4)
{
printf("VISA\n");
}
else //if digit is not 16
{
printf("INVALID\n");
}
}
else if (digit == 15)
{
if (x / 10000000000000 == 34 || x / 10000000000000 == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else if (digit == 13)
{
if (x / 1000000000000 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}`

Trying to find prime numbers

I'm a first year student in a programming university and my first assignment is to find the sum of prime numbers between 3990000000 and 4010000000. The problem is everything I do, when I run the program it says the sum is 0 with a return value of 25. I've been trying to debug this code but with no luck, could someone help me?
My code is:
#include <stdio.h>
#define STARTNUMBER 3990000000
#define ENDNUMBER 4010000000
int main() {
unsigned int num;
int j, c, flag, sum = 0;
flag = 1;
c = 5;
j = 7;
for (num = STARTNUMBER; num <= ENDNUMBER; num++) {
if (num % 2 == 0) { /*if number mod 2 equals zero go to next number*/
flag = 0;
break;
}
if (num % 3 == 0) { /*if number mod 3 equals zero go to next number*/
flag = 0;
break;
} else
/*check if number is prime with the sequences 5+6+6...<=sqrt(number) and 7+6+6..<=sqrt(number)*/
while (c * c <= num && j * j <= num && flag == 1) {
if (num % c == 0 || num % j == 0) {
flag = 0;
break;
}
c += 6;
j += 6;
}
if (flag == 1)
sum++;
}
printf("There are %d prime numbers", sum);
}
You are asking for the sum of the prime number, even if your code is just printing how many they are. Assuming you've misunderstood the exercise, I try to show a possible problem of your original question, glimpsing a possible trick in your exercise since the interval is very close to 232.
Assuming also you are on a 64-bit environment, if there are at least two prime numbers in that inteval, the sum is going to be greater than INT_MAX (231 - 1). An int is not sufficient to store a value, and also unsigned int is not sufficient since UINT_MAX is 232 - 1.
Finally, assuming you've solved your problems with the break statements already described in the comments, try to store your sum variable into a unsigned long int, and replace the last part of the loop with
if (flag==1)
sum += num;

How to find closest number in C?

So, I was working out some exercises from a recommended beginner's book: C Programming: A Modern Approach (2nd Edition) While working out one of the questions I found myself unable to proceed, as I couldn't return the number closest to the user's input.
Note: The question was asked before loops, functions and arrays were covered, thus I am assuming that these should not be required.
I managed to extract and simplify the problem to the following. Lets say I have the following numbers, and I want to return the number closest to the user's input:
10 20 30 40 50 60 70 80
printf("Enter a number to find closest value: ");
scanf("%d", &num);
For example:
User's input: 28
Closest number: 30
Next, I decided to find the difference between num and each of the numbers by subtracting from each.
difference1 = num - 10;
difference2 = num - 20;
and so on. (I am not using loops as these have not yet been covered in the book)
I taking into consideration, negative differences (25 - 40 = -15). If the difference is less than 0, I am multiplying the difference by -1 to get all the differences to a positive integer. By doing so I will be able to compare the differences successfully:
if (difference1 < 0) {
difference1 = difference1 * -1;
}
Next I am checking for the minimum difference in order to identify which was the closest number by doing:
if (difference1 < difference2) {
min1 = difference1;
}
else {
min1 = difference2;
}
if (difference3 < difference4) {
min2 = difference3;
}
else {
min2 = difference4;
}
if (difference5 < difference6) {
min3 = difference5;
}
else {
min3 = difference6;
}
if (difference7 < difference8) {
min4 = difference7;
}
else {
min4 = difference8;
}
if (min1 < min2) {
min5 = min1;
}
else {
min5 = min2;
}
if (min3 < min4) {
min6 = min3;
}
else {
min6 = min4;
}
if (min5 < min6) {
min = min5;
}
else {
min = min6;
}
I know this is a very long method, however I was unable to shorten the code without the use of a for-loop.
printf("Difference between the two numbers is %d\n", min);
Since min contains the difference between the user's input and the closest number I am unable to find a way to print the number closest to the user's input.
How can I trace the minimum difference to the original number which this was subtracted from ? Any suggestions would be appreciated, and please excuse the basic nature of this question.
I can help you to make the code shorter.
Here are my approach
int num = 71;
int diff,ans;
int div10 = (num / 10);
int lower = div10 * 10;
int upper = (div10 + 1) * 10;
if(lower<10) lower=10;
if(lower>80) lower=80;
if(upper<10) upper=10;
if(upper>80) upper=80;
int diff1 = lower - num;
int diff2 = upper - num;
if(diff1 < 0) diff1 *= -1;
if(diff2 < 0) diff2 *= -1;
if(diff1 < diff2) {
ans = lower;
diff = diff1;
}
else {
ans = upper;
diff = diff2;
}
printf("ans=%d, diff=%d\n",ans,diff);
Well what I would do:
int main(){
int closest;
int temp;
int a0 = 10, a1 = 20, a2 = 30; //and more..
int user_input = 28;
closest = abs(user_input - a0);
temp = abs(user_input - a1);
if (temp <= closest)
closest = temp;
//and so on
return 0;
}
Where abs is absolute value. Numbers should be in some data structure, because in the mean time you should ask if you still have something to check.
If you're dealing with 10s this should be quite simple:
num += 5;
closest_10 = num/10;
closest_10 *= 10;
Then you can have simple look up if you have an array of numbers. You can expand this to other multiples using a similar algorithm (say for 8s just replace 5 with 4 and 10 with).
As all numbers are fixed, the best approach (at least the most efficient once compiled) is to use the following snippet of code:
int closest(int number)
{
switch(number) {
case 0: case 1: case 2: case 3: case 4:
return 0;
case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14:
return 10;
...
case 95: case 96: case 97: case 98: case 99: case 100:
return 100;
} /* switch */
} /* closest */
Even more efficient if you can inline the code instead of writing a function.
You need to better define: Lets say I have the following numbers.
How many numbers is the big question. Your example has them all as evenly divisible by 10. Your numbers are 10, 20, 30, and so on, and you mention a total of 8 numbers. Is it only 8 numbers maximum? Can they be any number? Will they all be integers? If its all integers and all divisible by 10, then you can use the modulo or modulus operator instead of subtraction.
The mod operator % only works on integers.
int a, b, c;
a = 3;
b = 25
c = a % 10; /* c will equal 3, 0 / 10 = 0 with remainder 3 */
c = b % 10; /* c will equal 5, 25 / 10 = 2 but remainder is 5 */
/* check if a number is even, just mod by 2 if zero then even */
if ( ( b % 2 ) == 0 )
printf(" value in b is even number\n");
else
printf(" value in b is odd number\n");

Counting carry operations

Can anybody tell me why my program keeps getting wrong answer? It must count the number of carry operations in a sum. I tried every testcase came to my mind. I didn't get wrong output.
Problem Description:
Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
Input
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.
Output
For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
Here's my current code:
#include<stdio.h>
int main()
{
unsigned long long int a,b,m,n,rem_m,rem_n,judge=0,sum,count;
while((scanf("%llu%llu",&m,&n))==2)
{
if(m==0 && n==0)
{
break;
}
count=0;
while(m!=0 && n!=0)
{
rem_m=m%10;
rem_n=n%10;
if(judge==1)
{
rem_m++;
}
sum = rem_m+rem_n;
judge=0;
if(sum>=10)
{
count++;
judge++;
}
m=m/10;
n=n/10;
}
if(count==0)
{
printf("No carry operation.\n");
}
else
{
printf("%llu carry operations.\n",count);
}
}
return 0;
}
count the number of carry operations in a sum
Asserting a,b are >= 0:
Terse solution
For fun :)
"ds" stands for digit sum.
int ds(int n){return n == 0 ? 0 : n%10 + ds(n/10);}
int numberOfCarryOperations(int a,int b){return (ds(a) + ds(b) - ds(a+b)) / 9;}
Readable
Here is a more readable variation.
int digitSum(int n)
{
int sum;
for (sum=0; n > 0; sum+=n%10,n/=10);
return sum;
}
int numberOfCarryOperations(int a,int b){
// a, b >= 0
return (digitSum(a) + digitSum(b) - digitSum(a+b)) / 9;
}
You can prove mathematically: every time you have a carry, the digitSum decreases by 9.
9, because we are in number system 10, so we "lose 10" on one digit if we have carry, and we gain +1 as the carry.
Pythonic version
I do not know how to do this in C, but in python it is easy to write a better digitSum function. In python we can easily create the list of digits from a number, and then just use sum() on it to get digitSum of the given number.
Here is a terse python one-liner solution:
def numberOfCarryOperations(a, b):
# f is the digitSum function
f=lambda n:sum(map(int,str(n)));return(f(a)+f(b)-f(a+b))/9
The loop condition is wrong. You want while(m!=0 || n!=0) (i.e. while at least one of them is not zero) instead of while(m!=0 && n!=0), otherwise the answer will be wrong for things like 999 9, it will incorrectly stop after one iteration and report 1 carry operation whereas the correct answer should be 3. Think of it like this: you only want to stop when both of them are 0, so the loop must continue as long as at least one of the numbers is not 0.
Also, you forgot to clean up judge after printing output. You need to clear it before reading input again, or you could mistakenly have judge == 1 from a previous computation that ended with a carry (the name choice for this variable seems odd to me, you should rename it to something more meaningful like carry, but it's not the main issue here).
a and b are unused (you should enable compiler warnings).
The sample output shows the word operation (as in, singular) when the count is 1; your program always writes operations (plural). If you're submitting this to an automatic judge, the code will not pass because the output does not match exactly the expected output. To fix that small little detail, replace this:
else
{
printf("%llu carry operations.\n",count);
}
With:
else
{
printf("%llu carry operation%s.\n",count, count > 1 ? "s" : "");
}
Here's the fixed version:
#include <stdio.h>
int main(void)
{
unsigned long long int m,n,rem_m,rem_n,judge=0,sum,count;
while((scanf("%llu%llu",&m,&n))==2)
{
if(m==0 && n==0)
{
break;
}
count=0;
/* We want || here, not && */
while(m!=0 || n!=0)
{
rem_m=m%10;
rem_n=n%10;
if(judge==1)
{
rem_m++;
}
sum = rem_m+rem_n;
judge=0;
if(sum>=10)
{
count++;
judge++;
}
m=m/10;
n=n/10;
}
/* Clean up for next iteration */
judge = 0;
if(count==0)
{
printf("No carry operation.\n");
}
else
{
printf("%llu carry operations.\n",count);
}
}
return 0;
}
A ruby solution would be:
def count_carry_operations x, y
return 0 if x == 0 && y == 0
count = 0
carry = 0
while true
return count if x == 0 && y == 0
while x != 0 || y != 0
xr = x % 10
yr = y % 10
xr += 1 if carry == 1
sum = xr + yr
carry = 0
if sum >= 10
count += 1
carry += 1
end
x /= 10
y /= 10
end
carry = 0
end
count
end
A java solution would be:
public class Main {
public static int carry_count=0,carry_number=0;
public static void main(String[] args) {
System.out.println(Carry(99511,512));
}
private static int Carry(int num1,int num2){
if(num1/10==0 || num2/10==0){
int sum=num1%10+num2%10+carry_number;
if(sum>=10){
carry_number=1;
carry_count++;
return Carry(num1/10,num2/10);
}else{
return carry_count;}
}else {
int sum=num1%10+num2%10+carry_number;
if(sum>=10){
carry_number=1;
carry_count++;
}else {
carry_number=0;
}
return Carry(num1/10,num2/10);
}
}
}
Java program for people interested
static int numberOfCarryOperations(int num1, int num2) {
int counter = 0;
int result1 = 0;
int result2 = 0;
int carryNum = 0;
while( num1 != 0 || num2 != 0) {
result1 = num1%10;
result2 = num2%10;
if(num1 > 0 ) {
num1 = num1/10;
}
if( num2 > 0) {
num2 = num2/10;
}
if( (result1 + result2+carryNum) > 9 ) {
counter++;
carryNum = 1;
} else {
carryNum = 0;
}
}
return counter;
}
public static void main(String[] args) {
System.out.println(numberOfCarryOperations(123, 456)); // 0
System.out.println(numberOfCarryOperations(555, 555)); // 3
System.out.println(numberOfCarryOperations(900, 11)); // 0
System.out.println(numberOfCarryOperations(145, 55)); // 2
System.out.println(numberOfCarryOperations(0, 0));// 0
System.out.println(numberOfCarryOperations(1, 99999) );// 5
System.out.println(numberOfCarryOperations(999045, 1055) );// 5
System.out.println(numberOfCarryOperations(101, 809)); // 1
System.out.println(numberOfCarryOperations(189, 209) );// 1
}

tricky interview question in C

In the following interview question :
Given a number n, give me the numbers
(among 3..5 and an even number of
numbers) whose adding would return the
original number. The resulting numbers
should be as balanced as possible,
meaning that instead of returning 3
and 5, for instance, return 4 and
4. Ex:
7 = 3 + 4
16 = 4 + 4 + 4 + 4 rather than 3 + 5 + 4 + 4
24 = 12 + 12 or 6 + 6 + 6 + 6
I thought of the following method:
splitnumber(int n)
{
//check if the number is even
if(n%2==0)
{
print(n/2,n/2);
//check if x=2^m multiple exists or
// not..like 4,8,16 etc
print (n/x...n/x);
}
else //else if the no is odd... this part is incomplete
{
if(n-3>0)
{
print (3);
}
n-=3;
if(n>0)
{
if (n>5)
{
print(3)
n-=3;
}
}
}
}
but still I am not able to complete all the cases... How should I check when the answer has unbalanced solution??
if (n < 4) print n;
else
switch (n % 4)
case 0: *print n/4 4's*
case 1: *print n/4 - 1 4's* print 5
case 2: *print n/4 - 1 4's* print 3 print 3
case 3: *print n/4 4's* print 3
Slightly inefficient implementation in C#
if (n < 4) Console.WriteLine(n);
else
switch (n % 4)
{
case 0:
Console.WriteLine(String.Join(" ", new string('4', n / 4).ToArray()));
break;
case 1:
Console.WriteLine(
(String.Join(" ", new string('4', n/4).ToArray().Skip(1)) +
" 5").TrimStart());
break;
case 2:
Console.WriteLine(
(String.Join(" ", new string('4', n/4).ToArray().Skip(1)) +
" 3 3").TrimStart());
break;
case 3:
Console.WriteLine(String.Join(" ", new string('4', n/4).ToArray() +
" 3"));
break;
}
Here is my solution where the result will be perfectly balanced and with detection of impossible cases:
vector<int> recursive_splitnumber(int n) {
if (n <= 5) {
return vector<int>(1,n);
}
int unbalancer = 0;
vector<int> result1, result2;
do {
int val1, val2;
if (n%2 == 0) {
val1 = n%2 + unbalancer;
val2 = n%2 - unbalancer;
}
else {
val1 = (n-1)%2 + 1 + unbalancer;
val2 = (n-1)%2 - unbalancer;
}
result1 = recursive_splitnumber(val1);
result2 = recursive_splitnumber(val2);
// Concatenate the result of the even and odd splits
result1.insert(result1.end(),result2.begin(),result2.end());
++unbalancer;
} while (result1.size()%2 != 0 && unbalancer <= 1);
return result1;
}
bool splitnumber(int n) {
vector<int> split = recursive_splitnumber(n);
if (split.size()%2 == 0) {
copy(split.begin(), split.end(), ostream_iterator<int>(cout, " "));
return true;
} else
return false;
}
That solution will also take into account cases like the number 22 where the balanced division gives 11+11 (11 being a number that cannot be represented using the given rules), the subdivision will be done as 10+12, then 5+5+6+6 and finally 5+5+3+3+3+3.

Resources