All pastes #207897 Raw Edit

Indy

public c v1 · immutable
#207897 ·published 2006-10-18 08:46 UTC
rendered paste body
/*ID: indy901PROG: gift1LANG: C*/#include <stdio.h>#include <stdlib.h>int NP;int i;int j;int amount;int people;char name[ 14 ];int* accounts;int* spent;FILE* fp;char** names;int indexByName( char theName[ 14 ] ) {	int k;	int m;	int b;		for ( k = 0; k < NP; k++ ) {		b = 1;		for ( m = 0; m < 14; m++ ) {			if ( names[ k ][ m ] != theName[ m ] )				b = 0;		}		if ( b == 1 )			return k;	}}int main() {	fp = fopen( "gift1.in" , "r" );	fscanf( fp , "%d" , &NP );	accounts = ( int* )malloc( sizeof( int ) * NP );	spent = ( int* )malloc( sizeof( int ) * NP );	for ( i < 0; i < NP; i++ )		accounts[ i ] = spent[ i ] = 0;	names = ( char** )malloc( sizeof( char* ) * NP );	for ( i = 0; i < NP; i++ )		names[ i ] = ( char* )malloc( sizeof( char ) * 14 );	for ( i = 0; i < NP; i++ ) {		for ( j = 0; j < NP; j++ )			names[ i ][ j ] = '\0';	}	for ( i = 0; i < NP; i++ )		fscanf( fp , "%s" , names[ i ] );	for ( i = 0; i < NP; i++ ) {		fscanf( fp , "%s" , &name );		fscanf( fp , "%d %d" , &amount , &people );		if ( people != 0 ) {			amount -= amount % people;			spent[ indexByName( name ) ] += amount;			for ( j = 0; j < people; j++ ) {				fscanf( fp , "%s" , &name );				accounts[ indexByName( name ) ] += amount / people;			}		}	}	fclose( fp );	fp = fopen( "gift1.out" , "w" );	for ( i = 0; i < NP; i++ )		fprintf( fp , "%s %d\n" , names[ i ] , accounts[ i ] - spent[ i ] );	fclose( fp );	free( accounts );	free( spent );	for ( i = 0; i < NP; i++ )		free( names[ i ] );	free( names );	return 0;}