-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowers2_final.cpp
More file actions
55 lines (49 loc) · 1.4 KB
/
powers2_final.cpp
File metadata and controls
55 lines (49 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Project : Programming Languages 1 - Assignment 1 - Exercise 1
Author : Eleni-Elpida Kapsali (eleni_kaps@hotmail.com)
Date : April 29, 2020
Description : Powers2 (C++ code)
------------
School of ECE, National Technical University of Athens
*/
//Source: https://www.geeksforgeeks.org/represent-n-as-the-sum-of-exactly-k-powers-of-two-set-2/
#include<stdio.h>
#include<stdlib.h>
using namespace std;
//This function calculates log2(x)
int log2 (int x){
if(x > 1) return 1 + log2(x / 2);
else return 0;
}
int main(int argc, char* argv[]){
FILE *fptr;
fptr = fopen(argv[1], "r");
if(fptr == NULL) printf("Error in opening file");
else{
int T, K, N;
fscanf(fptr, "%d", &T);
for(int i = 0; i < T; i++){
fscanf(fptr, "%d", &N);
fscanf(fptr, "%d", &K);
int powers[K], sumpow = K;
for(int j = 0; j < K; j++) powers[j] = 1;
for(int j = (K-1); j >= 0; --j){
while(sumpow + powers[j] <= N){
sumpow += powers[j];
powers[j] *= 2;
}
}
if(sumpow != N) printf("[]\n");
else{
int size = log2(powers[K-1]) + 1;
int powers2[size];
for(int i = 0; i < size; i++) powers2[i] = 0;
for(int i = 0; i < K; i++) powers2[log2(powers[i])]++;
printf("[");
for(int i = 0; i < (size - 1); i++) printf("%d,", powers2[i]);
printf("%d]\n", powers2[size-1]);
}
}
}
return 0;
}