Monday, October 29, 2007

Kumpulan Jawaban

Jawaban Tugas Kelas A


Jawaban Unguided Kelas A


Jawaban Unguided Kelas C

Jawaban Unguided Kelas C

//file header.h
#include
#include
#include
#include

#define Max 5

typedef int address;
typedef int infotype;
typedef char string[20];
typedef struct
{
string strNama;
infotype umur;
}Anak;
typedef struct
{
Anak Element[Max];
address Head;
address Tail;
}Queue;

void createEmpty(Queue *Q);
int isEmpty(Queue Q);
int isFull(Queue Q);
int isOneElmt(Queue Q);
void Add(Queue *Q,Anak data);
void Del(Queue *Q);
void PrintQueue(Queue Q);


//file function.c
#include"header.h"

void createEmpty(Queue *Q)
{
(*Q).Head=(*Q).Tail=-1;
}

int isEmpty(Queue Q)
{
return(Q.Head==-1 && Q.Tail==-1);
}

int isFull(Queue Q)
{
return((Q.HeadQ.Tail && Q.Head-Q.Tail==1));
}

int isOneElmt(Queue Q)
{
return(Q.Head==Q.Tail && Q.Head!=-1);
}

void Add(Queue *Q,Anak data)
{
if(!isFull(*Q))
{
if(isEmpty(*Q))
{
(*Q).Head=(*Q).Tail=0;
(*Q).Element[(*Q).Head]=data;
}
else
{
if((*Q).Tail==Max-1)
(*Q).Tail=0;
else
(*Q).Tail++;
(*Q).Element[(*Q).Tail]=data;
}
}
else
printf ("Antrian penuh\n");
}

void Del(Queue *Q)
{
int bantu=(*Q).Head;
if(!isEmpty(*Q))
{
if(isOneElmt(*Q))
createEmpty(&(*Q));
else
{
if((*Q).Head==Max-1)
(*Q).Head=0;
else
(*Q).Head++;
}
printf("\nData yang dihapus");
printf("\nNama : %s",(*Q).Element[bantu].strNama);
printf("\nUmur : %d",(*Q).Element[bantu].umur);
}
}

void PrintQueue(Queue Q)
{
address i;
if(!isEmpty(Q))
{
if(Q.Head<=Q.Tail)
{
for(i=Q.Head;i<=Q.Tail;i++)
printf("%s/(%d tahun) ",Q.Element[i].strNama,Q.Element[i].umur);
}
else
{
for(i=Q.Head;i<=Max-1;i++)
printf("%s/(%d tahun) ",Q.Element[i].strNama,Q.Element[i].umur);
for(i=0;i<=Q.Tail;i++)
printf("%s/(%d tahun) ",Q.Element[i].strNama,Q.Element[i].umur);
}
}
else
printf("Queue kosong");
printf("\n");
}


//file main.c
#include"header.h"

main()
{
Queue Q[4];
Anak Data;
int i,iPilih,iHapus;
for(i = 0;i < 4;i++)
createEmpty(&Q[i]);
do
{
system("cls");
printf("MENU\n");
printf("1.Add Queue\n");
printf("2.Del Queue\n");
printf("3.Print Queue\n");
printf("0.Keluar\n");
printf("Pilihan: ");
scanf("%d",&iPilih);
switch(iPilih)
{
case 1:
fflush(stdin);
printf("Masukkan Nama = ");
gets(Data.strNama);
printf("Masukkan Umur = ");
scanf("%d",&Data.umur);
if(Data.umur >= 7 && Data.umur <= 15)
{
if(Data.umur == 7 || Data.umur == 8)
Add(&Q[0],Data);
else if(Data.umur == 9 || Data.umur == 10)
Add(&Q[1],Data);
else if(Data.umur == 11 || Data.umur == 12)
Add(&Q[2],Data);
else
Add(&Q[3],Data);
}
else
printf("Maaf lomba hanya untuk anak umur 7 tahun sampai 15 tahun\n");

for(i = 0;i < 4;i++)
{
printf("Loket %d: ",i+1);
PrintQueue(Q[i]);
}
getch();
break;
case 2:
printf("Masukkan loket yang antriannya ingin dihapus = ");
scanf("%d",&iHapus);
Del(&Q[iHapus-1]);
getch();
break;
case 3:
for(i = 0;i < 4;i++)
{
printf("Loket %d: ",i+1);
PrintQueue(Q[i]);
}
getch();
break;
}
}while(iPilih!=0);
}

Kumpulan Jawaban

Jawaban Tugas Kelas A


Jawaban Unguided Kelas A


Jawaban Unguided Kelas C

Jawaban Unguided Kelas A

//file header.h
#include
#include
#include
#include
#define MaxEl 10 //maksimal isi antrian

typedef int address;
typedef struct{
char Elmt[MaxEl]; //tempat menyimpan isi antrian
address Head;
address Tail;
}Queue;

typedef struct{
Queue antrian[2];
}Queues;

void CreateEmpty(Queue *Q);
int IsEmpty(Queue Q);
int IsFull(Queue Q);
int IsOneElmt(Queue Q);
void Add(Queues *Q,char C); //memasukkan data-data ke antrian
void Del(Queue *Q); //hapus data di head
void PrintQueue(Queues Q);
int CekChar(char C); //mengecek karakter berupa angka,huruf atau lainnya
void JmlChar(Queues Q); //menghitung jumlah isi tiap antrian
int Search(Queues Q,char X);

//file function.c
#include"header.h"

void CreateEmpty(Queue *Q)
{
(*Q).Head=(*Q).Tail=-1;
}

int IsEmpty(Queue Q)
{
return(Q.Head==-1 && Q.Tail==-1);
}

int IsFull(Queue Q)
{
return((Q.HeadQ.Tail && Q.Head-Q.Tail==1));
}

int IsOneElmt(Queue Q)
{
return(Q.Head==Q.Tail && Q.Head!=-1);
}

void Add(Queues *Q,char C)
{
int j=CekChar(C);
if(j!=-1)
{
if(!IsFull((*Q).antrian[j]))
{
if(IsEmpty((*Q).antrian[j]))
{
(*Q).antrian[j].Head=(*Q).antrian[j].Tail=0;
(*Q).antrian[j].Elmt[(*Q).antrian[j].Tail]=C;
if(j==0)
printf("%c masuk antrian angka\n",C);
else
printf("%c masuk antrian huruf\n",C);
}
else
{
if((*Q).antrian[j].Tail==MaxEl-1)
(*Q).antrian[j].Tail=0;
else
(*Q).antrian[j].Tail++;
(*Q).antrian[j].Elmt[(*Q).antrian[j].Tail]=C;
if(j==0)
printf("%c masuk antrian angka\n",C);
else
printf("%c masuk antrian huruf\n",C);
}
}
else //penuh, hapus data di head
printf("Antrian sudah penuh");
}
else
printf("Karakter bukan merupakan huruf maupun angka\n");
}

void Del(Queue *Q)
{
char temp=(*Q).Elmt[(*Q).Head];
if(!IsEmpty(*Q))
{
if(IsOneElmt(*Q))
CreateEmpty(&(*Q));
else
{
if((*Q).Head==MaxEl-1)
(*Q).Head=0;
else
(*Q).Head++;
}
printf("Elemen yang dihapus:%c\n\n",temp);
}
}

void PrintQueue(Queues Q)
{
address i,j;
for(j=0;j<2;j++) j="="0)" i="Q.antrian[j].Head;i<="Q.antrian[j].Tail;i++)" i="Q.antrian[j].Head;i<="MaxEl-1;i++)" i="0;i<="Q.antrian[j].Tail;i++)">=48 && C<=57) return 0; //angka else if((C>=65 && C<=90) || (C>=97 && C<=122)) return 1; //huruf else return -1; } void JmlChar(Queues Q) { int i,j,jml; for(j=0;j<2;j++) { jml=0; if(!IsEmpty(Q.antrian[j])) { if(Q.antrian[j].Head<=Q.antrian[j].Tail) { for(i=Q.antrian[j].Head;i<=Q.antrian[j].Tail;i++) jml=jml+1; } else { for(i=Q.antrian[j].Head;i<=MaxEl-1;i++) jml=jml+1; for(i=0;i<=Q.antrian[j].Tail;i++) jml=jml+1; } } if(j==0) printf("\nLoket angka berisi: %d karakter",jml); else printf("\nLoket huruf berisi: %d karakter",jml); } }


//file main.c
#include"header.h"

void main()
{
Queues Q;
char string[30];
int pil,i,pjg;
for(i=0;i<2;i++)
CreateEmpty(&(Q.antrian[i]));
do{
system("cls");
puts("Menu:");
puts("1.Isi Queue");
puts("2.Print Queue");
puts("3.Jumlah karakter tiap antrian");
puts("0.Keluar");
printf("Pilihan: ");
scanf("%d",&pil);
switch(pil)
{
case 1:{
puts("\nMasukkan string: ");
scanf("%s",&string);
pjg=strlen(string);
for(i=0;i
Add(&Q,string[i]);
getch();
}break;
case 2:{
printf("\nIsi dari antrian:");
PrintQueue(Q);
getch();
}break;
case 3:{
printf("\nJumlah karakter tiap antrian: ");
JmlChar(Q);
getch();
}break;

}
}while(pil!=0);
}

Jawaban Tugas Kelas A

//file header.h
#include
#include
#include
#include

#define Max 10

typedef int address;
typedef int infotype;
typedef struct
{
infotype T[Max];
address Head;
address Tail;
}Queue;

void createEmpty(Queue *Q);
int isEmpty(Queue Q);
int isFull(Queue Q);
int isOneElmt(Queue Q);
void Add(Queue *Q,infotype val);
void Del(Queue *Q);
int panjangAntrian(Queue Q);
void PrintQueue(Queue Q);


//file function.c
#include"header.h"

void createEmpty(Queue *Q)
{
(*Q).Head=(*Q).Tail=-1;
}

int isEmpty(Queue Q)
{
return(Q.Head==-1 && Q.Tail==-1);
}

int isFull(Queue Q)
{
return((Q.HeadQ.Tail && Q.Head-Q.Tail==1));
}

int isOneElmt(Queue Q)
{
return(Q.Head==Q.Tail && Q.Head!=-1);
}

void Add(Queue *Q,infotype val)
{
if(!isFull(*Q))
{
if(isEmpty(*Q))
{
(*Q).Head=(*Q).Tail=0;
(*Q).T[(*Q).Head]=val;
}
else
{
if((*Q).Tail==Max-1)
(*Q).Tail=0;
else
(*Q).Tail++;
(*Q).T[(*Q).Tail]=val;
}
}
else
printf ("Antrian penuh\n");
}

void Del(Queue *Q)
{
//infotype temp=(*Q).T[(*Q).Head];
if(!isEmpty(*Q))
{
if(isOneElmt(*Q))
createEmpty(&(*Q));
else
{
if((*Q).Head==Max-1)
(*Q).Head=0;
else
(*Q).Head++;
}
// printf("Elemen yang dihapus :%c",temp);
}
else
printf("Antrian kosong\n");
}

int panjangAntrian(Queue Q)
{
int iPanjang = 0,i;
if(!isEmpty(Q))
{
if(Q.Head<=Q.Tail)
{
for(i=Q.Head;i<=Q.Tail;i++)
iPanjang++;
}
else
{
for(i=Q.Head;i<=Max-1;i++)
iPanjang++;
for(i=0;i<=Q.Tail;i++)
iPanjang++;
}
}
return iPanjang;
}

void PrintQueue(Queue Q)
{
address i;
if(!isEmpty(Q))
{
if(Q.Head<=Q.Tail)
{
for(i=Q.Head;i<=Q.Tail;i++)
printf("%d ",Q.T[i]);
}
else
{
for(i=Q.Head;i<=Max-1;i++)
printf("%d ",Q.T[i]);
for(i=0;i<=Q.Tail;i++)
printf("%d ",Q.T[i]);
}
}
else
printf("Queue kosong");
printf("\n");
}


//file main.c
#include"header.h"

main()
{
Queue Q[4];
int iPilih,iInput = 0,iRandom,i,iTerpendek,iIndeks,iJumlahTerpendek = 0,iTemp[4];
int iHapus,flag = 0,iVIP = 0,iBiasa = 0;
srand((unsigned)time(NULL));
for(i=0;i<4;i++)
createEmpty(&Q[i]);
do
{
system("cls");
printf("==== PROGRAM PEMBELIAN TIKET KONSER ====\n\n");
printf("MENU\n");
printf("1.Add Queue\n");
printf("2.Del Queue\n");
printf("3.Jumlah Pembeli\n");
printf("0.Keluar\n");
printf("Pilihan : ");
scanf("%d",&iPilih);
switch(iPilih)
{
case 1 :
iRandom = rand () % 2;
if(iRandom == 0)
{
printf("Tiket yang dibeli: Tiket Biasa\n");
if(isFull(Q[0]) && isFull(Q[1]) && !isFull(Q[3]))
{
Add(&Q[3],++iInput);
iBiasa++;
}
else
{
if(panjangAntrian(Q[0]) <= panjangAntrian(Q[1]))
{
Add(&Q[0],++iInput);
iBiasa++;
}
else
{
Add(&Q[1],++iInput);
iBiasa++;
}
}
}
else
{
printf("Tiket yang dibeli: Tiket VIP\n");
if(!isFull(Q[2]))
{
Add(&Q[2],++iInput);
iVIP++;
}
else if(!isFull(Q[3]))
{
Add(&Q[3],++iInput);
iVIP++;
}
}

for(i=0;i<4;i++)
{
printf("Loket %d = ",i+1);
PrintQueue(Q[i]);
}
getch();
break;
case 2 :
iJumlahTerpendek = 0;
printf("Antrian loket sebelum penghapusan:\n");
for(i=0;i<4;i++)
{
printf("Loket %d = ",i+1);
PrintQueue(Q[i]);
}

//mengeset queue tidak kosong pertama sebagai queue dengan antrian terpendek
//proses ini digunakan untuk menghindari untuk mengeset queue kosong sebagai queue terpendek
for(i=0;i<4;i++)
{
if(!isEmpty(Q[i]))
{
iTerpendek = panjangAntrian(Q[i]);
iIndeks = i;
break;
}
}

//membandingkan panjang antrian sehingga dapatkan queue dengan antrian terpendek
//variabel iIndeks digunakan untuk menampung indeks queue dengan antrian terpendek
for(i=iIndeks;i<4;i++)
{
if(!isEmpty(Q[i]))
{
if(panjangAntrian(Q[i]) < iTerpendek)
{
iTerpendek = panjangAntrian(Q[i]);
iIndeks = i;
}
}
}

//menghitung banyaknya queue dengan antrian terpendek yang sama, sekaligus menyimpan
//indeks queue yang terpendek tersebut
for(i=0;i<4;i++)
{
if(iTerpendek == panjangAntrian(Q[i]))
{
iTemp[iJumlahTerpendek++] = i;
}
}

//bila iJumlahTerpendek == 1 maka berarti hanya terdapat 1 buah queue terpendek
if(iJumlahTerpendek == 1)
{
printf("\nLoket dengan antrian terpendek: ");
printf("Loket %d\n\n",iIndeks+1);
Del(&Q[iIndeks]);
}
//queue terpendek berjumlah lebih dari 1
else
{
flag = 0;
printf("\nLoket dengan antrian terpendek: ");
for(i=0;i {
printf("\nLoket %d",iTemp[i]+1);
}

do
{
printf("\nPilih loket yang antriannya ingin dihapus: ");
scanf("%d",&iHapus);
//mencari indeks queue terpendek yang sama dengan nilai iHapus
//dari beberapa queue terpendek
for(i=0;i<=iJumlahTerpendek;i++)
{
if(iHapus == iTemp[i]+1)
{
flag = 1;
break;
}
}
}while(flag == 0);
Del(&Q[iHapus-1]);
}

printf("Antrian loket setelah penghapusan:\n");
for(i=0;i<4;i++)
{
printf("Loket %d = ",i+1);
PrintQueue(Q[i]);
}
getch();
break;
case 3:
printf("Jumlah pembeli tiket biasa = %d\n", iBiasa);
printf("Jumlah pembeli tiket VIP = %d\n", iVIP);
getch();
break;
}
}while(iPilih!=0);
}

Wednesday, October 10, 2007

Jawaban The Chosen Sub Matrix

#include
#include
#include
#include
#include
typedef int MatriksInt[20][20];
void buatsubmatrik(MatriksInt x,MatriksInt &matriksbaru,int ukuran2,int baris,int kolom);
int jumlahelemen(MatriksInt x,int ukuran1);
void display(MatriksInt matriks,int ukuran1);
bool cekelemen(int array[10],int x);
void createEmptyArray(int array[10]);
void PrintfArray(int array[10]);

int main()
{
char N;
int hasil=100;
int hasilbaris,hasilkolom;
int baca=0;
int ukuran2=0;
int ukuran1=0;
int baris,kolom;
MatriksInt matriks;
MatriksInt submatriks;
while (scanf( "%d", &N ) != EOF )
{
ukuran1=N;
scanf("%d",&ukuran2);
//printf("%d %d\n",ukuran1, ukuran2);
for(int i=0;i
{
for(int j=0;j
{
scanf("%d",&matriks[i][j]);
}
}
display(matriks,ukuran1);
//buatsubmatrik(matriks,submatriks,ukuran2,4,2);
//display(submatriks,ukuran2);
//printf("Jumlah Elemen =%d",jumlahelemen(submatriks,ukuran2));
for(int i=0;i
{
for(int j=0;j
{
if((ukuran1 - i> ukuran2-1)&&(ukuran1 - j> ukuran2-1))
{
buatsubmatrik(matriks,submatriks,ukuran2,i,j);
//display(submatriks,ukuran2);
//printf("\n");
if(jumlahelemen(submatriks,ukuran2)
{
hasil=jumlahelemen(submatriks,ukuran2);
hasilbaris=i;
hasilkolom=j;
}
}
}
//printf("\n");
}
printf("%d %d \n",hasilbaris+1,hasilkolom+1);
hasil=100;
}
return 0;
}
void buatsubmatrik(MatriksInt x,MatriksInt &matriksbaru,int ukuran2,int baris,int kolom)
{ int i=0,j=0;
for(int bantu=baris;bantu<=ukuran2+baris;bantu++)
{
for(int bantu2=kolom;bantu2<=ukuran2+kolom;bantu2++)
{
matriksbaru[i][j]=x[bantu][bantu2];
j++;
}
j=0;
i++;
}
}
int jumlahelemen(MatriksInt x,int ukuran2)//mereturnkan berapa elemen yang berbeda belum oke
{
int array[10];createEmptyArray(array);
int p=0;
for(int i=0;i
{
for(int j=0;j
{
if(!cekelemen(array,x[i][j]))
{
array[p++]=x[i][j];
}
}
}
return p;
}
bool cekelemen(int array[10],int x) //untuk mengecek elemen apakah ada yang sama di dalam array
{
bool ketemu=false;
for(int j=0;j<10;j++)
{
if((array[j]==x)&& !ketemu)
{
ketemu=true;
}
}
return ketemu;
}
void display(MatriksInt matriks,int ukuran1)
{
for(int i=0;i
{
for(int j=0;j
{
printf("%d ",matriks[i][j]);
}
printf("\n");
}
}
void createEmptyArray(int array[10])
{
for(int j=0;j<10;j++)
{
array[j]=NULL;
}
}
void PrintfArray(int array[10])
{
for(int j=0;j<10;j++)
{
printf("%d ",array[j]);
}
}

The Chosen Sub Matrix

ACM/ICPC Indonesia National Contest 2007 - Qualification

Problem A

The Chosen Sub Matrix

GCC/Visual C++: matrix.cpp Java: matrix.java
Time Limit: 1s


From a given N x N matrix, you should find an M x M sub matrix which has the least distinct element in it. If there are more than one sub matrixes which have the same number of distinct elements then compare each element in descending order and choose one that has the first highest element. If all of distinct elements of all sub matrixes are the same, chose one with the least row index, and then the least column index. The matrix index starts at 1.

For example, given a 4x4 matrix:
3 9 9 9
3 9 9 2
3 9 9 2
2 5 5 2

Then, the possible sub matrixes of 3x3 are:
3 9 9
3 9 9
3 9 9
S1
9 9 9
9 9 2
9 9 2
S2
3 9 9
3 9 9
2 5 5
S3
9 9 2
9 9 2
5 5 2
S4
Sub matrix S1 has 2 distinct elements: 9 and 3;
Sub matrix S2 has 2 distinct elements: 9 and 2;
Sub matrix S3 has 4 distinct elements: 9, 5, 3, and 2;
Sub matrix S4 has 3 distinct elements: 9, 5, and 2.
Sub matrixes are ranked using the rules above and give result as S1, S2, S4, and then S3.
Which means the chosen sub matrix is S1.


Input Specification

The first line of each case contains two integers, N (1<=N<=10) the size of matrix, and M (1<=M<=N) the size of sub matrix to be chosen. In the next N lines, each contains N integers (each separated by a space) that represent the matrix. Each element in the matrix should be between 0 and 9 inclusively.


Output Specification

For each case you should output in a single line, the top-left index (row and column, separated by a single space) of the chosen sub matrix.


Sample Input

4 3
3 9 9 9
3 9 9 2
3 9 9 2
2 5 5 2
10 2
1 5 7 8 2 3 3 3 1 7
2 2 3 6 3 7 3 2 3 1
5 9 3 5 7 0 4 6 9 1
1 0 3 4 2 6 4 3 9 0
7 4 9 9 5 4 6 2 1 5
5 6 9 9 6 6 3 8 0 8
4 3 3 5 2 1 7 6 4 1
6 5 9 5 0 3 1 8 8 6
2 2 2 8 0 1 3 5 9 0
3 6 4 2 3 3 0 2 0 0

Sample Output

1 1
5 3

Jawaban No Pause Telegraph

#include
#include
#include
char Cek(char x[]);
void Cetak(char x[]);
void createEmpty(char x[]);
int main()
{
char N;
int perhatian=0;
int i=0;
char tampung[10];
char hasil[10];
int j=0;
char temp=0;
bool cetak=true;
bool min=true;
createEmpty(hasil);
createEmpty(tampung);
while ( scanf( "%c", &N ) != EOF )
{
if ((int)N != 10)
{
tampung[i++]=N;
if(Cek(tampung)!='O')
{
i=0;
perhatian=0;
hasil[j++]=Cek(tampung);
createEmpty(tampung);
}
else
perhatian++;
}
else
{
i=0;
j=0;
if(perhatian==0)
Cetak(hasil);
else
printf("could not be translated\n");
createEmpty(hasil);
createEmpty(tampung);
perhatian=0;
}
}

return 0;
}

char Cek(char x[])
{
if (strcmp(x,".--")==0) return 'A';
else if(strcmp(x,"-.")==0) return 'B';
else if(strcmp(x,"---")==0) return 'C';
else if(strcmp(x,"..")==0) return 'D';
else if(strcmp(x,"--..")==0) return 'E';
else if(strcmp(x,"--.-")==0) return 'F';
else if(strcmp(x,".-.")==0) return 'G';
else
return 'O';
}

void Cetak(char x[])
{
int i=0;
while(x[i]!=NULL)
{
printf("%c",x[i]);
i++;
}
printf("\n");
}
void createEmpty(char r[])
{
for(int x=0;x<10;x++)
{
r[x]=NULL;
}
}

ACM/ICPC Indonesia National Contest 2007 - Qualification

No Pause Telegraph
GCC/Visual C++: nopause.cpp Java: nopause.java
Time Limit: 1s



Several hundreds years ago in a country, there was a war between the colonialist and the military of the country. In the war, telegraph machine was used to communicate between the country and its allied cities around the country. The Morse code was used to send messages through telegraph machine. In Morse, letters are represented only by "." (dot) and "-" (dash). Between letters there is a pause to avoid mistranslation of letters.

In that era with the popularity of telegraph machine, there was wariness that the enemy could read the messages, if the messages were accidentally sent to them. Being aware of the threat, a machine was created to encode the messages. It was called No Pause Telegraph. This new telegraph machine was similar to the regular machine, only that the machine encoded the messages with no pause between letters.

After doing some researches on old relics, our archeologist has analyzed the code being used successfully. The archeologist so far has found 7 letters that been used on the found relics, those are:

A .-- dot dash dash
B -. dash dot
C --- dash dash dash
D .. dot dot
E --.. dash dash dot dot
F --.- dash dash dot dash
G .-. dot dash dot

Your task is to translate several messages encoded by the machine on the remaining relics.



Input Specification
Each line of input is a code encoded by No Pause Telegraph. The line will consists of only characters of . (dot(s)) and - (dash(es)).



Output Specification
For each line of input, print the least alphabetical possible message in one line. If there is no possible message, print "could not be translated" (without quotes).



Sample Input
.---.-
.---.---..--..--.-.-.



Sample Output
could not be translated
ABCDEFG

Untuk jawaban click disini

Jawaban A Good Programmer

#include
#include
int cekkesamaan(int data[],int index, int P1, int P2);
int main()
{
char N;
int P1,P2;
int bantu;
int data[10];
bool cek=true;
int index=0;
bool kedua=false;
while ( scanf( "%c", &N ) != EOF )
{
P1=N-48;
scanf("%d",&P2);
while(cek)
{
scanf("%c",&N);
if(N==10 && kedua)
{
cek=false;

}
else
{
if(N!=10 && N !=32)
data[index++]=N-48;
}
kedua=true;

}
cek=true;
kedua=false;
printf("%d\n",cekkesamaan(data,index,P1,P2));
index=0;
}
return 0;
}

int cekkesamaan(int data[],int index, int P1, int P2)
{
int nP1=0;int nP2=0;
for(int i=0;i
{
for(int j=i+1;j
{
if(abs(data[i]-data[j])==P1)
{
nP1++;
}
else if(abs(data[i]-data[j])==P2)
{
nP2++;
}
else
{

}
}
}
if(nP1>nP2)
return nP1;
else
return nP2;
}

A Good Programmer

Time Limit: 1s


A good programmer must be good with numbers. That's why we would like to test your skill in numbers. Write a program to count how many pairs could be found where the differences between numbers in a pair is P.

For example, given the list of numbers : 4 8 1 12 5

If the requested different (P), is 4, then your program should give 3 as the output, where the pairs are 4-8, 8-12, and 1-5. Same numbers could be showed up more than once on the list. In this case, the numbers should be considered as different numbers.


Input Specification
The input file consists of several test cases. First line of each test case contains two integers: N (1 <= N <= 10,000) indicating the number of items in the list, and P (1 <= P <= 10,000) indicating the requested different. The second line contains N integer(s) t1, t1, t2, ...tn (1 <= ti <= 100,000,000) which shows the list of numbers.


Output Specification
For each test case, print in a single line a number of distinct pairs where the difference between numbers in pair is P.


Sample Input
3 2
3 1 6
5 4
4 8 1 12 5
4 3
4 1 1 4


Sample Output
1
3
4


Source: BNPC-HS 2006, Final Round.

Untuk jawabannya click disini

Jawaban Avoiding Financial Nightmare

Jawaban saya (he,,,,mohon petunjuk dari pembaca, soalnya saya masih pemula dalam soal mogram) :
#include
#include
#include
#include
#include
float pemangkatan(float i, int n);
void hitungpembayaran(int besarpeminjaman, int lamapeminjaman, float bunga);
int main()
{
int besarpeminjaman, lamapeminjaman, bunga, N;
float i;
while (scanf( "%d", &N ) != EOF )
{
besarpeminjaman=N;
scanf("%d",&lamapeminjaman);
scanf("%d",&bunga);
i=bunga;
i=i/100;
hitungpembayaran(besarpeminjaman,lamapeminjaman,i);
}

}
void hitungpembayaran(int besarpeminjaman, int lamapeminjaman, float bunga)
{
float A;
float B;
int D;
A= bunga*besarpeminjaman*(pemangkatan((1+bunga),lamapeminjaman));
B= pemangkatan((1+bunga),lamapeminjaman)-1;
A=A/B;
D=A;
if(A-0.5>=D)
printf("%d \n",D+1);
else
printf("%d \n",D);
}

float pemangkatan(float i, int n)
{
if(n==0)
return 1;
else if(n==1)
return i;
else
{
return i*pemangkatan(i,n-1);
}
}

Jawaban UTS

Beginilah jawaban saya
he,,,,mohon petunjuk dari pembaca, soalnya saya masih pemula dalam soal mogram
//file Header.h
#include
#include
#include
#include
#define MAX_STACK 20
#define MAX_HEIGHT 10
typedef char String[5];
typedef struct
{
float panjang;
float lebar;
float tinggi;
String ID;
}Kotak;

typedef struct
{
int top;
Kotak content[MAX_STACK];
}StackKotak;

void CreateStack(StackKotak *S);
int IsEmpty(StackKotak S);
int IsEmptyKotak(Kotak k);
int IsFull(StackKotak S);
void PUSH(StackKotak *S, Kotak D);
Kotak POP(StackKotak *S);
float getTotalTinggi(StackKotak S);
float getVolume(Kotak D);
void ShowAll(StackKotak S);


//file function.c
#include "Header.h"
void CreateStack(StackKotak *S)
{
(*S).top = -1;
}
int IsEmpty(StackKotak S)
{
return (S).top==-1;
}
int IsEmptyKotak(Kotak k)
{
return k.lebar==0&&k.panjang==0&&k.lebar==0&&strcmpi(k.ID,"")==0;
}
int IsFull(StackKotak S)
{
return getTotalTinggi(S)==10;
}

float getVolume(Kotak D)
{
return (D.panjang*D.lebar*D.tinggi);
}
float getTotalTinggi(StackKotak S)
{
float temp=0.0;
int i;
for (i=0;i<=(S).top;i++) { temp +=(S).content[i].tinggi; } return (temp); } void PUSH(StackKotak *S, Kotak D) { if(IsEmpty(*S)) { (*S).top++; (*S).content[(*S).top] = D; printf("\nOK..."); } else { if ( (!IsFull(*S) ) && (getTotalTinggi(*S) <= (MAX_HEIGHT - D.tinggi)) && (getVolume((*S).content[(*S).top]) > getVolume(D)))
{
(*S).top++;
(*S).content[(*S).top] = D;
printf("\nOK...");
}
else if (getTotalTinggi(*S) > (MAX_HEIGHT - D.tinggi))
printf ("Stack gagal di masukkan\n karena Tinggi pucuk kotak sudah melebihi 10 meter");
else
printf("Stack gagal di masukkan\n karena volumenya terlalu besar");
}
}
Kotak POP(StackKotak *S)
{
Kotak a;
strcpy(a.ID,"");
a.lebar=0;
a.panjang=0;
a.tinggi=0;
if ((*S).top > -1)
{
(*S).top--;
return (*S).content[(*S).top+1];
}
else
{
return a;
}
}

void ShowAll(StackKotak S)
{
if (S.top == -1)
printf("\nStack kosong...");
else
{
int i;
for(i = S.top; i >= 0; i--)
{
printf("\n ID : %s", (S).content[i].ID);
printf("\n Panjang : %.2f",(S).content[i].panjang);
printf("\n Lebar : %.2f", (S).content[i].lebar);
printf("\n Tinggi : %.2f", (S).content[i].tinggi);

printf("\n");
}

}
}

//file function.c
#include "Header.h"

void main()
{
int pilih;
Kotak MyKotak;
StackKotak MyStack;
CreateStack(&MyStack);
do
{
system("cls");
printf("---MENU---");
printf("\n1. Push kotak");
printf("\n2. Pop kotak");
printf("\n3. Show Stack");
printf("\n\n Masukkan Pilihan : ");
pilih = getche();
switch (pilih)
{
case '1' :
{
printf("\nMasukkan ID kotak : ");scanf("%s",(MyKotak.ID));
printf("\nMasukkan Panjang kotak (Meter): ");scanf("%f",&MyKotak.panjang);
printf("\nMasukkan Lebar kotak (Meter): ");scanf("%f",&MyKotak.lebar);
printf("\nMasukkan Tinggi kotak (Meter): ");scanf("%f",&MyKotak.tinggi);
PUSH(&MyStack, MyKotak);
getch();
break;
}
case '2' :
{
MyKotak=POP(&MyStack);
if(!IsEmptyKotak(MyKotak))
{
printf("\nKotak yang di-pop : ");
printf("\n ID : %s", MyKotak.ID);
printf("\n Panjang : %.2f",MyKotak.panjang);
printf("\n Lebar : %.2f", MyKotak.lebar);
printf("\n Tinggi : %.2f", MyKotak.tinggi);
}
else
{
printf("\n Stack kosong...");
}
getch();
break;
}
case '3':
{
ShowAll(MyStack);
getch();
break;
}
}
} while (pilih != 27);
}

Friday, October 5, 2007

A Good Programmer

A Good Programmer

Time Limit: 1s


A good programmer must be good with numbers. That's why we would like to test your skill in numbers. Write a program to count how many pairs could be found where the differences between numbers in a pair is P.

For example, given the list of numbers : 4 8 1 12 5

If the requested different (P), is 4, then your program should give 3 as the output, where the pairs are 4-8, 8-12, and 1-5. Same numbers could be showed up more than once on the list. In this case, the numbers should be considered as different numbers.


Input Specification
The input file consists of several test cases. First line of each test case contains two integers: NN <= 10,000) indicating the number of items in the list, and P (1 <= P <= 10,000) indicating the requested different. The second line contains N integer(s) t1, t1, t2, ...tn (1 <= ti <= 100,000,000) which shows the list of numbers. Output Specification
For each test case, print in a single line a number of distinct pairs where the difference between numbers in pair is P. (1 <=


Sample Input
3 2
3 1 6
5 4
4 8 1 12 5
4 3
4 1 1 4



Sample Output
1
3
4


Source: BNPC-HS 2006, Final Round.

Untuk jawaban versi saya click disini

Thursday, October 4, 2007

Soal dan jawaban UTS Struktur data

SOAL :

kita di suruh untuk membuat STACK kotak pada suatu gudang diimplementasikan dengan sebuah program menggunakan struktur data array. atribut stack terdiri dari panjang, lebar, tinggi dan identitas dari kotak tersebut. Stack hanya bisa di tumpuki kotak yang volume lebih kecil dari kotak yang ada pada bawahnya, sehingga operasi PUSH mempunyai keterbatasan ini. Operasi POP dapat di lakukan seperti biasa yaitu kotak paling atas. maksimum tumpukan di batasi sebanyak 10 m, yang bearti pucuk dari kotak teratas tidak boleh melebihi 10 m.

Fungsi yang perlu dibuat!

procedure CreateStack(input/output S:Stack), function IsEmpty(S:Stack)->boolean, function IsFull(S:Stack)->boolean, procedure PUSH(input/output S:Stack, ouput D: infotype), dan function POP(input/output S:STACK, input I:indeks)->infotype


untuk jawabannya cllik di sini

ACM/ICPC Indonesia National Contest 2007 - Qualification

Avoiding Financial Nightmare

GCC/Visual C++: avoid.cpp Java: avoid.java
Time Limit: 1s


Nowadays, getting a loan from a bank or financial company has become very popular, either it?s for commercial or personal purposes. If you are a well-managed on your expenses, having a loan from a bank or using credit cards for your expenses could be a good help, otherwise it could be your worst financial nightmare. By considering the risks of paying bills, our professor has decided to get a loan to buy a new house on a pleasant city in Indonesia.

Our professor has considered 3 main variables that will affect his monthly bills:
  • Principal, the remaining amount of the loan
  • Period, the number of months to pay off the loan
  • Rate, the monthly interest rate

    To help their customer, bank or financial company normally offers a fixed amount payment system. Every month the customer should pay a fixed amount of money which consists of two kinds of payment:
    1. Interest Payment.
      Interest Payment = Rate x Remaining Principal.
      The amount of interest payment should be rounded up to the higher nearest integer.

    2. Principal Payment.
      Principal Payment = Total Payment - Interest Payment.
      The previous Principal is to be subtracted with the current Principal Payment to get the current Principal.
    The total monthly payment should be calculated in some way so that the total monthly payment to be paid spread evenly each month, and at the end of the period the remaining Principal is zero or a negative amount nearest to zero (if it?s not possible to reach zero).

    For example, let the professor get a loan in the amount of $42,000 with 5% interest rate per month that should be paid in 5 months.
    Term Payment Principal
    Total Interest Payment Principal Payment
    - - - - $42,000
    1 $9,701 $2,100 $7,601 $34,399
    2 $9,701 $1,720 $7,981 $26,418
    3 $9,701 $1,321 $8,380 $18,038
    4 $9,701 $902 $8,799 $9,239
    5 $9,701 $462 $9,239 $0

    1st term: He should pay $9,701 ($2,100 for interest payment, and $7,601 for principal payment)
    Interest Payment : 5% x $42,000 = $2,100
    Principal Payment : $9,701 - $2,100 = $7,601
    Current Principal : $42,000 - $7,601 = $34,399

    2nd term: He pays $9,701 ($1,702 for interest payment, and $7,981 for principal payment)
    Interest Payment : 5% x $34,399 = $1,720
    Principal Payment : $9,701 - $1,720 = $6,902
    Current Principal : $34,399 - $6,902 = $26,418, and so on.

    Unfortunately, the professor is terrible with financial stuffs. We don?t want him to end up broke, do we? So, let us help him with the calculation on how much money he should spend to pay his monthly bills on the loan. In that way, the professor will be able to buy his new house and who knows that someday we might be invited to visit his house in return to our help.


    Input Specification

    The input contains multiple cases. Each case consists of three integers respectively, N (1<=N<=10,000,000) the initial principal, M (1<=M<=100) the period, and R (0<=R<=100) the percent rate.

    Output Specification

    For each case, you should output in a single line the total monthly payment should be made to satisfy the condition.


    Sample Input

    42000 5 5
    100000 10 10

    Sample Output

    9701
    16275
    Click di sini untuk melihat jawaban
  •