Showing posts with label Volume 100 (10000-10099). Show all posts
Showing posts with label Volume 100 (10000-10099). Show all posts

Saturday, December 21, 2013

10035 - Primary Arithmetic

Problem B: Primary Arithmetic

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. 
 
Solution
#include <stdio.h>
#define M 10

int main(){
   unsigned long A,B;
   char SA[M],SB[M];
   int C,COUNT;
   while(scanf("%lu%lu",&A,&B) == 2 && (A || B)){
      COUNT = C = 0; 
      while(A||B){
         C = (C+A%10+B%10)/10;
         A /= 10;
         B /= 10;
         COUNT += C;
      }
  
      if(COUNT){
         if(COUNT == 1)
            printf("1 carry operation.\n");
         else
            printf("%d carry operations.\n",COUNT);     
      }else
         printf("No carry operation.\n");
   }
   return 0;
}
 

10038 - Jolly Jumpers

Problem E: Jolly Jumpers

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.

Input

Each line of input contains an integer n <= 3000 followed by n integers representing the sequence.

Output

For each line of input, generate a line of output saying "Jolly" or "Not jolly".

Sample Input

4 1 4 2 3
5 1 4 2 -1 6

Sample Output

Jolly
Not jolly
 
Solution:
#include <stdio.h>
#define MAX 3000
int main(){ 
   static int N, I, J, V[MAX], A[MAX];

   while(scanf("%d",&N) == 1){ 
      for(I = 0; I < N; I++){
         scanf("%d",&V[I]);
         A[I] = 0;        
      }
      J = N-1;
      for(I = 0; I < J; I++)
         A[abs(V[I]-V[I+1])] = 1;
      J = 1;
      for(I = 1; I < N; I++){
         if(!A[I]){
            J = 0;
            break;
         }
      }
      if(J)
         printf("Jolly\n");
      else 
         printf("Not jolly\n");
   }
   return 0;
}