Is there any way of creating a simple java(or c,c ++, python) program that prints 3 (outputs the 3) when given input=6 and it gives output=6 when given input=3 without using "if conditions" ?
Assuming you're happy for it to produce other outputs on inputs that aren't 6 or 3, then you can just compute 9-x.
You can always just use a switch-case statement. Also, if you only want those two answers, you could also take the input as an int and do 9-[your int] and print that answer.
You can use the XOR bit operation. It compares pairs of bits and returns 0 if bits are equals and 1 if bits are different.
We have 3 = 011b and 6 = 110b. This numbers differ by 1 and 3 digit (bit), so XOR mask will be 101b = 5.
Code example:
public static int testMethod(int value){
return System.out.println(value ^ 5);
}
without if or without control flow statement/condition statement ?
you could use switch statement
private void tes(int i) {
switch (i) {
///give output 6 where input is 3
case 3:
System.out.println(6);
break;
///give output 3 where input is 6
case 6:
System.out.println(3);
break;
}
}
Related
I am spending my evening doing some programming problems from Kattis. There is one part of the problem 4 thought that I am stuck on.
Given a number, the program is supposed to return the operations (+, -, * or /) required between 4 fours to achieve that number.
For example, the input
9
would result in the output
4 + 4 + 4 / 4 = 9
My solution (not efficient, but simple) is to evaluate all possible ways to combine the operators above and see if any of the combinations achieve the wanted result.
To do this I have written the function seen below. It takes in an array of chars which are the operators to be evaluated (uo[3], could look like {+, /, *}), and the wanted result as an integer (expRes).
bool check(char uo[3], int expRes) {
int res = 4;
for(int oPos = 2; oPos >= 0; oPos--) {
switch (uo[oPos]) {
case '+' : res += 4; break;
case '-' : res -= 4; break;
case '*' : res *= 4; break;
case '/' : res /= 4; break;
}
}
return res == expRes;
}
I realized that this "sequential" approach comes with a problem: it doesn't follow the order of operations. If I was to call the function with
uo = {+, -, /}
and
expRes = 7 it would return false since 4 + 4 = 8, 8 - 4 = 4, 4 / 4 = 1.
The real answer is obviously true, since 4 + 4 - 4 / 4 = 7.
Can any of you think of a way to rewrite the function so that the evaluation follows the order of operations?
Thanks in advance!
Its an easy problem if you look at it.
You are restricted with four 4's and three operators in between, that is you already know your search space. So one solution is to generate the complete search space which is O(n^3) = 4^3 = 64 total equations, where n is the number of operators. Keep the answer to these solutions as a <key, value> pair so that look up to the input of test case is O(1).
Step wise you'd do.
Generate Complete Sequence and store them as key, value pairs
Take Input from test cases
Check if key exists, if yes print the sequence, else print that the sequence doesn't exist
Solution would take 64*1000 operations, which can easily be computed with in a second and would avoid Time Limit Exceeded Error that usually these competitions have
in Code form (most of it is incomplete):
// C++ Syntax
map<int, string> mp;
void generateAll() {
// generate all equations
}
void main () {
generateAll();
int n, t; scanf("%d", &t);
while (t--) {
scanf("%d", &n);
if ( mp.find(n) != mp.end() )
// equation exists to the input
else
// equation doesn't exist for the input
}
}
Describe if statement. Write an if statement to multiply even numbers by 3 and odd numbers by 4.
You can use two way to do if condition in java.
Java Code:
STEP 1:
System.out.println(number%2==0?number*3:number*4);
STEP 2:
if(number % 2==0)
System.out.println(number*3);
else
System.out.println(number*4);
both are same function.
Python code
if number % 2 == 0:
print number * 3
else:
print number * 4
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
Codeforces problem 158B-http://codeforces.com/problemset/problem/158/B
I am getting an unexpected output for test case 5.I think I should get 1 as output but I am it as 2.Please guide me.Judge's log:
Test: #5, time: 30 ms., memory: 380 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
2
2 1
Output
2
Answer
1
Checker Log
wrong answer expected 1, found 2
My solution:
#include<iostream>
using namespace std;
int n,a[100000],i,b,c,d,e,f,g,h;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
b=0;
c=0;
d=0;
e=0;
for(i=0;i<n;i++)
{
if(a[i]==1) //To check for number of 1,2,3 and 4 membered groups.
b=b+1;
if(a[i]==2)
c=c+1;
if(a[i]==3)
d=d+1;
if(a[i]==4)
e=e+1;
}
f=e;
if(d>b) //More 3 member groups than 1.
{
f=f+d; //f=f+b+(g-b) 3 and 1 member groups combine.Remaining 3 i.e. (g-b) member groups will can't combine with 2 member groups.Hence,they take separate taxies.
g=-1;
}
if(b>=d) //More 1 member groups than 3.
{
f=f+d;
g=b-d; //g=remaining 1 member groups.
}
h=(2*c)%4; //Empty seats in last taxi.Possible values can be 0,1,2,3.
if(h==0)
f=f+(c/2);
else
f=f+((c+1)/2);
if(g!=-1)
{
g=g-h; //Remaining 1 member groups after combining with remaining seats in last 2 member taxi.
if((g%4)==0)
f=f+(g/4);
else
f=f+(g/4)+1;
}
cout<<f;
}
If your input is 2 2 1, then b and c will both be 1, making f 0 and g 1 in the first set of conditionals. h will be (2 * 1) % 4 or 2, making an update to f (0 + 1 = 1). Since g is 1, g-h is -1, which will lead to you executing f=f+(g/4)+1 which is f=1 + (-1/4)+1 which is 1 + 0 + 1 = 2 in integer math.
I think you wanted to check if g-h>0 instead of g!=-1, but there are a ton of places you could simplify your code. Note that using a debugger and stepping through this would have shown you where your problems are much faster, and be much more helpful to increasing your skills, than asking SO.
Just for anyone else looking at this question, this is a fairly simple answer to the problem.
Do it by hand and see if you get the same answer. If you get the same answer by hand as the computation, your algorithm is wrong. If you don't, your code is wrong.
Print variables from intermediate computations to see if they are what you think they should be.
Make sure, if it might matter, that you reinitialize your variables (including arrays) before each use.
Other tips:
Use a switch statement instead of multiple if-equal statements.
Name your variables. It helps you keep track when looking at your code.
When you have multiple variables with similar use, consider using an array instead. b, c, d, and e all seem similar.
I just found out the hard way an inline if (A?B:C) does not work as expected in a switch statement.
where A a boolean, B and C both integer unequal to 0. The result of this statement is 0 when placed inside a switch.
I found a stackoverflow post [1] where this behaviour was mentioned but I can not find any explanation why this doesn't work as I would expect. What is causing this?
For example:
int foo = 6;
switch(foo)
{
case 6:
return 10 + true ? 2 : 4;
}
[1] Benefits of inline functions in C++?
This is nothing to do with switch.
10 + true ? 2 : 4
is equivalent to:
(10 + true) ? 2 : 4.
If you want it to act like:
10 + (true ? 2 : 4)
then you will need to write it like that.