int main(void) { char name[NAMELEN]; FILE *fin, *fout; int i, j, np, amt, ng; Person *giver, *receiver; fin = fopen("gift1.in", "r"); fout = fopen("gift1.out", "w"); fscanf(fin, "%d", &np); assert(np <= MAXPEOPLE); for(i=0; i<np; i++) { fscanf(fin, "%s", name); addperson(name); } /* process gift lines */ for(i=0; i<np; i++) { fscanf(fin, "%s %d %d", name, &amt, &ng); giver = lookup(name); for(j=0; j<ng; j++) { fscanf(fin, "%s", name); receiver = lookup(name); giver->total -= amt/ng; receiver->total += amt/ng; } } /* print gift totals */ for(i=0; i<np; i++) fprintf(fout, "%s %d\n", people[i].name, people[i].total); exit (0); }
int main() { setvbuf (stdout, NULL, _IONBF, 0); Person map[rows][cols]={ {addobject(-3),addperson(1,-2,0,3,6),addperson(2,0,0,6,0),addobject(-4)}, {addobject(-1),addperson(3,-4,0,0,0),addperson(4,5,0,0,0),addobject(-3)}, {addobject(-2),addobject(-2),addperson(5,-3,0,0,0),addobject(-1)} }; enum boolean solved = false; int numberofswaps = 7; int randomlistsize = numberofswaps*4; int randomlist[randomlistsize]; int counter = 0; int counterlimit = 5; while(solved == false && counter < counterlimit){ for(int i = 0; i<randomlistsize; i++){ if (i % 2 == 0){ randomlist[i] = rand() % rows; } else{ randomlist[i] = rand() % cols; while(map[randomlist[i-1]][randomlist[i]].canmove == false){ randomlist[i-1] = rand() % rows; randomlist[i] = rand() % cols; } } } for(int i =0; i<randomlistsize;i++){ printf("%d",randomlist[i]); } // now random swapping for(int i = 0; i<randomlistsize; i+=4){ Person temp = map[i][i+1]; map[i][i+1] = map[i+2][i+3]; map[i+2][i+3] = temp; } // evaluate Person map2[rows][cols]={ {addobject(-3),addperson(2,0,0,6,0),addperson(3,-4,0,0,0),addobject(-4)}, {addobject(-1),addperson(4,5,0,0,0),addperson(5,-3,0,0,0),addobject(-3)}, {addobject(-2),addobject(-2),addperson(1,-2,0,3,6),addobject(-1)} }; int evaluation = evaluatemap(rows, cols, map2); if(evaluation == true){ printf("Done at %d iterations",counter); } solved = evaluation; if(counter == counterlimit -1){ printf("Failed"); } counter++; } return EXIT_SUCCESS; }