Sie sind auf Seite 1von 19

The Blocks Problrm

#include <stdio.h>

#include <stdlib.h>

#define Si(a) scanf("%d", &a)

#define Ss(a) scanf("%s", a)

#define MAX 25

typedef struct block {

struct block *above;

struct block *below;

int table_loc;

int value;

} block;

block *table[MAX];

void put_back(block *a) {

block *b;

while(a->above != NULL) {

b = a->above;

a->above = NULL;

b->below = NULL;

b->table_loc = b->value;

table[b->value] = b;

a = b;

}
}

block *top(struct block *a) {

while(a->above != NULL) a = a->above;

return a;

void stack(block *a, block *b) {

block *top_b = top(b);

block *a_below = a->below;

int table_loc = b->table_loc;

/* Remove stack a from its current position. */

if(a_below != NULL) a_below->above = NULL;

else table[a->value] = NULL;

/* Stack stack a onto the top of stack b. */

top_b->above = a;

a->below = top_b;

/* Update stack reference for block a and blocks above a. */

while(a != NULL) {

a->table_loc = table_loc;

a = a->above;

void print_table(int n) {
block *a;

int i;

for(i = 0; i < n; i++) {

printf("%d:", i);

for(a = table[i]; a != NULL; a = a->above) {

printf(" %d", a->value);

printf("\n");

int main() {

int n, i, v;

char verb[4], prep[4];

block *blocks[MAX];

block *a, *b;

for(i = 0; i < MAX; i++)

table[i] = blocks[i] = NULL;

/* Read number of blocks `n`. */

Si(n);

/* Initialize world with `n` blocks. */

for(i = 0; i < n; i++) {

table[i] = blocks[i] = (block *) malloc(sizeof(block));

blocks[i]->above = NULL;

blocks[i]->below = NULL;
blocks[i]->table_loc = i;

blocks[i]->value = i;

while(1) {

/* Read the verb. */

Ss(verb);

/* Exit when verb is quit. */

if(verb[0] == 'q') break;

/* Read the object. */

Si(v);

a = blocks[v];

/* Read the preposition. */

Ss(prep);

/* Read the complement. */

Si(v);

b = blocks[v];

/* Ignore action if it is illegal. */

if(a->table_loc == b->table_loc) continue;

/* When verb is `move`, put back that are above the object
`a`. */

if(verb[0] == 'm') put_back(a);


/* When preposition is `onto`, put back the blocks that are
above the complement `b`. */

if(prep[1] == 'n') put_back(b);

/* Pile object `a` over complement `b`. */

stack(a, b);

print_table(n);

for(i = 0; i < n; i++)

free(blocks[i]);

return 0;

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

#define s 25

int block[s][s];
int result[s];
int n;

int find(int val,int &pos)


{
int position =0,i,j,flag=1;

for(i=0;i<n && flag;i++)


for(j=0;j<result[i];j++)
if(block[i][j]==val)
{
pos=j;
position=i;
flag=0;
break;
}
return position;
}

void return_to_initial(int row, int p)


{
int val;

for(p;p<result[row];p++)
{
val=block[row][p];
block[val][result[val]]=block[row][p];
result[val]++;
}
}

int main()
{
char input[50],x[10],y[10];
int i,j,a,b,pos1,pos2,p;
//freopen("b.in","r",stdin);

while((scanf("%d\n",&n))==1)
{
/*initialize*/
for(i=0;i<n;i++)
{
block[i][0]=i;
result[i]=1;
}

while(gets(input))
{

sscanf(input,"%s %d %s %d\n",&x,&a,&y,&b);

if(strcmp(x,"quit")==0)
break;

if(a==b|| a>=n || b>=n)


continue;

i=find(a,pos1);
j=find(b,pos2);

if(i==j)
continue;

if(strcmp(x,"move")==0)
{
return_to_initial(i,pos1+1);

result[i]=pos1;
if(strcmp(y,"onto")==0)
{
return_to_initial(j,pos2+1);
result[j]=pos2+1;
}

block[j][result[j]++]=a;
}

else if(strcmp(x,"pile")==0)
{
if(strcmp(y,"onto")==0)
{
return_to_initial(j,pos2+1);
result[j]=pos2+1;
}
for(p=pos1;p<result[i];p++)
block[j][result[j]++]=block[i][p];

result[i]=pos1;
}
}
for(i=0;i<n;i++)
{
printf("%d:",i);

if(!result[i])
{
printf("\n");
continue;
}

for(j=0;j<result[i];j++)
printf(" %d",block[i][j]);
printf("\n");
}
}
return 0;
}

import java.util.Scanner;
public class blockproblem {
int[] cariStack(int n,String str[],String cari){
int x=-1,i=0;
int indx=-1;
int hasil[]=new int[2];
do{
if(str[i]!=null){
x=str[i].indexOf(cari);
}
if(x>=0) indx=i;
i++;
}while((x<0)&&(i<n));
hasil[0]=indx;
hasil[1]=x;
return hasil;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
blockproblem bp=new blockproblem();
int n=s.nextInt();
String st[]=new String[n];
for (int i = 0; i < n; i++) {
st[i]=String.valueOf(i);
}
String cmd="",src,pos,des;
while(!(cmd.equalsIgnoreCase("quit"))){
cmd=s.next();
if(!(cmd.equalsIgnoreCase("quit"))){
src=s.next();
pos=s.next();
des=s.next();
if(cmd.equalsIgnoreCase("move")){
if(pos.equalsIgnoreCase("onto")){
int [] urut=bp.cariStack(n,st,src);
int [] urutdes=bp.cariStack(n,st,des);
if(urut[0]>=0){
if(st[urutdes[0]]==null) st[urutdes[0]] = src;
else st[urutdes[0]] += src;
if(st[urut[0]].length()==src.length()) {
st[urut[0]]=null;
}else if(urut[1]==0){
st[urut[0]]=st[urut[0]].substring(1, st[urut[0]].length());
}else if(urut[1]==st[urut[0]].length()-1){
st[urut[0]]=st[urut[0]].substring(0, st[urut[0]].length()-1);
}else{
st[urut[0]]=st[urut[0]].substring(0, urut[1]-1)
+ st[urut[0]].substring(urut[1]+1, st[urut[0]].length());
}
}
}else if(pos.equalsIgnoreCase("over")) {
int [] urut=bp.cariStack(n,st,src);
int [] urutdes=bp.cariStack(n,st,des);
if(urut[0]>=0){
if(st[urutdes[0]+1]==null) st[urutdes[0]+1] = src;
else st[urutdes[0] + 1] += src;
if(st[urut[0]].length()==src.length()) {
st[urut[0]]=null;
}else if(urut[1]==0){
st[urut[0]]=st[urut[0]].substring(1, st[urut[0]].length());
}else if(urut[1]==st[urut[0]].length()-1){
st[urut[0]]=st[urut[0]].substring(0, st[urut[0]].length()-1);
}else{
st[urut[0]]=st[urut[0]].substring(0, urut[1]-1)
+ st[urut[0]].substring(urut[1]+1, st[urut[0]].length());
}
}
}
}else if(cmd.equalsIgnoreCase("pile")){
if(pos.equalsIgnoreCase("onto")){
if(Integer.parseInt(des)<n){
int [] urut=bp.cariStack(n,st,src);
if(urut[0]>=0){
if(st[Integer.parseInt(des)]==null) st[Integer.parseInt(des)] = st[urut[0]];
else st[Integer.parseInt(des)] += st[urut[0]];
st[urut[0]]=null;
}
}
}else if(pos.equalsIgnoreCase("over")) {
if(Integer.parseInt(des)<n-1){
int [] urut=bp.cariStack(n,st,src);
if(urut[0]>=0){
if(st[Integer.parseInt(des)+1]==null) st[Integer.parseInt(des) + 1] = st[urut[0]];
else st[Integer.parseInt(des) + 1] += st[urut[0]];
st[urut[0]]=null;
}
}
}
}
}
}
for (int i = 0; i < st.length; i++) {
System.out.println(i+":"+st[i]);
}
}
}

import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author Sanchit M. Bhatnagar
* @see http://uhunt.felix-halim.net/id/74004
*
*/
public class P101 {

public static void main(String args[]) // entry point from OS


{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
@SuppressWarnings("unchecked")
ArrayList<Integer>[] blocks = new ArrayList[size];
for (int i = 0; i < size; i++) {
blocks[i] = new ArrayList<Integer>();
blocks[i].add(i);
}

// System.out.println(Arrays.deepToString(blocks));

while (sc.hasNext()) {
String t = sc.next();
if (t.equals("quit")) {
break;
} else {
int a = sc.nextInt();
String x = sc.next();
int b = sc.nextInt();

if (a == b)
continue;

Point posA = findBlock(a, blocks);


Point posB = findBlock(b, blocks);

if (posA.x == posB.x)
continue;

if (t.equals("move")) {
moveBack(posA, blocks);
if (x.equals("onto")) {
moveBack(posB, blocks);
blocks[posA.x].remove(posA.y);
blocks[posB.x].add(a);
} else {
blocks[posA.x].remove(posA.y);
blocks[posB.x].add(a);
}
} else if (t.equals("pile")) {
if (x.equals("onto")) {
moveBack(posB, blocks);
int removed = 0;
int tSize = blocks[posA.x].size();
for (int i = posA.y; i < tSize; i++) {
blocks[posB.x].add(blocks[posA.x].remove(i - removed));
removed++;
}
} else {
int removed = 0;
int tSize = blocks[posA.x].size();
for (int i = posA.y; i < tSize; i++) {
blocks[posB.x].add(blocks[posA.x].remove(i - removed));
removed++;
}
}
}
}
// System.out.println(Arrays.deepToString(blocks));
}

// System.out.println(Arrays.deepToString(blocks));

for (int i = 0; i < size; i++) {


System.out.print(i + ":");
int tSize = blocks[i].size();
for (int j = 0; j < tSize; j++) {
System.out.print(" " + blocks[i].remove(0));
}
System.out.println();
}
sc.close();
}

private static void moveBack(Point posA, ArrayList<Integer>[] blocks) {


int removed = 0;
int tSize = blocks[posA.x].size();
for (int i = posA.y + 1; i < tSize; i++) {
int x = (Integer) blocks[posA.x].remove(i - removed);
removed++;
blocks[x].add(x);
}
}

private static Point findBlock(int a, ArrayList<Integer>[] blocks) {


for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[i].size(); j++) {
if ((Integer) blocks[i].get(j) == a) {
return new Point(i, j);
}
}
}
return null;
}
}

Cost Cutting
#include<stdio.h>
int main()
{
int a,b,c,d,ans,j;
while(scanf("%d",&d)==1)
{
for(j=1;j<=d;j++)
{
scanf("%d%d%d",&a,&b,&c);
if(a>b && b>c || a<b && b<c)
ans=b;
if(a>c && c>b || a<c && c<b)
ans=c;
if(b>a && a>c || b<a && a<c)
ans=a;
printf("Case %d: %d\n",j,ans);
}
}
return 0;
}

Automatic Answer

#include<stdio.h>
int main()
{
int t,i,n,ans,a,m;
while(scanf("%d",&t)==1)
{
i=1;
while(i<=t)
{
scanf("%d",&n);
a=n*315+36962;
m=a/10;
ans=m%10;
if(ans<0)
ans=ans/(-1);
printf("%d\n",ans);
i++;
}
}
return 0;
}

Tex Quotes

#include<stdio.h>
#include<string.h>
int main()
{
char s[100000];
long i,l,p=0;
while(gets(s))
{
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]=='"')
{
p=p+1;
if(p%2==1)
printf("``");
else
printf("''");
}
else
printf("%c",s[i]);
}
printf("\n");

}
return 0;
}

One Two Three

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

int main ()
{
int testCase; scanf ("%d", &testCase);

while ( testCase-- )
{
char a [10]; scanf ("%s", a);

if ( strlen (a) == 5 ) printf ("3\n");


else
{
int cnt = 0;

if ( a [0] == 'o' ) cnt++;


if ( a [1] == 'n' ) cnt++;
if ( a [2] == 'e' ) cnt++;

if ( cnt >= 2 ) printf ("1\n");


else printf ("2\n");
}
}

return 0;
}

List of Conquests
#include<stdio.h>
#include<string.h>
int main()
{
int a,i,j,k;
char cn[2001][30],ld[70];
while(scanf("%d",&a)==1)
{
int count[2001]= {0};
for(i=0; i<a; i++)
{
scanf("%s",cn[i]);
gets(ld);
}
for(i=0; i<a; i++)
for(j=i+1; j<a; j++)
if(strcmp(cn[i],cn[j])==0)
{
count[i]++;
cn[j][0]='\0';
}
for(i=0; i<a-1; i++)
{
for(j=i+1; j<a; j++)
{
if(cn[j][0]=='\0')continue;
if(strcmp(cn[i],cn[j])>0)
{
strcpy(ld,cn[i]);
strcpy(cn[i],cn[j]);
strcpy(cn[j],ld);
k=count[i];
count[i]=count[j];
count[j]=k;
}
}
}
for(i=0; i<a; i++)
{
if(cn[i][0]!='\0')
printf("%s %d\n",cn[i],count[i]+1);
}
}
return 0;
}

One-Two-Three

Source Code:
/**

* @author Quickgrid ( Asif Ahmed )

* @link https://quickgrid.wordpress.com
*/

#include<stdio.h>

#define O 111

#define N 110

#define E 101

static char s[8];

int main(){

register unsigned n;

scanf("%u", &n);

while (n--){

scanf("%s", s);

if (s[3]){

printf("3\n");

}else{

unsigned s0 = s[0];

unsigned s1 = s[1];

unsigned s2 = s[2];

if ((s0 == O && s1 == N) || (s1 == N && s2 == E) || (s0 == O &&


s2 == E))

printf("1\n");

else

printf("2\n");

return 0;

}
#include <stdio.h>

/*Time : 0.004s*/
int main(){
int i,t,len,cnt;
char str[5],one[]={"one"};

scanf("%d",&t);
while(t--){
scanf("%s",str);
for( len = 0; str[len]; len++);
cnt = 0;
if(len == 5)
printf("3\n");
else{
for( i = 0; i < len; i++){
if( one[i] == str[i]){
cnt++;
}
}
if( cnt >= 2)
printf("1\n");
else
printf("2\n");
}
}
return 0;
}

I love big Numbers!

Math, Factorials, Competitve Two

import java.math.BigInteger;
import java.util.Scanner;

class Main {
public static void main (String args[]) {
Scanner scanner = new Scanner(System.in);
int sum[] = new int[1010];

BigInteger factorial = BigInteger.ONE;


for (int i = 1; i <= 1000; i++) {
factorial = factorial.multiply(BigInteger.valueOf(i));
String value = factorial.toString();
for (int j = 0; j < value.length(); j++) {
sum[i] += value.charAt(j) - '0';
}
}
while (scanner.hasNext()) {
System.out.println(sum[scanner.nextInt()]);
}
}
}
input
The Blocks Problem

import
java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

/**
*
* @author ahani
*/
public class Main {
public static int NumberBlocks;
public static Stack<Integer> Blocks[];
public static int Position[];
public static String Line;
public static int A, B;

public static void main(String[] args) throws


IOException{
BufferedReader Input = new BufferedReader(new
InputStreamReader(System.in));
NumberBlocks = Integer.parseInt(Input.readLine());
Blocks = new Stack[NumberBlocks];
Position = new int[NumberBlocks];
for(int i = 0; i < NumberBlocks; i++) {
Blocks[i] = new Stack<Integer>();
Blocks[i].push(i);
Position[i] = i;
}
//for(int i = 0; i < NumberBlocks; i++) {

//}
Line = "";
while(!(Line = Input.readLine()).equals("quit")) {
StringTokenizer token = new
StringTokenizer(Line);
String First = token.nextToken();
A = Integer.parseInt(token.nextToken());
String Second = token.nextToken();
B = Integer.parseInt(token.nextToken());

if(A == B || Position[A] == Position[B])


continue;
if(First.equals("move")) {
if(Second.equals("onto")) {
MoveOnto(A, B);
} else if(Second.equals("over")) {
MoveOver(A, B);
}
} else if(First.equals("pile")) {
if(Second.equals("onto")) {
PileOnto(A, B);
} else if(Second.equals("over")) {
PileOver(A, B);
}
}
}
for(int i = 0; i < Blocks.length; i++)
System.out.println(Solve(i));
}
public static void MoveOnto(int First, int Second) {
ClearAbove(Second);
MoveOver(First, Second);
}
public static void MoveOver(int First, int Second) {
ClearAbove(First);

Blocks[Position[Second]].push(Blocks[Position[First]].pop());
Position[First] = Position[Second];
}
public static void PileOnto(int First, int Second) {
ClearAbove(Second);
PileOver(First, Second);
}
public static void PileOver(int First, int Second) {
Stack<Integer> Pile = new Stack<Integer>();
while(Blocks[Position[First]].peek() != First) {
Pile.push(Blocks[Position[First]].pop());
}
Pile.push(Blocks[Position[First]].pop());
while(!Pile.isEmpty()) {
int Tmp = Pile.pop();
Blocks[Position[Second]].push(Tmp);
Position[Tmp] = Position[Second];
}
}
public static void ClearAbove(int Block) {
while(Blocks[Position[Block]].peek() != Block) {
Intial(Blocks[Position[Block]].pop());
}
}
public static void Intial(int Block) {
while(!Blocks[Block].isEmpty()) {
Intial(Blocks[Block].pop());
}
Blocks[Block].push(Block);
Position[Block] = Block;
}
public static String Solve(int Index) {
String Result = "";
while(!Blocks[Index].isEmpty()) Result = " " +
Blocks[Index].pop() + Result;
Result = Index + ":" + Result;
return Result;
}

Das könnte Ihnen auch gefallen