I have to code a program that counts how many prime numbers are between 2 and "n".
The first input have to be the total number of tests and other ones have to be the "n" (number limit of the range of numbers to check).
The problem:
My inputs: 7 10 15 50 100 1000 10000 7
The right outputs for the inputs above: 4 6 15 25 168 1229 4
What my code outputs: 4 6 15 25 800 9800 4
My code:
#include <iostream>
using namespace std;
int f(int number){
int m=0,k=1;
for(k; k<=number; k++)
if(number%k==0)
m++;
if(m==2)
return true;
}
int main (){
int limit=0, counter=0, test=0;
bool n;
cin>>test;
for(int v=0; v<test; v++){
cin>>limit;
for(int i=2; i<=limit; i++){
n=f(i);
if (n==true)
counter++;
}
cout<<counter<<endl;
counter=0;
}
return 0;
}
You probably need to turn on warnings in your compiler. The function f returns a bool (not an int as declared) and does not do so unless the number of divisors of x is equal to two. This are fairly trivial mistakes that any decent C++ compiler should warn you about. Do not ignore warnings.
Related
i tried to solve this problem with dfs and dynamic programming . then
i submit my code to my school grader but the answer is wrong .
am i implement something wrong with dfs .
what's wrong with my code.
PS.sorry for my bad english
The problem :
given a random number there's 2 different way you can do with this
number
1.divide it by 3 (it has to be divisible)
2.multiply it by 2
given n number find the original order before it was swapped
----EXAMPLE1---- INPUT : 6 4 8 6 3 12 9 OUTPUT : 9 3 6 12 4 8
----EXAMPLE2---- INPUT : 4 42 28 84 126 OUTPUT : 126 42 84 28
Here's my code
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
int n ;
int input[51];
map<int,int> order ;
map<int,int> memo ;
bool valid(int a){
for(int i=0;i<n;i++){
if(input[i]==a)return 1 ;
}
return 0 ;
}
void dfs(int st){
memo[st]=1;
if(valid(st/3)){
if(memo[st/3]==0){
dfs(st/3);
order[st]+=order[st/3];
}
else order[st]+=order[st/3];
}
if(valid(st*2)){
if(memo[st*2]==0){
dfs(st*2);
order[st]+=order[st*2];
}
else order[st]+=order[st*2];
}
}
int main(){
cin >> n ;
for(int i=0;i<n;i++){
cin >> input[i];
memo[input[i]]=0;
order[input[i]]=1;
}
for(int i=0;i<n;i++){
if(memo[input[i]]==0)dfs(input[i]);
}
for(int i=n;i>=1;i--){
for(int k=0;k<n;k++){
if(order[input[k]]==i){
printf("%d ",input[k]);
break;
}
}
}
}
Information the OP should have told us in the first place:
my code gave the correct answer only 7 of 10 test case .i've already
asked my teacher he only told me to be careful with the recursion .
but i couldn't figure it out what's wrong with my recursion or
something else
An example that "fails":
Here's a failing case: Say you have the sequence 3 1 2 4. valid will
return true for 4 / 3 because it sees 1 in the sequence. –
Calculuswhiz
the better solution
#include<bits/stdc++.h>
using namespace std;
struct number{
long long int r , f3 , f2 ;
};
vector<number> ans ;
bool cmp(number a,number b){
if(a.f3!=b.f3)return a.f3>=b.f3;
if(a.f2!=b.f2)return a.f2<=b.f2;
return true ;
}
int main(){
int n ;cin>> n ;
long long int input ;
for(int i=0;i<n;i++){
cin >> input ;
long long int r = input ;
long long int f3 = 0, f2 = 0 ;
while(input%3==0){
f3++;
input/=3;
}
while(input%2==0){
f2++;
input/=2;
}
ans.push_back({r,f3,f2});
}
sort(ans.begin(),ans.end(),cmp);
for(auto i : ans){
cout << i.r << " " ;
}
}
The darkest place is under the lamp.
Look at the problem definition:
1.divide it by 3 (it has to be divisible)
Where do you test for the divisibility?
So, one error is here:
if(valid(st/3)){
This test should read:
if(st % 3 == 0 && valid(st/3)){
With this simple improvement, all three test cases pass.
A hint to improve (simplify) the solution
Numbers that are not divisible by 3 must come after those divisible.
Similarly, those not divisible by 9 must be coming after those that does.
Similarly for 27, 81,...
Now, if you divide your numbers into subsets of numbers of the form n = 3^k*m, where m % 3 != 0, then in each such a subset the only operation allowed by your algorithm is "multiply by 2". So it suffices to order them in ascending order.
The problem can be solved without dfs, nor is recurnece really necessary. Just order the numbers in a funny way: in descending order with respect to the number of times the number is divisible by 3, and then in ascending order. So, a task for you: challenge your teacher with a solution that, once the numbers are read in, does just one instruction std::sort (or qsort, as I see you write in C), then tests the validity of the solution, and prints it.
Moreover, I've just proved that if a solution exists, it is unique.
Why this code is showing TLE in the time limit of 1 sec ?
taking string inputs to be of size 10^6 and 10 in number
total number of operation touch approax 10^8 ,which is inclusive in the range of 1 sec but this code shows TLE;
Question : To find a substring of particular length and particular character in a main string
ex:
Example Input
3
5 2
ab*
5 2
*a**b
5 1
abcde
Example Output
NO
YES
NO
#include <iostream>
#include<string>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int k;
cin>>k;
string s ;
cin>>s;
string a="";
string r(k,'*');
// cout<<r<<endl;
string ans = "NO";
for(int i = 0 ; i <= n-k;i++)
{
a = s.substr(i,k);
if(a==r)
{
ans ="YES";
break;
}
}
cout<<ans<<"\n";
}
return 0;
}
That's because time complexity of substr is O(N), N being the length of the substring. So in the worst case, your code will run in O(N^2) which is not accepted if N >= 10^4
Additional:
O(N) will be accepted if N <= 10^7 while some online platforms allow it up to 10^8
I would like to produce the number of times an element is NOT seen again in the vector. The elements in the vector must be randomized by a seed, in this case, 3.
For example, if numbers 2, 6, and 4 are shown again in the vector with the size of 30, the totalComplete should be 27 since 3 numbers repeat themselves.
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<int> seats;
int totalComplete = 0;
srand(3);
seats.resize(30);
for(int k = 0; k < 20; k++){
seats.at(k)=(rand()%30);
cout << seats.at(k);
cout <<" ";
totalComplete++;
for(int j = 0; j < 20; j++){
if(j != k)
if(seats.at(k) == seats.at(j))
totalComplete--;
}
cout<<endl;
}
cout << totalComplete;
return 0;
}
So in my code, the numbers randomly placed in the vector is 6 25 18 0 15 10 12 16 1 4 28 4 3 19 22 21 24 25 1 15. The numbers 25, 15, 1, and 4 repeat themsevles so the totalComplete should be 16. However, my code produces 0. I believe the region where I check if number is repeated is wrong.
My immediate advice would be to separate the code into small pieces, and test each one separately. I'd start with a function to just fill the array with random numbers. Then I'd write a function to just count unique entries. Test each one in isolation, and check that you're getting what you expect. Then when each one works by itself, put them together to get a complete program that does what you really want.
I wish to find the number of occurrences of a number taken as input in the given multidimensional array defined by the logic below:
...
int n,x,count=0;
cin>> n >> x;
int a[n][n] ;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]= i*j ;
}
}
for( int i=1;i<=n;i++)
{
for( int j=1;j<=n;j++)
{
if(a[i][j] == x)
++count;
}
}
cout<< count ;
...
For eg., if I give input as 6(n) and 12(to find its number of occurrences, x here). The multidimensional array looks something like this:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
Now, the number of occurences of 12 here is 4(count).
But when I give n as 10 and x as 5, the program stops working. I can't seem to find what is happening. Can someone help me on this?
Also in what way can I modify my code?
How can I handle the case when n is as large as 1000 or 10k without changing the logic of the program?
Indices in C/C++ starts at 0. If an array is declared to have size n as in int a[n] the only valid indices are: 0,1,...,n-1 i.e. [0,n[
If you go out of bound undefined behaviour is expected. That should be your case.
Fix the loops as follows (note the new bounds and the +1 in i and j)
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]= (i+1)*(j+1) ;
#include <algorithm>
// ...
for(int i = 0; i < n; ++i) {
count += std::count(a[i], a[i] + n, x);
}
or a more simpler version:
std::cout << std::count(a[0], a[0] + n*n, x);
int checkdiv(int num)
{
int SqrtOfnumber,i;
SqrtOfnumber=sqrt(num);
int counter=1;
for(i=2;i<SqrtOfnumber;i++)
{
if(num%i==0)
counter++;
}
counter=counter *2;
if(i*i == num)
counter++;
return counter;
}
**In cases of 2 numbers have the same number of divisors the output should be the one with the smallest value
Input examples
2 \\ test cases
1 10
1000 2000
Expected Output
Between 1 and 10, 6 has a maximum of 4 divisors.
Between 1000 and 2000, 1680 has a maximum of 40 divisors.
The above code Output
Between 1 and 10, 10 has a maximum of 4 divisors.
Between 1000 and 2000, 1680 has a maximum of 38 divisors.
The function is returning false number of divisors while the rest of the code work corectly, How can I fix it?
the input is a number which I want to check how many divisors it have and the output in the number of divisors
The fix for the problem was use double for the SquarOfnumber
int checkdiv(int x)
{
double SqrtOfnumber;
SqrtOfnumber=sqrt(x);
int counter=1,i;
for(i=2;i<SqrtOfnumber;i++)
{
if(x%i==0)
counter++;
}
counter=counter*2;
if(i*i == x)
counter++;
return counter;
}