Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Created December 22, 2014 05:08
Show Gist options
  • Save hyunjun/cbf1082fd0a2994f97d2 to your computer and use it in GitHub Desktop.
Save hyunjun/cbf1082fd0a2994f97d2 to your computer and use it in GitHub Desktop.
[hackerrank] Manasa and Stones
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_STONE_NUM 8
void num_of_stones(int n, int a, int b, char** res) {
int small = a;
int big = b;
if ( b < a ) {
small = b;
big = a;
}
int start = small * (n - 1), end = big * (n - 1), step = big - small;
if ( 0 == step ) {
*res = (char*) malloc(sizeof(char) * MAX_STONE_NUM);
sprintf(*res, "%d", small * (n - 1));
} else {
char* tmp = (char*) malloc(sizeof(char) * MAX_STONE_NUM);
int i = 0, res_pos = 0;
*res = (char*) malloc(sizeof(char) * MAX_STONE_NUM * ((end - start) / step + 1));
for ( i = start; i <= end; i += big - small ) {
//printf("%d\n", i);
memset(tmp, '\0', MAX_STONE_NUM);
sprintf(tmp, "%d", i);
printf("[th tmp] %s\n", tmp);
sprintf(*res + res_pos, "%s ", tmp);
res_pos += strlen(tmp) + 1;
}
*(*res + res_pos - 1) = '\0';
free(tmp);
}
}
int main() {
int num_of_tests = 0;
fscanf(stdin, "%d", &num_of_tests);
int i = 0, n = 0, a = 0, b = 0;
char* res = NULL;
for ( i = 0; i < num_of_tests; ++i ) {
fscanf(stdin, "%d", &n);
fscanf(stdin, "%d", &a);
fscanf(stdin, "%d", &b);
num_of_stones(n, a, b, &res);
printf("%s\n", res);
}
if ( res != NULL )
free(res);
return 0;
}
mport java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static String numOfStones(final int n, final int a, final int b) {
int small = a;
int big = b;
if ( b < a) {
small = b;
big = a;
}
int start = small * (n - 1);
List<Integer> res = new ArrayList<Integer>();
res.add(start);
while ( res.get(res.size() - 1) < big * (n - 1) ) {
res.add(res.get(res.size() - 1) - small + big);
}
StringBuffer sb = new StringBuffer();
sb.append(start);
for ( int i = 1; i < res.size(); ++i )
sb.append(" " + res.get(i));
return sb.toString();
}
public static void main(final String[] args) {
Scanner sc = new Scanner(System.in);
final int numOfTests = Integer.parseInt(sc.nextLine());
for ( int i = 0; i < numOfTests; ++i ) {
int n = Integer.parseInt(sc.nextLine());
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
System.out.println(numOfStones(n, a, b));
}
}
}
// https://www.hackerrank.com/challenges/manasa-and-stones
object Solution {
def numOfStones(n: Int, a: Int, b: Int): String = {
val small = Math.min(a, b)
val big = Math.max(a, b)
/*val start = small * (n - 1)
var res = Array[Int](start)
while ( res(res.length - 1) < big * (n - 1) ) {
res :+ res(res.length - 1) - small + big
}
for ( r <- Range(start = small * (n - 1), end = big * (n - 1), step = big - small) ) {
println(r)
}*/
val stepVal = big - small
if ( 0 == stepVal ) {
(small * (n - 1)).toString
} else {
//Range(start = small * (n - 1), end = big * (n - 1), step = big - small).toString
/*for ( i <- small * (n - 1) to big * (n - 1) by stepVal ) {
println(i)
}*/
(small * (n - 1) to big * (n - 1) by stepVal).mkString(" ")
}
}
def main(args: Array[String]) {
val inps = io.Source.stdin.getLines.drop(1).toArray.map((s: String) => s.toInt)
for ( s <- Range(start = 0, end = inps.length, step = 3) ) {
println(numOfStones(inps(s), inps(s + 1), inps(s + 2)))
}
}
}
def num_of_stones(n, a, b):
a, b = min(a, b), max(a, b)
start = a * (n - 1)
res = [start]
while res[-1] < b * (n - 1):
res.append(res[-1] - a + b)
return ' '.join([str(i) for i in res])
if __name__ == '__main__':
num_of_tests = int(raw_input())
for i in range(num_of_tests):
n = int(raw_input())
a = int(raw_input())
b = int(raw_input())
print num_of_stones(n, a, b)
def num_of_stones(n, a, b):
a, b = min(a, b), max(a, b)
if a == b:
return a * (n - 1)
return ' '.join([str(i) for i in range(a * (n - 1), b * (n - 1) + 1, b - a)])
if __name__ == '__main__':
num_of_tests = int(raw_input())
for i in range(num_of_tests):
n = int(raw_input())
a = int(raw_input())
b = int(raw_input())
print num_of_stones(n, a, b)
ef num_of_stones(n, a, b):
a, b = min(a, b), max(a, b)
start = a * (n - 1)
res = [start]
while res[-1] < b * (n - 1):
res.append(res[-1] - a + b)
return ' '.join([str(i) for i in res])
if __name__ == '__main__':
num_of_tests = int(input())
for i in range(num_of_tests):
n = int(input())
a = int(input())
b = int(input())
print(num_of_stones(n, a, b))
def num_of_stones(n, a, b):
a, b = min(a, b), max(a, b)
if a == b:
return a * (n - 1)
return ' '.join([str(i) for i in range(a * (n - 1), b * (n - 1) + 1, b - a)])
if __name__ == '__main__':
num_of_tests = int(input())
for i in range(num_of_tests):
n = int(input())
a = int(input())
b = int(input())
print(num_of_stones(n, a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment