Jawaban Struktur Data
Soal No 1
/**
* by Nama Anda
* NIM Anda
*/
#include<iostream>
using namespace std;
void pemnjumlahanSubset(int arr[],int l,int r, int sum){
if(l>r){
cout<<sum<<" ";
return;
}
pemnjumlahanSubset(arr,l+1,r,sum+arr[l]);
pemnjumlahanSubset(arr,l+1,r,sum);
}
int main(){
int n;
cin>>n;
int arr[n];
for( int i =0 ;i<n;i++){
cin>>arr[i];
}
pemnjumlahanSubset(arr,0,n-1,0);
return 0;
}
________________________________________________________
Soal No 2
/**
* by Nama Anda
* NIM Anda
*/
#include<iostream>
using namespace std;
int mundur(int a){
// a >= 0
cout<<a<<endl;
if(a>0)
mundur(a-1);
else
return 0;
}
int main(){
int x;
cin>>x;
mundur(x);
}
________________________________________________________
Soal No 3
#include <iostream>
using namespace std;
void swap(int *x,int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int a,b;
cin>>a>>b;
swap(a,b);
cout<<a<<" "<<b;
}
________________________________________________________
Soal No 4
#include <iostream>
using namespace std;
int pangkat(int *x,int *y){
if(*y==0){
return 1;
}else{
*y-=1;
return (*x)*pangkat(x,y);
}
}
int main(){
int x,y;
cin>>x>>y;
cout<<pangkat(&x,&y)<<endl;
}
________________________________________________________
Soal No 5
#include <iostream>
using namespace std;
void balik (int *A){
if(*A<10){
*A = *A * 10;
} else {
*A= (*A%10) * 10 + *A/10;
}
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void Urut (int a[], int N){
/*gunakan algoritma sorting*/
int i, j, min_id;
for (i = 0; i < N-1; i++)
{
min_id = i;
for (j = i+1; j < N; j++)
if (a[j] < a[min_id])
min_id = j;
swap(&a[min_id], &a[i]);
}
}
int main (){
int N;
cin >> N;
int X[N], i=0;
while (i < N){
cin >> X[i];
balik(&X[i]);
i++;
}
Urut(X, N);
i=0;
while (i < N){
balik(&X[i]);
cout << X[i] <<" ";
i++;
}
return 0;
}
Comments
Post a Comment