20130404

457 - Linear Cellular Automata

A biologist is experimenting with DNA modification of bacterial colonies being grown in a linear array of culture dishes. By changing the DNA, he is able ``program" the bacteria to respond to the population density of the neighboring dishes. Population is measured on a four point scale (from 0 to 3). The DNA information is represented as an array DNA, indexed from 0 to 9, of population density values and is interpreted as follows:

  • In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that dish will have a population density of DNA[K].

  • The dish at the far left of the line is considered to have a left neighbor with population density 0.

  • The dish at the far right of the line is considered to have a right neighbor with population density 0.

Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [0,0,0,0,0,0,0,0,0,0]). Others result in immediate population explosions (e.g., [3,3,3,3,3,3,3,3,3,3]). The biologist is interested in how some of the less obvious intermediate DNA programs might behave.

Write a program to simulate the culture growth in a line of 40 dishes, assuming that dish 20 starts with a population density of 1 and all other dishes start with a population density of 0.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

For each input set your program will read in the DNA program (10 integer values) on one line.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each input set it should print the densities of the 40 dishes for each of the next 50 days. Each day's printout should occupy one line of 40 characters. Each dish is represented by a single character on that line. Zero population densities are to be printed as the character' '. Population density 1 will be printed as the character '.'. Population density 2 will be printed as the character 'x'. Population density 3 will be printed as the character 'W'.

Sample Input

1

0 1 2 0 1 3 3 2 3 0

Sample Output

bbbbbbbbbbbbbbbbbbb.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.xbx.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.bb.bb.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.........bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.xbbbbbbbx.bbbbbbbbbbbbbbbbbbbbbbbbbbbb.bbxbbbbbxbb.bbbbbbbbbbbbbbbbbbbbbbbbbb...xxxbbbxxx...bbbbbbbbbbbbbbbbbbbbbbbb.xb.WW.xbx.WW.bx.bbbbbbbbbbbbbbbbbbbbbb.bbb.xxWb.bWxx.bbb.bbbbbbbbbbb

Note: Whe show only the first ten lines of output (the total number of lines must be 50) and the spaces have been replaced with the character "b" for ease of reading. The actual output file will use the ASCII-space character, not "b".

code:

#include <stdio.h>
#include <string.h>
char  trans(int i){
  char s; 
  if (i==0) s=' ';
  if(i==1) s='.';
  if(i==2) s='x';
  if(i==3) s='W';
  return s; 
}
void copy(int *src, int *des, int n){
  while(n--)
*des++=*src++;
}
int main(){

  int casen , init[45], den[15], init2[45];
  scanf("%d", &casen);
  getchar();
  for(int i=0; i<casen; i++){
  memset(init2, 0, sizeof(init2));
  init2[20]=1;
for(int m=0; m<45; m++){
  if(m==20)
    init[m]=1;
  else
    init[m]=0;
}
for(int j=0; j<10; j++)
  scanf("%d", &den[j]);
for(int j=0; j<=49; j++){
  for (int k=1; k<=40; k++){
    printf ("%c", trans(init2[k]));
    init2[k]=den[init[k]+init[k-1]+init[k+1]];
  }
  printf("\n");
  copy(init2, init, 45);
}
if(i!=(casen-1))printf("\n");
getchar(); 
  }
  return 0;
}

694 - The Collatz Sequence

An algorithm given by Lothar Collatz produces sequences of integers, and is described as follows:

Step 1:

Choose an arbitrary positive integer A as the first item in the sequence.

Step 2:

If A = 1 then stop.

Step 3:

If A is even, then replace A by A / 2 and go to step 2.

Step 4:

If A is odd, then replace A by 3 * A + 1 and go to step 2.It has been shown that this algorithm will always stop (in step 2) for initial values of A as large as 109, but some values of A encountered in the sequence may exceed the size of an integer on many computers. In this problem we want to determine the length of the sequence that includes all values produced until either the algorithm stops (in step 2), or a value larger than some specified limit would be produced (in step 4).

Input

The input for this problem consists of multiple test cases. For each case, the input contains a single line with two positive integers, the first giving the initial value of A (for step 1) and the second giving L, the limiting value for terms in the sequence. Neither of these, A or L, is larger than 2,147,483,647 (the largest value that can be stored in a 32-bit signed integer). The initial value of A is always less than L. A line that contains two negative integers follows the last case.Output

For each input case display the case number (sequentially numbered starting with 1), a colon, the initial value for A, the limiting value L, and the number of terms computed.Sample Input

3 100

34 100

75 250

27 2147483647

101 304

101 303

-1 -1

Sample Output

Case 1: A = 3, limit = 100, number of terms = 8

Case 2: A = 34, limit = 100, number of terms = 14

Case 3: A = 75, limit = 250, number of terms = 3

Case 4: A = 27, limit = 2147483647, number of terms = 112

Case 5: A = 101, limit = 304, number of terms = 26

Case 6: A = 101, limit = 303, number of terms = 1

code:

#include <stdio.h>
int main(){
  long x,y, org;
  int n, casen=1;
  while(scanf("%ld%ld", &x, &y)==2&&x!=-1&&y!=-1){
n=1; org=x;
while(x!=1&& x<=y){
  if(x%2==0)
    x=x/2;
  else
    x=x*3+1;
  n++;
   }
if (x>y) n--;
printf("Case %d: A = %ld, limit = %ld, number of terms = %d\n", casen, org, y, n);
casen++;
  }
  return 0; 
}

489 - Hangman Judge

In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows:

  • The contestant tries to solve to puzzle by guessing one letter at a time.

  • Every time a guess is correct, all the characters in the word that match the guess will be 'turned over.' For example, if your guess is 'o' and the word is 'book', then both 'o's in the solution will be counted as 'solved.'

  • Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once.

  • If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.

  • If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.

  • If the contestant does not guess enough letters to either win or lose, the contestant chickens out.

Your task as the ``Hangman Judge'' is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.

Input

Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input).

Output

The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results:

You win.

You lose.

You chickened out.

Sample Input

1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1

Sample Output

Round 1
You win.
Round 2vYou chickened out.
Round 3
You lose.

code

#include <stdio.h>
#include <string.h>

int main(){
  int round, count, flag, wrong, k; 
  char ans[50], gus[50];
  while(scanf("%d", &round)==1&& round!=-1){
scanf("%s", ans);
scanf("%s", gus);
count=0;wrong=0;
for(int i=0; i<strlen(gus); i++){
  flag=0;
  for(int j=0; j<strlen(ans); j++){
    for(k=0; k<i; k++)
      if (gus[i]==gus[k])
    break; 
    if(k==i){
      if (ans[j]==gus[i]){
    count++; 
    flag=1;
      }
    }
  }
  if(count ==strlen(ans)) {
    printf("Round %d\n", round);
    printf("You win.\n");
    break;
    }
  if (!flag&&k==i) wrong++;
  if(wrong==7) {
    printf("Round %d\n", round);
    printf("You lose.\n");
    break;
  }
}



if(wrong<7&&count<strlen(ans)) {
  printf("Round %d\n", round);
  printf("You chickened out.\n");
}


  }
  return 0;
}

537 - Artificial Intelligence?

Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first!

So they don't state a problem like U=10V, I=5A, P=?" but rather likeYou have an electrical circuit that contains a battery with a voltage of U=10V and a light-bulb. There's an electrical current of I=5A through the bulb. Which power is generated in the bulb?".

However, half of the pupils just don't pay attention to the text anyway. They just extract from the text what is given: U=10V, I=5A. Then they think: ``Which formulae do I know? Ah yes, P=UI. Therefore P=10V5A=500W. Finished."

OK, this doesn't always work, so these pupils are usually not the top scorers in physics tests. But at least this simple algorithm is usually good enough to pass the class. (Sad but true.)

Today we will check if a computer can pass a high school physics test. We will concentrate on the P-U-I type problems first. That means, problems in which two of power, voltage and current are given and the third is wanted.

Your job is to write a program that reads such a text problem and solves it according to the simple algorithm given above.

Input

The first line of the input file will contain the number of test cases.

Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xA, U=xV or P=xW, where x is a real number.

Directly before the unit (A, V or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:

DataField ::= Concept '=' RealNumber [Prefix] Unit

Concept ::= 'P' | 'U' | 'I'
Prefix ::= 'm' | 'k' | 'M'
Unit ::= 'W' | 'V' | 'A'
Additional assertions:

The equal sign ('=') will never occur in an other context than within a data field.

There is no whitespace (tabs,blanks) inside a data field.Either P and U, P and I, or U and I will be given.Output

For each test case, print three lines:

  • a line saying ``Problem #k" where k is the number of the test case
  • a line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
  • a blank line

Sample Input

3
If the voltage is U=200V and the current is I=4.5A, which power is generated?
A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please.
bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?
Sample Output

Problem #1
P=900.00W

Problem #2
I=0.45A

Problem #3
U=1250000.00V

code

#include<stdio.h>
#include<string.h>
#include <math.h>
float num(char ques[], int i){
  double value=0;
  int flag=0, neg=0,k=0;
  for(int j=i+1; j<strlen(ques); j++){
if(ques[j]=='-') neg=1;
if(ques[j]=='.') flag=1;
if(ques[j]>='0'&&ques[j]<='9'){
  if(flag) {
    k--;
    value+=pow(10, k)*(ques[j]-'0');
  }
  else
    value=10*value+(ques[j]-'0');
}
if (ques[j]=='m') value=value*0.001;
if(ques[j]=='k') value=value*1.0*1000;
if(ques[j]=='M') value=value*1.0*1000000;
if(ques[j]=='A'||ques[j]=='V'||ques[j]=='W') break;
   }
  if(neg) value=-value;
  return value;
}
int main(){
  int n, fi, fp, fu, org;
  double p, u, i;
  char ques[1000];
  scanf("%d", &n);
  org=n;
  getchar();
  while(n--){
fi=0; fp=0; fu=0;
gets(ques);
for(int x=0; x<strlen(ques); x++){
  if(ques[x]=='='){
    if(ques[x-1]=='P'){
      p=num(ques, x);
      fp=1;

    }
    if(ques[x-1]=='I'){
      i=num(ques,x);
      fi=1;
    }
    if(ques[x-1]=='U'){
      u=num(ques, x);
      fu=1;
    }


  }
}
printf("Problem #%d\n", org-n);
if(!fp){
  p=u*i;
  printf("P=%.2fW\n", p);
} 
if(!fi){
  if(u!=0)
    i=p/u;
  else
    i=0;
  printf("I=%.2fA\n", i);
}
if(!fu){
  if (i!=0)
    u=p/i; 
  else
    u=0;
  printf("U=%.2fV\n",u );
}
printf("\n");

  }
  return 0;

}

10361 - Automatic Poetry

“Oh God”, Lara Croft exclaims, “it’s one of these dumb riddles again!”

In Tomb Raider XIV, Lara is, as ever, gunning her way through ancient Egyptian pyramids, prehistoric caves and medival hallways. Now she is standing in front of some important Germanic looking doorway and has to solve a linguistic riddle to pass. As usual, the riddle is not very intellectually challenging.

This time, the riddle involves poems containing a “Schuttelreim”. An example of a Schuttelreim is the following short poem:

Ein Kind halt seinen Schnabel nur,wenn es hangt an der Nabelschnur.

/German contestants please forgive me. I had to modify something as they were not appearing correctly in plain text format/

A Schuttelreim seems to be a typical German invention. The funny thing about this strange type of poetry is that if somebody gives you the first line and the beginning of the second one, you can complete the poem yourself. Well, even a computer can do that, and your task is to write a program which completes them automatically. This will help Lara concentrate on the “action” part of Tomb Raider and not on the “intellectual” part.Input

The input will begin with a line containing a single number n. After this line follow n pairs of lines containing Schuttelreims. The first line of each pair will be of the forms1<s2>s3<s4>s5

where the si are possibly empty, strings of lowercase characters or blanks. The second line will be a string of lowercase characters or blanks ending with three dots “...”. Lines will we at most 100 characters long.Output

For each pair of Schuttelreim lines l1 and l2 you are to output two lines c1 and c2 in the following way: c1 is the same as l1 only that the bracket marks “<” and “>” are removed. Line c2 is the same as l2 , except that instead of the three dots the string s4s3s2s5 should appear.

Sample Input

3
ein kind haelt seinen <schn>ist>ur
wenn es haengt an der ...
weil wir zu spaet zur <>oma <k>amen
verpassten wir das ...
<d>u <b>ist
...

Sample Output

ein kind haelt seinen schnabel nur
wenn es haengt an der nabel schnur
weil wir zu spaet zur oma kamen
verpassten wir das koma amen
du bist
bu dist

code

#include <stdio.h>
#include <string.h>
int main(){
  char a[200], b[200] , c[200], s1[200], s2[200], s3[200], s4[200],s5[200];
  int n ,flag;
  scanf("%d", &n);
  getchar();
  while(n--){
memset(a, '\0', sizeof(a));
memset(b, '\0', sizeof(b));
memset (c, '\0', sizeof(c));
memset(s1, '\0', sizeof(s1));
memset(s2, '\0', sizeof(s2));
memset(s3, '\0', sizeof(s3));
memset(s4, '\0', sizeof(s4));
memset(s5, '\0', sizeof(s5));

gets(a); gets(b);
flag=1; 
int i2=0, i3=0, i4=0, i5=0;
for (int i=0; i<strlen(a); i++){
  if(a[i]=='<'||a[i]=='>')
    flag++;
  else{
    if(flag==2){
      s2[i2]=a[i];
      i2++;
    }
    if(flag==3){
      s3[i3]=a[i];
      i3++;
    }
    if(flag==4){
      s4[i4]=a[i];
      i4++;
    }
    if(flag==5){
      s5[i5]=a[i];
      i5++;
    }
    putchar(a[i]);
  }

}
putchar('\n');
for(int i=0; i<strlen(b); i++){
  if(b[i]!='.')
    putchar(b[i]);
  if(b[i]=='.')
    break;
}
printf("%s",s4); printf("%s", s3); printf("%s", s2); printf("%s", s5);
putchar('\n');
  }
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章