Quiz 04 === # [A] Tag ## Description During our childhood, a famous game we have played is the ‘tag’. Tag is a simple game for three or more people that a ghost has to tag someone to transfer the person into ghost. One basic element which determines the number of people one can tag is the speed. More precisely, a ghost can only tag people who have slower speed than it. Write a program to calculate the number of persons which can be caught by a ghost. **Input** The input begins with an integer 𝑚 indicating the number of cases, followed by 𝑚 lines. Each line contains several positive numbers and ends with -1. These positive values in turn indicate the speed of ghost and others. **Output** For each case, output the number of people the ghost can tag by following the format given in sample output **Sample Input** 4 83 22 17 19 96 -1 27 72 39 70 13 68 100 36 95 4 -1 23 34 74 65 -1 17 36 91 43 89 7 41 43 65 49 47 -1 **Sample Output** 3 people have been caught. 2 people have been caught. No one has been caught. One person has been caught. ## Code ```c= #include <stdio.h> int main(){ int m; //cases int ghost, other, caught; //speed scanf("%d", &m); for(int i=0; i<m; i++){ scanf("%d", &ghost); other = 1; caught = 0; while(other != -1){ scanf("%d", &other); if(other == -1){ break; } if(other < ghost){ caught++; } } switch(caught){ case 0: printf("No one has been caught.\n"); break; case 1: printf("One person has been caught.\n"); break; default: printf("%d people have been caught.\n", caught); break; } } } ``` # [B] Adder ## Description An adder is a device for adding two numbers under some number system. When adding integer numbers, their positions are aligned to the right, and each pair of values at the same position will be added. For a number system with base 𝑏, a carry occurred if the sum of the two values at the same position as well as the carry from their previous (right) position is greater than or equal to 𝑏. Help to write a program to calculate the addition of two numbers in any base. **Input** The first input contains an integer, denoting the number of cases. Each case contains three integers, representing the base 2 ≤ 𝑏 ≤ 10, and the two values to be added, respectively. **Output** For each case, output the sequence during addition according to the format shown in the sample output. Each two consecutive cases are separated by a newline character. **Sample Input** 3 2 1001 11 3 2012 101 4 2020 202 **Sample Output** Case 1: 1001 + 11 (base 2) 1 + 1 + 0 = 0 with one carry 0 + 1 + 1 = 0 with one carry 0 + 0 + 1 = 1 1 + 0 + 0 = 1 Case 2: 2012 + 101 (base 3) 2 + 1 + 0 = 0 with one carry 1 + 0 + 1 = 2 0 + 1 + 0 = 1 2 + 0 + 0 = 2 Case 3: 2020 + 202 (base 4) 0 + 2 + 0 = 2 2 + 0 + 0 = 2 0 + 2 + 0 = 2 2 + 0 + 0 = 2 ## Code ```c= #include <stdio.h> int main(){ int n; //cases int base, a, b, carry; scanf("%d", &n); for(int i=1; i<=n; i++){ scanf("%d %d %d", &base, &a, &b); printf("Case %d: %d + %d (base %d)\n", i, a, b, base); carry = 0; while(a || b || carry){ printf("%d + %d + %d = %d", a%10, b%10, carry, (a%10 + b%10 + carry)%base); if(a%10 + b%10 + carry >= base){ printf(" with one carry\n"); carry = 1; } else{ printf("\n"); carry = 0; } a /= 10; b /= 10; } puts(""); } } ``` # [C] SeqLength ## Description Research on sequence over past centuries has obtained many interesting findings. A famous sequence which can almost converge to one can be given in the following description: “For any input x, if it is odd then multiply it by three and then add it by one; otherwise, divide it by two.” Write a program to calculate the sequence length for any initial number x. **Input** The input starts with an integer 𝑚 as the number of cases. Each case contains one integer, which denotes the value of 𝑥. **Output** For each case, output the sequence length y of 𝑥 in the format: “The sequence length for x is y.” **Sample Input** 2 6 8 **Sample Output** The sequence length for 6 is 9. The sequence length for 8 is 4. ## Code ```c= #include <stdio.h> int main(){ int m; //cases int x, tmp_x, y; scanf("%d", &m); for(int i=0; i<m; i++){ scanf("%d", &x); tmp_x = x; y = 1; while(tmp_x > 1){ if(tmp_x%2){ //x is odd tmp_x = tmp_x*3 + 1; } else{ //x is even tmp_x /= 2; } y++; } printf("The sequence length for %d is %d.\n", x, y); } } ``` ###### tags: `程設一quiz`