problem in Light OJ:
1001 - Opposite Task:
This problem gives you a flavor the concept of special judge. That means the judge is smart enough to verify your code even though it may print different results. In this problem you are asked to find the opposite task of the previous problem.
To be specific, I have two computers where I stored my problems. Now I know the total number of problems is n. And there are no duplicate problems and there can be at most 10 problems in each computer. You have to find the number of problems in each of the computers.
Since there can be multiple solutions. Any valid solution will do.
Input:
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with a line containing an integer n (0 ≤ n ≤ 20) denoting the total number of problems.
Output:
For each case, print the number of problems stored in each computer in a single line. A single space should separate the non-negative integers.
Sample Input
Output for
Sample Input:
3
10
7
7
Sample output:
0(space)10
0(space)7
1(space)6
my code :
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c,sum;
int i,j,mini=0,maxi;
int com1,com2;
do{
scanf("%d",&c);
}while (c>25);
int t[c+1];
for(i=1; i<=c; i++)
{
do{
scanf("%d",&t[i]);
}while (t[i]>20);
}
for(i=1; i<=c; i++)
{
maxi=t[i];
com1=rand() % (maxi - mini + 1) + mini;
com2=t[i]-com1;
printf("%d %d\n",com1,com2);
}
return 0;
}
When I submit my code the judge gives wrong answer.
But while I compile the code in CodeBlocks it gives right answer.
I can't understand the problem in online judge.
How can I solve it.
The original post has mini=0 for all cases, but where the total problems is > 10 that may not work. I have adjusted that
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c = 0, mini, maxi, i, com1, com2, *t;
scanf ("%d",&c);
t = malloc (c * sizeof(int));
for (i=0; i<c; i++)
scanf ("%d", &t[i]);
printf("\n");
for (i=0; i<c; i++) {
if (t[i]>10) {
maxi = 10;
mini = t[i] - 10;
} else {
maxi = t[i];
mini = 0;
}
com1 = rand() % (maxi - mini + 1) + mini;
com2 = t[i] - com1;
printf("%d %d\n",com1,com2);
}
free (t);
return 0;
}
Please note that results of malloc() and scanf() have not been checked: they should be.
You seem to be looping unnecessary number of times for the input. I have removed the unnecessary loops from your code while retaining the logic you used. I made a silly mistake with my earlier answer.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c = 0;
int maxi=10;
scanf("%d",&c);
int t[c];
for(int i=0; i<c; i++)
scanf("%d",&t[i]);
for(int i=0; i<c; i++)
{
int com1 = t[i],com2 = 0;
if (t[i] > 10)
{
com1 = 10
com2 = t[i]-com1;
}
printf("%d %d\n",com1,com2);
}
return 0;
}
Related
#include <stdio.h>
main()
{
int n=10;
for(int a=n;a>=1;a++) //for bringing out numbers from 1-10
{
int e=a%2; //int e to figure out if the number is even(divisible by 2)
if(e==0)
printf("%d\n",a); //printing the even numbers
}
}
I am new to programming. Learning C.
Here I am trying to make a program that prints even numbers till 10. Executing this code leads to endless even numbers starting from 10.
Can't seem to figure out the logic error here. Some help, please?
The logic for the for loop is not correct.
int n = 10;
for(int a = 0; a <= n; a++) {
if(a%2==0){
printf(a);
}
}
Notice that this is stating at 0, because in CS almost all the time the count starts at 0.
Your loop will never end, it should be:
for(int a = 1; a <= 10; a++)
The entire program should be like that:
#include <stdio.h>
int main(void)
{
for(int a = 1; a <= 10; a++) //for bringing out numbers from 1-10
{
int e = a % 2; //int e to figure out if the number is even(divisible by 2)
if(e == 0)
printf("%d\n", a); //printing the even numbers
}
}
Output:
2
4
6
8
10
Wrote this to find the prime numbers between 2 to 1000. But it stops after showing that 2 and 3 are prime numbers. I know I can find how to write a code for finding out prime numbers on the internet. But I really need to know what's going wrong here.
#include <stdio.h>
main() {
int i, j;
int ifPrime = 1;
for (i = 2; i < 1000; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
ifPrime = 0;
break;
}
}
if (ifPrime == 1) {
printf("%d is prime\n", i);
}
}
}
The line
int ifPrime=1;
must be inside the outer for loop. There it will be initialized for every i. This corresponds to the natural language words "to check whether a number i is prime, first assume it is. Then check if it is divisible". The code you had before said "to check whether the numbers 2 to 1000 are prime, first assume they are", and this wording was too broad.
The code should be:
int main()
{
for (int i = 2; i < 1000; i++)
{
int ifPrime = 1;
for (int j = 2; j < i; j++)
I replaced main with int main since that is required since 20 years. (You should not learn programming from such old books.)
I moved the int i and the int j into the for loops so that you cannot accidentally use these variables outside the scope where they have defined values.
To avoid this bug in the future, it's a good idea to extract the is_prime calculation into a separate function. Then you would have been forced to initialize the ifPrime in the correct place.
Another way of finding the cause of this bug is to step through the code using a debugger and ask yourself at every step: does it still make sense what the program is doing?
You are not setting ifPrime back to 1 after checking for the single number. So once you get a number that is non_prime, ifPrime is now 0 and hence if(ifPrime == 1) would never return true post that and hence you only see 2, 3 as prime
#include <stdio.h>
int main(void) {
for( int i=2;i<1000;i++)
{
int ifPrime = 1;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
ifPrime=0;
break;
}
}
if(ifPrime==1)
{
printf("%d is prime\n",i);
}
}
return 0;
}
I am trying to implement Insertion sort algorithm in C.
But all I get is SIGSEGV error in online IDEs and the output doesn't show up in Code::Blocks. How to avoid Such errors.
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* Here i and j are for loop counters, temp for swapping
count for total number of elements,array for elements*/
int i, j, temp, count;
printf("How many numbers are you going to enter");
scanf("%d", &count);
int n[20];
printf("Enter %d elements", count);
// storing elements in the array
for(i = 0; i < count; i++) {
scanf("%d", n[i]);
}
// Implementation of insertion sort algorithm
for(i = 0; i < count; i++) {
temp = n[i];
j = i - 1;
while(temp < n[j]) {
n[j+1] = n[j];
j = j - 1;
}
n[j+1] = temp;
}
printf("Order of sorted elements");
for(i = 0; i < count; i++) {
printf("%d", n[i]);
}
return 0;
}
There are a couple of problems with your code. First of all, what is a SIGSEGV error? Well, it's another name for the good old Segmentation fault error, which is basically the error you get when accessing invalid memory (that is, memory you are not allowed to access).
tl;dr: change scanf("%d",n[i]); to scanf("%d",&n[i]);. You're trying to read the initial values with scanf("%d",n[i]);, this raises a segmentation fault error because scanf expects addresses in which put the values read, but what you're really doing is passing the value of n[i] as if it were an address (which it's not, because, as you did not set any value for it yet, it's pretty much just memory garbage). More on that here.
tl;dr: change int n[20]; to int n[count]. Your array declaration int n[20]; is going to store at most 20 integers, what happens if someone wants to insert 21 or more values? Your program reserved a certain stack (memory) space, if you exceed that space, then you're going to stumble upon another program's space and the police (kernel) will arrest you (segmentation fault). Hint: try inserting 21 and then 100 values and see what happens.
tl;dr: change for(i = 0; i < count; i++) { to for(i = 1; i <= count; i++) {. This one is a logic problem with your indexes, you are starting at i = 0 and going until i = count - 1 which would be correct in most array iteration cases, but as j assumes values of indexes before i, you need i to start from 1 (so j is 0, otherwise j = -1 in the first iteration (not a valid index)).
My final code is as follows. Hope it helped, happy coding!
#include <stdio.h>
#include <stdlib.h>
int main() {
/*Here i and j are for loop counters,temp for swapping
count for total number of elements,array for elements*/
int i, j, temp, count;
printf("How many numbers are you going to enter?\n");
scanf("%d",&count);
int n[count];
printf("Enter %d elements\n",count);
//storing elements in the array
for(i = 0; i < count; i++) {
scanf("%d", &n[i]);
}
//Implementation of insertion sort algorithm
for(i = 1; i <= count; i++) {
temp = n[i];
j = i-1;
while(temp < n[j]) {
n[j+1] = n[j];
j--;
}
n[j+1] = temp;
}
printf("Order of sorted elements\n");
for(i = 0; i < count; i++) {
printf("%d\n",n[i]);
}
return 0;
}
Edit: If you're having trouble with online IDEs, consider running your programs locally, it saves a lot of time, plus: you never know what kernel version or magic the online IDEs are using to run your code (trust me, when you're coding in C -- fairly low level language, these things make a difference sometimes). I like to go all root style using Vim as text editor and gcc for compiling as well as gdb for debugging.
I am trying to find the number of distinct numbers from input which are not equal to 0. n is in 1-100 range. The numbers in the array are in the 0-600 range.
http://codeforces.com/problemset/problem/937/A
For this question, I wrote a code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int n, count = 0, i;
scanf("%d", &n);
int ar[n], ar2[601];
memset(ar2, 0, 600 * sizeof(int));
for (i = 0; i < n; i++) {
scanf("%d ", &ar[i]);
if (ar2[ar[i]] == 0)
ar2[ar[i]] = 1;
}
for (i = 1; i < 601; i++) {
if (ar2[i] != 0)
count++;
}
printf("%d",count);
return 0;
}
for the first test case (4 1 3 3 2) , it outputs the right answer 3 in ideone.com 's gcc 6.3, but outputs 4 in gcc 5.1 which is used at codeforces.
Why does this happen, and how can I prevent this ?
(I think it's because of memset, but I'm not sure.)
You are defining an array of size n before the value of n has beed determined (note that you scanf the value of n later). This is undefined behaviour, such that different compilers may give different results, and even starting your program on your machine may give different results (including crashes).
instead of
int n, count = 0, i;
int ar[n];
...
write
int n, count = 0, i;
scanf("%d", &n);
int ar[n], ar2[601] = { 0 };
At least the malformed array should then be solved, and ar2 is completely initialized with 0. You can get rid of your memset, which initialized only 600 items (instead of 601) anyway.
Here is a quicker solution to the problem
#include <stdio.h>
int main()
{
bool seen_number_before[601] = { false };
int count = 0;
seen_number_before[0] = true;
int n;
scanf("%d", &n); // Should do error checking here
for (int i = 0; i < n; ++i) {
int v;
scanf("%d", v); // More error checking
if (!seen_number_before[v]) // Not seen it before
seen_number_before[v] = true; // Mark it as seen
++count; // Add to the count
}
}
printf("%d\n", count);
return 0;
}
}
There are some errors in your code, from a[n] when n is not defined.
To check errors try compiling with some useful options:
gcc -Wall code.c -o code -g
the -Wall is for Warning all and the -g is used for debug on valgrind (useful tool to check memory leak and other errors).
Also I suggest you to name properly every var in your code, could helpful for a large size of code base.
This is my solution,
#include <stdio.h>
#include <stdlib.h>
int main(){
int n, count = 0;
scanf("%d", &n);
int *a = malloc(n * sizeof(int));
int hash[600] = { 0 };
for(int i=0; i<n; i++){
scanf("%d ", &a[i]);
if(a[i] != 0){
hash[a[i]] = 1;
}
}
for(int i=0; i<600; i++){
printf("%d ", hash[i]);
if(hash[i] == 1) ++count;
}
printf("\n\n%d\n", count);
return 0;
}
it can be optimized in time, using only one for, and/or in memory by creating an hashset of int, and every int can store 32 bit and do some bitwise operations, so if the nth bit is 1, count++, otherwise don't do nothing.
Here are two functions below that compile perfectly but I seem to be getting a weird error with the very first inputted integer. I have tried debugging in GDB but when it's only the first inputted value that is having this weird error, then it makes things complicated.
#include <stdio.h>
#include "Assg9.h"
#include <stdlib.h>
#include <assert.h>
#include <math.h>
void getPrimes(int usernum, int* count, int** array){
(*count) = (usernum - 1);
int sieve[usernum-1], primenums = 0, index, fillnum, multiple;
for(index = 0, fillnum = 2; fillnum <= usernum; index++, fillnum++){
sieve[index] = fillnum;
}
for (; primenums < sqrt(usernum); primenums++)
{
if (sieve[primenums] != 0){
for (multiple = primenums + (sieve[primenums]); multiple < usernum - 1; multiple += sieve[primenums])//If it is not crossed out it starts deleting its multiples.
{
if(sieve[multiple]) {
--(*count);
sieve[multiple] = 0;
}
}
}
}
int k;
for (k = 0; k < usernum; k++)
if (sieve[k] != 0)
{
printf("%d ", sieve[k]);
}
*array = malloc(sizeof(int) * (usernum +1));
assert(array);
(*array) = sieve;
}
void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
int i;
fprintf(fpout, "There are %d prime numbers less than or equal to %d \n", count, n);
for(i = 0; i < count; i++)
{
if(*(array + i) != 0){
fprintf(fpout, "%d ", *(array + i));
}
}
}
Our Output:
Please enter an integer in the range 2 <-> 2000 both inclusive: 2
2 32664
Do you want to try again? Press Y for Yes and N for No: y
Please enter an integer in the range 2 <-> 2000 both inclusive: 2
2
Do you want to try again? Press Y for Yes and N for No: n
Good bye. Have a nice day
Expected output should obviously just display 2. This is the case for any integer from 2-2000 for the very first inputted integer. The very last, or last 2, prime numbers print very large numbers, sometimes even negative numbers. I have no clue why, but after the first inputted value everything works perfectly. Tried debugging this with GDB like crazy but with no luck. Would really appreciate someone's help for this bizarre error
You aren't initializing the sieves array to 0s. So you're looping from 0 to usernum-1, printing out every number that isn't a 0. Since you didn't initialize the array, the 2nd element is a random value and is being printed out
This code is a problem:
(*array) = sieve;
You are are assigning the address of sieve, a temporary local array, to *array. You need to copy the array contents instead.
Are you also this person who has asked three questions about identical code?