|
|
Ask Away!. But maybe before you do, take a look at some of the threads posted here, and some of the questions and replies in the Quick Answers section. We don't do you work for you, but we're willing to help you with issues you may have. So questions like "why does this program produce unexpected results", or "why doesn't this compile" are likely to get answers. So are questions like "I'm trying to do X. Should I use a vector or a list?." But questions that ask "Implement a linked list, insert some values, sort and print the list", with no indication that you've made any attempt to solve the question yourself are likely to be either ignored or get a "We don't do homework" type of reply.
Keep Calm and Carry On
|
|
|
|
|
Well, aside from asking this exact question again (ok, you might ask it again to assert that VN and/or K5 is/are NOT robots I guess), use keywords in your PROGRAM by typing them into the "search" here at CP. The Discussions are titled and the QA is tagged. So keep those two things in mind also.
And.
Have a great day!
|
|
|
|
|
c ++ who can show me the direction of this article. There is an undirected star graph consisting of nodes labeled 1 to n. A star graph is a graph in which there is a central node and exactly n - 1 edges connecting the central node to every other node.
You are given a 2Dedges integer array where each edge edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Returns the center of the given star graph.
Input: edges =
4
1,2
5,1
1,3
1,4
Output: 1
|
|
|
|
|
And...
What's the problem?
Count the connections of each node.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
#include <iostream>
using namespace std;
bool checkSquareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return true;
i++;
}
return false;
}
int squareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return i;
i++;
}
return i;
}
int numberOfStep(int num)
{
int cnt =0;
int i =0;
while(num)
{
if(checkSquareNumber(num)== true)
{
num = squareNumber(num);
cnt++;
}
else if(num%3==0)
{
num = num/3;
cnt++;
}
else if(num%2==0)
{
num/=2;
cnt++;
}
else
{
num--;
cnt++;
}
}
return cnt;
}
int main()
{
int num,t;
cin>>t;
while(t--)
{
cin>>num;
cout<<numberOfStep(num)<<endl;
}
return 0;
}
|
|
|
|
|
Quote: I'm having a problem with my code. can anyone point out where my error is?. i am trying to count the minimum number of steps to get any number to 0
|
|
|
|
|
Ok, so it appears the rules of this game are:
- If a number is a perfect square, take its square root
- if a number is divisible by 3, divide it by 3
- if a number is divisible by 2, divide it by 2
- otherwise, subtract 1.
So, given an input of 32, we'd go 32 -> 16 -> 4 -> 2 -> 1 -> 0 for 5 steps.
and given 75, we'd go 75 -> 25 -> 5 -> 4 -> 2 -> 1 -> 0 for 6 steps.
Now, what problem are you having? A quick glance over the code didn't show any obvious errors.
Your checkSquareNumber() and squareNumber() functions are quite inefficient. You'd be better off googling a better square root algorithm (or just using the library sqrt() function). Ad since they are doing the same thing, they could be combined (return -1 if it is not a perfect square, and you can replace the two calls with one)
Truth,
James
|
|
|
|
|
Quote: i am having problem with my code. where the first condition to check if the original number is a perfect square. can you point out where my error is or point me to a better direction for this problem. i am trying to count the minimum number of steps to convert any number to 0
|
|
|
|
|
If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of num ), together withe the expected result. Otherwise, how could we possibly help?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
intput: 2 10 32
output: 4 5
modified 22-Jun-22 4:57am.
|
|
|
|
|
Quote: the problem is I can't run my code
|
|
|
|
|
A quick run with the debugger would spot immediately the bug: if num becomes 1 then your code end in a infinite loop, because 1 is a perfect square of itself. For a quick fix, replaceQuote: if(checkSquareNumber(num)== true) with
if(num > 1 && checkSquareNumber(num)== true)
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
The two functions checkSquareNumber and squareNumber should both start their calculations at 2, rather than 0. For square roots, zero is meaningless and 1 is the square root of itself.
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
Member 14968771 wrote: what to ask Mrs Google to help me write a C++ code to
Try: "How to learn C++". Give me about 196,000,000 results
Mircea
|
|
|
|
|
Member 14968771 wrote: is "tokenization" a good search word ?? Fair enough, but that is only the very beginning. A precursor to parsing.
Tokenization is the chopping into atomic pieces of the input text, with no concern for how they are put together. All the tokenizer knows is how to delimit a symbol (token): That a word symbol start with a alphabetic and continues through alphanumerics but ends at the first non-alphanumeric - the tokenizer doesn't know or care whether the word is a variable name, a reserved word or something else. If it finds a digit, it devours digits. If the first non-digit is a math operator or a space, it has found an integer token. If it is a decimal point or an E (and the language permits exponents in literals), the token is a (yet incomplete) float value, and so on. The only language specific thing that the tokenizer needs to know is how to identify the end of a token. Once it has chopped the source code into pieces, its job is done.
Parsing is identifying the structures formed by the tokens. Identifying block, loops, conditional statements etc.
The borderline isn't necessarily razor sharp. Some would say that when the tokenizer finds an integer literal token, it might as well take the the task of converting it to a binary numeric token value, to be handed to the parser. That might be unsuitable in untyped languages where a numeric literal may be treated as a string. After identifying a word symbol, it might search a table of reserved words, possibly delivering it to the parser as a reserved word token. Again, in some languages this is unsuitable (and lots of people would say it goes far beyond a tokenizer's responsibility).
If you want to analyze some input, doing an initial tokenization before starting the actual parsing is a good idea. Most compilers do that.
Curious memory:
One of my fellow students was in his first job after graduation set to identify bacteria in microscope photos. That was done by parsing: They had BNF grammars for different kinds of bacteria, and the image information was parsed according to the various grammars. If the number of parsing errors was too high, the verdict was 'Nope - it surely isn't that kind of bacteria, let me try another one!' Those grammars with a low error count was handed over to a human expert for confirmation, or possibly making a choice between viable alternatives, if two or more grammars gave a low error count. This mechanism took a lot of trivial work off the medical personnel, and the computer could scan far more images for possibly dangerous bacteria than there would be human resources to do. The university lecturer in the Compilers and Compilation course certainly hadn't prepared us for compiling bacteria!
|
|
|
|
|
trønderen wrote: compiling bacteria! Is that making bugs from bacteria, or vice versa?
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
|
I want to create some DLL files for specific calculations and import them into my C# project. Can those calculations (C++ codes in DLL files) be done in the C# application as fast as a native C++ environment?
|
|
|
|
|
As always. It depends. It depends on what the calculations are and how you write the code.
There's no way for anyone to give you a direct answer for this.
|
|
|
|
|
I was reading this turorial on how to use Java in your C++ project, and at one step it says to add the location of jvm.dll to PATH. Well that is fine for developing purpose, but not for a released project. So instead of that I tried the second part, to add it manually to Debug/Release folder and remove the location from PATH, but unfortunately I'm getting the following error:
Error occurred during initialization of VM
Failed setting boot class path.
What I'm I doing wrong, and how to fix the problem?
|
|
|
|
|
|
|
Well, has it worked when you added the location of jvm.dll to PATH?
|
|
|
|