For a project, I need to create a program where dice 9 are rolled. All the dice that rolled the lowest (best) score of that group are recorded in the scorecard, removed for the next round, and it continues until all 9 dice are gone. This is then done twice. The combined score of the former nine and ladder nine are then summed.
For example:
Round 1: 9 dice rolled, the best score of the round was -1 and 2 dice landed on it. Hole1 and Hole2 are marked with a score of -1.
Round 2: 7 dice (9-2 removed) rolled, the best score of the round was 0 with 1 die. Hole3 is marked with the score 0.
This continues until 0 dice remain. The scores are added up and the game is played again for holes for another 9 rounds.
The output based on the example given should look something like this:
hole1
hole2
hole3
...
hole18
frontnine
backnine
score
-1
-1
0
best
sum(hole1-9)
sum(hole10-18)
total
Below is the program for a single die with the lowest value being recorded and removed. How do I modify it to get what I want (listed above).
data work.project;
array scorecard(18) hole1-hole18;
array dice(9) dice1-dice9;
seed= 12345;
do frontback=0,1;
do rolls=1 to 9;
do die=1 to 9;
dup=0;
dice(die)=rantbl(seed, 1/12,3/12,2/12,2/12,3/12,1/12)-2;
if die=rolls then best=dice(die);
if dice(die) < best then best=dice(die);
end;
end;
end;
scorecard((rolls+dup)+frontback*9)=best;
end;
frontnine=sum( of hole1-hole9);
backnine=sum(of hole10-hole18);
score= frontnine + backnine;
output;
keep hole1-hole18 frontnine backnine score;
run;
Related
I am trying to teach myself c++.
On Sololearn I have a task, which is
You are making a program for a bus service.
A bus can transport 50 passengers at once.
Given the number of passengers waiting in the bus station as input, you need to calculate and output how many empty seats the last bus will have.
Sample Input: 126
Sample Output: 24
It also says I should use the "%" operator.
This is the code I created:
int bus = 50;
int stop;
cin >> stop;
cout<< stop % bus;
return 0;
I get 12.
What is the correct way? I'm finding it difficult to understand what the modulo operator does. My understanding is that it divides as many times as it can and leaves the remainder (i.e. 16 % 3 = 1).
First you have to get the number of passengers
int passengers; cin >> passengers;
then you have to find how many passengers are left
int remainPass = passengers % 50;
then you have to find how many seats are left
int remainSeats = 50 - remainPass;
Modulos operator basically represents the leftover from the division
so what we need to do is take the number of people that will remain in the last bus travel which is stop % bus and compute bus - (stop % bus)
that way we know the number of empty seats on the last travel
This is like each bus was filled to the fullest (50 people per bus) what will remain is 26 and so on the last bus the number of empty seats will be 50 - 26 = 24
PS: 12 doesn't seem to be the right output of 126 % 50 it should be 26
Problem
Finally, progress reached the Madoka family and she decided to play with her little sister in the sensational game Space Arrays.
The rules of the game are as follows:
Initially, a sequence a1,a2,…,aN is given.
The players alternate turns.
In each turn, the current player must choose an index i and increment ai, i.e. change ai to ai+1.
Afterwards, if there is no permutation p1,p2,…,pN of the integers 1
through N such that ai≤pi holds for each valid i, the current player
loses.
Otherwise, the game continues with the next turn.
Madoka is asking you to help her ― tell her if the first player (the player that plays in the first turn) or second player wins this game if both play optimally.
Input
The first line of the input contains a single integer T denoting the
number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers a1,a2,…,aN.
Output
For each test case, print a single line containing the string "First" if the first player wins or "Second" if the second player wins (without quotes).
Constraints
1≤T≤2⋅10^4
1≤N≤2⋅10^5
The sum of N over all test cases doesn't exceed 2⋅10^5
1≤ai≤N for each valid i
Subtasks
Subtask #1 (100 points): Original constraints
Example Input
4
4
1 2 3 3
4
1 1 3 3
5
1 2 2 1 5
3
2 2 3
Example Output
First
Second
Second
Second
Explanation
Example case 1:
If the first player increases the fourth element, the resulting sequence is (1,2,3,4).The second player loses after increasing any of the elements.
If the first player increases the second element, the resulting
sequence is (1,3,3,3) ,and he loses because there is no valid
permutation. For example if p=(2,1,4,3), the second element of a is
greater than the second element of p.
Time Limit : 1 Secs
Source limit : 50000 byte
Here is my code
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
int main() {
// your code goes here
lli t;
cin >> t;
while(t--){
lli n;
cin >> n;
int arr;
int arrSum = 0;
for(lli i=0; i<n ;i++){
cin >> arr;
arrSum += arr;
}
lli turn = 0;
lli sum = (n*(n+1))/2;
turn = sum-arrSum;
if(turn < 0){
turn = 0;
}
if(turn%2 == 1){
cout << "First" << endl;
}
else{
cout << "Second" << endl;
}
}
return 0;
}
In your algorithm you don't check if first player initially loses without making a turn but the sum is lower than n*(n+1)/2, e.g. for input
1
5
1 1 2 5 5
The sum is 13.
5 * (5 + 1) / 2 - (1 + 1 + 2 + 5 + 5) == 1
But first player can't do anything and has lost before the first turn. Ssecond player wins.
Actual output from your code: First.
Expected output: Second
Another example is
1
5
1 1 4 4 4
with
5 * (5 + 1) / 2 - (1 + 1 + 4 + 4 + 4) == 1
In both cases your code returns that first player wins but actually second player wins without the game even starting.
I am solving a problem called Car Fueling on coursera and I followed the pseudocode that had been given. The problem states:
You are going to travel to another city that is located d miles away from your home city. Your car can travel at most m miles on a full tank and you start with a full tank.
Along your way, there are gas stations at distances stop 1, stop 2, . . . , stop n from your home city. What is the minimum number of refills needed?
Input Format.
The first line contains an integer d.
The second line contains an integer m.
The third line specifies an integer n.
Finally, the last line contains integers stop 1, stop 2, . . . , stop n.
Output Format.
Assuming that the distance between the cities is d miles, a car can travel at most m miles on a full tank, and there are gas stations at distances stop 1, stop 2, . . . , stop n along the way, output the minimum number of refills needed. Assume that the car starts with a full tank. If it is not possible to reach the destination, output −1.
Here is the function to solve the problem:
int compute_min_refills(int dist, int tank, vector<int> & stops) {
int curref=0;//current position of the car
int numref=0;//number of refills
int lastref=0;//last position of the car
int n=stops.size();//the amount of stops to refill at
while(curref<=n){
if(curref>=n){return -1;}
lastref=curref;
while((curref<=n) && (stops[curref+1]-stops[lastref]<=tank)){curref=curref+1;}
if(curref==lastref){return -1;}
if(curref<=n){numref=numref+1;}
}
return numref;
}
When I submit, it fails on the first test case, but I don't know what could be causing it to give error.
The test case:
Input:
500
200
4
100 200 300 400
Your output:
1
Correct output:
2
I am new to SAS and I was wondering how to cure the variable not found problem in creating a binomial distribution?
DATA additional (KEEP=X);
DO REPEAT = 1 TO 1000;
CALL STREAMINIT(1234);
DO I=1 TO 1000;
X=RAND("BINOMIAL",0.6,10); /*NUMBER OF WINS IN TEN TOSSES*/
END;
IF X GE 5 THEN WINNER + 1;
ELSE LOSER + 1;
OUTPUT;
END;
RUN;
PROC PRINT DATA=additional;
VAR WINNER LOSER;
RUN;
I am creating a binomial random variable which if x is great than 5 then counts one for the winner, if less than 5 then counts one for the loser, the question is asking to found how many time are winners and how many times are losers. I kept on getting variable not found error. Am i doing something wrong with generating the binomial distribution.
/further editing/ this is the problem I am given.
You are given $10. Let the variable money = 10.
You play a game 10 times. The probability that you win a game is 0.4,
and the probability that you lose a game is 0.6.
If you win a game, you win $1. If you lose a game, you lose $1. So if
you win the first game, money becomes 11. But if you lose the first
game, money becomes 9.
After you have played the game 10 times, money is the amount that you
go home with. If you end up with at least $10, call yourself a winner.
Otherwise, call yourself a loser. Define the variable result as winner
or loser.
(a) Write a data step to generate random numbers and simulate your
result 1000 times. So that I can easily check your outputs, use
1234 as your seed for the random number generator. (You do not
need to show me the 1000 results.)
(b) Write a proc step to show how many times you are a winner, and
how many times you are a loser.
Not fully understand that you want to do with simulation. From your codes, you just keep 1000 records, which are all kept at last loop because of your first loop end position; call streaminit should be first line; you only keep X, you could not get winner and loser variable.
I guess maybe you could try this.
DATA additional;
CALL STREAMINIT(1234);
DO REPEAT= 1 TO 1000; *numbers of sample;
DO I=1 TO 100; *size of sample;
X=RAND("BINOMIAL",0.6,10); /*NUMBER OF WINS IN TEN TOSSES*/
IF X GE 5 THEN results='WINNER';
ELSE results='LOSER';
OUTPUT;
END;
END;
RUN;
proc freq data=additional;
by repeat;
table results;
run;
Edit: It seems that you want to know final results, you could get it from above code by changing results as numeric variable. Here is modified codes, if win is +1, lose is -1.
DATA additional;
CALL STREAMINIT(1234);
DO REPEAT= 1 TO 100; *numbers of sample;
DO I=1 TO 10; *size of sample;
X=RAND("BINOMIAL",0.6,10); /*NUMBER OF WINS IN TEN TOSSES*/
IF X GE 5 THEN results+1;
ELSE results=results-1;
OUTPUT;
END;
results=0;
END;
RUN;
proc freq data=additional;
by repeat;
table results;
run;
Hi Im trying to translate this code to TI-BASIC. Im having problems with how to change for loop into while loop and also with incrementing a number in TI-BASIC.
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
You can efficiently use a While loop in this situation:
Input "NUMBER: ",A
1->B
3->I
√(A->D
If not(fPart(A/2
DelVar BWhile I<=D and B
fPart(A/I->B
I+2->I
End
If not(B
Disp "NOT
Disp "PRIME
In TI-Basic a While loop works as you would expect and you can have conditions for it.
Incrementing a number is as simple as saying
X+i->X
Where 'i' is the incrementer.
To change a For loop into a While loop, you'll have to set up the While loop to constantly check to see if the number and increment have passed the upper bound while increasing the increment each run through.
If you wanted to mimic i++ or ++i in TI-Basic (Using a While loop), all you would have to change would be the arrangement of the code. Please note that TI-Basic For statements always operates under ++i.
Example (i++):
0->X
While X<10
Disp X
X+1->X
End
This will display (With each number on a new line)
0 1 2 3 4 5 6 7 8 9
Example (++i):
0->X
While X<10
X+1->X
Disp X
End
This will display (With each number on a new line)
1 2 3 4 5 6 7 8 9 10
Let it be noted that TI-Basic For statements are much much faster than While loops when it comes to incrementing and should almost always be considered superior for the task.
Integrating Timtech's idea to skip even numbers effectively halves the required time to check the primality of the number with the addition of only a few extra lines.
I expanded the idea to skip multiples of two and multiples of three.
Input "Number:",X:abs(X->X
0
If not(fPart(X/2)) or not(fPart(X/3:Return
For(B,5,sqrt(X),6)
If not(fPart(X/B)) or not(fPart((X+2)/B:Return
End
1
Test Number: 1003001
Time Required: ~4 Seconds (So much better than 15 :D)
Size: 65 Bytes
I dont see why you would want to use a while loop as ti-basic has for loops:
0->F
Input "ENTER NUMBER:",N
For(I,2,int(N/2
If N/I=int(N/I
Then
int(N/2->I
1->F
End
End
If F
Then
Disp "NUMBER IS PRIME
Else
Disp "NUMBER IS NOT PRIME
End
N/I=int(N/I is a way to check for a number's remainder (another way of saying N%I==0 but ti basic does not have modulus). Another trick here is setting I to its maximum bound (int(N/2) as a sort of "break" like other languages would have