Here's my ramdomize code:
int monstername;
monstername = rand() % 3;
but I want 0 with 70% of output 1 with 20% 2 with 10% how I can do this with C?
int monstername;
int random_var = rand() % 10;
if(random_var < 7) {
// 0-70%
monstername = 0;
}
else if(random_var < 9) {
// 70-90% here
monstername = 1;
}
else {
// 90-100% here
monstername = 2;
}
Try
int monstername;
int rn = rand();
if (rn < 0.7*RAND_MAX)
monstername = 0;
else if (rn < 0.9*RAND_MAX)
monstername = 1;
else
monstername = 2;
Assuming you don't care about modulo bias, you want something like:
int monstername;
int r = rand() % 10;
if (r < 7)
monstername = 0;
else if (r < 9)
monstername = 1;
else
monstername = 2;
If you do care about modulo bias, look into arc4random_uniform(3) instead of rand(3).
int weighted[10] = {0,1,0,0,1,0,0,2,0,0};
monstername = weighted[rnd() % 10];
Related
here's the sample of the input and the output
What's wrong with my code? The output on my Dev-C++ is alright, no problem, but when I submitted it to an online judge it turns out as wrong answer. I don't know what's the problem, and I prefer to not change most of them (because this code turns out from my difficult brain, I'm sorry). But any help would be appreciated, thank you.
#include <stdio.h>
int main () {
int T = 0, max = 0, X, sum = 0;
scanf ("%d", &T);
long long int N = 0;
//2891
for (int i = 0; i < T; i++) {
sum = 0;
scanf ("%lld", &N);
int A = N;
while (A / 10 != 0) {
A = A / 10;
max++;
}
int length = max;
for (int j = length; j >= 0; j--) {
int X = N % 10; //1
sum += X;
int Xx = N / 10; //289
int Y = Xx % 10; //9
int Yy = Y * 2; //18
if (Yy > 9) {
Yy -= 9; //9
}
sum += Yy;
N = Xx / 10;
}
if (sum % 10 == 0) {
printf ("PASS\n");
} else printf ("FAIL\n");
}
return 0;
}
I have a problem with output of the following C program. It does not get me the correct output.I transform this program from the sum of two big numbers.
So I get the output with some "errors" like "232423-3-4-34--3424" instead of "23242334343424". How do i fix this?
#include<stdio.h>
int main() {
int num1[255], num2[255], dif[255];
char s1[255], s2[255];
int l1, l2;
printf("Enter Number1:\n");
scanf("%s", &s1);
printf("Enter Number2:\n");
scanf("%s", &s2);
for (l1 = 0; s1[l1] != '\0'; l1++)
num1[l1] = s1[l1] - '0';
for (l2 = 0; s2[l2] != '\0'; l2++)
num2[l2] = s2[l2] - '0';
int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++) {
dif[k] = (num1[i] - num2[j] - carry) % 10;
carry = (num1[i] - num2[j] - carry) / 10;
}
if (l1 > l2) {
while (i >= 0) {
dif[k++] = (num1[i] - carry) % 10;
carry = (num1[i--] - carry) / 10;
}
} else if (l1 < l2) {
while (j >= 0) {
dif[k++] = (num2[j] - carry) % 10;
carry = (num2[j--] - carry) / 10;
}
} else {
if (carry > 0)
dif[k++] = carry;
}
printf("Result:");
for (k--; k >= 0; k--)
printf("%d", dif[k]);
return 0;
}
Your result includes negative numbers because the code doesn't correctly implement modulo-10 arithmetic. In lines like this:
dif[k] = (num1[i] - num2[j] - carry) % 10;
If subtraction num1[i] - num2[j] - carry is less than 0, you want to store the result of 10-subtraction. There are languages where the % operator works like that in, but in C it returns a negative number, so -1 % 10 yields -1 and not 9.
To fix the issue, the code needs to be more explicit, e.g.:
int sub = num1[i] - num2[j] - carry;
if (sub >= 0) {
dif[k] = sub;
carry = 0;
} else {
dif[k] = 10 - sub;
carry = 1;
}
I have to print numbers with max N bits where count of bits set to 1 = count of bits set to 0. I ignoring leading zeros. I thinking that this applies only when count of bits is even.
My code:
int power(k) {
return 1 << k;
}
void print_numbers(int n){
n -= (n % 2); // FOR EVEN COUNT OF BITS
int exp = 1; // EXPONENTS WILL BE ODD (2^1, 2^3, 2^5, ...)
while (exp < n) {
int start = power(exp);
int end = power(exp + 1);
int ones = (exp + 1) / 2; // ALLOWED COUNT OF 1
for (int i = start; i < end; i++) {
int bits_count = 0;
for (int j = 0; j <= exp; j++){ // CHECK COUNT OF 1
bits_count += ((i >> j) & 1);
}
if (bits_count == ones){
printf("%d\n", i);
}
}
exp += 2;
}
For N = 12 this function print 637 numbers. Is this solution correct or am i wrong? Any idea for more efficient or better solution?
I came up with this, which is a totally different approach (and perfectible) but works:
#include <stdio.h>
void checker(int number)
{
int c;
int zeros = 0;
int ones = 0;
for (c = 31; c >= 0; c--)
{
if (number >> c & 1)
{
ones++;
}
else if(ones > 0)
{
zeros++;
}
}
if(zeros == ones)
{
printf("%i\n", number);
}
}
int main()
{
int c;
for (c = 4095; c >= 0; c--)
{
checker(c);
}
return 0;
}
Which get me 638 values (including 0)
I have written this program in Lua and C which is a greedy algorithm.
Greedy = function(num)
local q = 0;
local d = 0;
local n = 0;
local p = 0;
local x = 1;
while x == 1 do
if (num >= 0.25) then
q = q + 1;
num = num - 0.25;
print("q");
elseif (num >= 0.10) then
d = d + 1;
num = num - 0.10;
print("d");
elseif (num >= 0.05) then
n = n + 1;
num = num - 0.05;
print("n");
elseif (num >= 0.01) then
p = p + 1;
num = num - 0.01;
print("p");
end
if (num == 0) then
x = 0;
end
end
if (x == 0) then
local all = q+d+n+p;
print(all);
end
end
Greedy(1);
This code works fine with some numbers but if I try to calculate numbers like 0.90,0.04 or 0.12 it wont work and I have the same code written in C too. But I have the same problem with it.
#include <stdio.h>
int greedy(void){
int quarter = 0;
int dime = 0;
int nickel = 0;
int penny = 0;
float num = 0.40;
int x = 1;
while(x == 1){
if (num >= 0.25){
quarter = quarter + 1;
num = num - 0.25;
printf("q\n");
}else if(num >= 0.10){
dime = dime + 1;
num = num - 0.10;
printf("d\n");
}else if(num >= 0.05){
nickel = nickel + 1;
num = num - 0.05;
printf("n\n");
}else if(num >= 0.01){
penny = penny + 1;
num = num - 0.01;
printf("p\n");
};
if(num == 0){
x = 0;
};
};
if(x == 0){
int all = quarter + dime + nickel + penny;
printf("%i\n", all);
};
return 0;
};
int main(void){
greedy();
}
What did I do wrong?
The problem is that, many floating-point numbers can't be represented (using double or float) precisely, try this in Lua:
> print(0.9 == 0.9 - 0.2 + 0.1 + 0.1)
false
It should be equal in maths, but not here. The same for the C code.
Seeing your scope is counting your money, rewrite your code this way:
Greedy = function(num)
local q = 0;
local d = 0;
local n = 0;
local p = 0;
local x = 1;
num=num*100
while x == 1 do
if (num >= 25) then
q = q + 1;
num = num - 25;
print("q");
elseif (num >= 10) then
d = d + 1;
num = num - 10;
print("d");
elseif (num >= 5) then
n = n + 1;
num = num - 5;
print("n");
elseif (num >= 1) then
p = p + 1;
num = num - 1;
print("p");
end
if (num == 0) then
x = 0;
end
end
if (x == 0) then
local all = q+d+n+p;
print(all);
end
end
Greedy(1);
long long int fun2(int a, int b, int m)
{
long long int res = 1;
long long int c = a % m;
for (int i = 1; i <= b; i <<= 1)
{
c = c % m;
if ((b & i) != 0)
{
res = res * c;
res = res % m;
}
c = c * c;
}
return res;
}
int fun(int num, int k)
{
srand((unsigned)time(NULL));
if (num <= 1)
{
return num * 10;
}
if (num == 2 || num == 3 || num == 5)
{
return num * 10 + 1;
}
if (num % 2 == 0)
{
return num * 10;
}
if (num % 3 == 0)
{
return num * 10;
}
if(num % 5 == 0)
{
return num * 10;
}
int s = 0;
int s_pow = 1;
while ((s_pow & (num - 1)) == 0)
{
s = s + 1;
s_pow = s_pow << 1;
}
int d = num / s_pow;
for (int i = 0; i < k; i++)
{
int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;
if (fun2(a, d, num) != 1)
{
is_prime = false;
for (int r = 0; r <= s - 1; r++)
{
if (fun2(a, (1 << r) * d, num) == num - 1)
{
is_prime = true;
break;
}
}
if (!is_prime)
{
return num * 10;
}
}
}
return num * 10 + 1;
}
Where is a problem, maybe these long long int with int compares doesnt work correctly.
Compilation for windows and linus is without any warnings. It works but gives bad results for linux, for windows is ok. Please help.
#EDIT
I deleted code with INT_MIN and INT_MAX I just tried to fix the problem with this. (Sorry, should have delete it)
Problem SOLVED by myself !!!! Imagine that problem was in random a. I exchange this
int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;
with this
int a = (int)(rand()%(num-1)) + 1;
and everything works perfect – user3144540