Tag Archives: The c language

P – Random Teams




mathematical knowledge

#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;
int main()
{
	int t;
	long long m,n;
    cin>>n>>m;
    
    	cout<<(m-n%m)*(n/m)*(n/m-1)/2+(n%m)*(n/m+1)*(n/m)/2<<" "<<(n-m+1)*(n-m)/2<<endl;
    
  
	return 0;
}

Assign the intersection combination of two linked lists to linked list a

typedef struct node
{
    int data;
    struct node *next;
} LNode, *LinkList;

LinkList Unions(LinkList la, LinkList lb)
{
    LNode *p, *q, *w, *u;
    p = la->next;
    q = lb->next;
    w = la;
    while (p && q)
    {
        if (p->data == q->data)
        {
            w->next = p;
            w = w->next;
            p = p->next;
            u = q;
            q = q->next;
            free(u);
        }
        else if (p->data < q->data)
        {
            u = p;
            p = p->next;
            free(u);
        }
        else
        {
            u = q;
            q = q->next;
            free(q);
        }
    }
    if (q)
    {
        p = q;
    }
    while (q)
    {
        u = q;
        q = q->next;
        free(u);
    }
    w->next = NULL;
    free(lb);
    return la;
}

Waitpid call return error prompt: no child processes problem

The problem
An error occurred in a function today with a probabilistic waitpid call. The error is No child processes. The prompt does not have this child process, the PID number can also be wrong, so add the print, when the duplicate found that the PID number can correspond to, no problem.
online, found “No child” the processes of error code corresponding ECHILD, waitpid there is in man’s document, if the process set the SIGCHLD signal processing to SIG_IGN, then the call will return ECHILD waitpid.
to see the code, the parent process does have to capture the SIGCHLD signal inside, for processing way

rc = waitpid(-1, &status, WNOHANG);

Wait for any child process to exit so that it can be recovered. I suspect that calling WaitPid again when the parent process has already collected its body will be an error. And that explains the probabilistic problem. So write the following code to verify it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

void sig_handle(int sig)
{
    waitpid(-1, NULL, 0);
    printf("llm->%s(%d)\n", __FUNCTION__, __LINE__);
}

int main(int argc, char *argv[])
{
    int rtn = 0;
    int pid = 0;
    char *arg[] = {"date", NULL};
    signal(SIGCHLD, sig_handle);
    //signal(SIGCHLD, SIG_IGN);

    while(1)
    {
        pid = fork();
        if(!pid)
        {
            execvp("date", arg);
            exit(1);
        }
        usleep(10*1000);
        rtn = waitpid(pid, NULL, 0);

        if(rtn < 0)
            perror("waitpid");
        //usleep(10*1000);
    }
    return 0;
}

If you add sleep before waitpid, the problem will be 100% duplicated. If you remove sleep, there will be no problem.
The solution
Given the current code logic, there is a problem with either Waitpid being removed, so the best course of action is to determine whether it is true or false. If Waitpid returns ECHILD, then ignore the error.
The system calls
This is not a problem with the system call. This is not a problem with the implementation of waitpid after fork. The implementation handles the signal accordingly. The SIGCHLD signal processing action is restored before fork, as follows:

int __libc_system(char *command)
{
	int wait_val, pid;
	__sighandler_t save_quit, save_int, save_chld;

	if (command == 0)
		return 1;

	save_quit = signal(SIGQUIT, SIG_IGN);
	save_int = signal(SIGINT, SIG_IGN);
	save_chld = signal(SIGCHLD, SIG_DFL);

	if ((pid = vfork()) < 0) {
		signal(SIGQUIT, save_quit);
		signal(SIGINT, save_int);
		signal(SIGCHLD, save_chld);
		return -1;
	}
	if (pid == 0) {
		signal(SIGQUIT, SIG_DFL);
		signal(SIGINT, SIG_DFL);
		signal(SIGCHLD, SIG_DFL);

		execl("/bin/sh", "sh", "-c", command, (char *) 0);
		_exit(127);
	}
	/* Signals are not absolutly guarenteed with vfork */
	signal(SIGQUIT, SIG_IGN);
	signal(SIGINT, SIG_IGN);

#if 0
	printf("Waiting for child %d\n", pid);
#endif

	if (wait4(pid, &wait_val, 0, 0) == -1)
		wait_val = -1;

	signal(SIGQUIT, save_quit);
	signal(SIGINT, save_int);
	signal(SIGCHLD, save_chld);
	return wait_val;
}
weak_alias(__libc_system, system)

Vs2017 C + + cannot open source file: “sdkddkver. H”, “stdio. H”, “TCHAR. H”

The first time it was installed, the error in the picture appeared. Searched for this for a long time, thought is the installation problem, uninstall reinstall several times…… Thirty grams of stuff, it’s no joke.

But finally found that the reason for this problem is!! In the case of Visual Studio default installation, there are files that are not checked by default, that is, they are not installed by default. (Also should be because Win10SDK installation failed, but how to install also can not install, even if…

, causing problems)
But!!!!! H and stdio.h exist in these files.
It is not installed by default, as shown in the figure below:

Because oneself is this pit for a long time, think the net search is all sorts of separate download, the latter search, and then reconfigure and so on incomparable trouble operation. Here are some of the easiest ways to do it once and for all

Solution:
Just go to Programs and Functions, go to VS, right-click, modify, and check those, and you’re done.


Right-click and go to Change
Then you can see:

Then, click More and Select – Modify (check the box). After the completion of the picture, found that I forgot that several are unsolved problems ticked)

C++, check, and then confirm the modification OK, wait for the installation, problem solved. You don’t have to go to download WindowsSDK yourself.

Solve!
(after this can be used, my development needs can basically meet, but this is the installation of Win10SDK failure, the helpless move)

Separate installation, you can reference this article: http://blog.csdn.net/hhh1108/article/details/50352027
There is also a point of benefit, so you can install to other disks. Configure it yourself. VS will be installed on the C drive.

Of course, if you can install the Win10SDK on your computer, you should not have this problem.

The problem of flash back by pressing enter window when debugging or executing program in Visual Studio C

In the process of learning C with Visual Studio, we sometimes finished typing the code and found that the program could execute as well. But is it ever frustrating to press the enter key to execute the next command in the program and see the window flash back?Don’t worry. Just type the following simple code.
Just add the last line:
system(” pause “);
, don’t forget “; “Oh.
# include< stdio.h>
#include< stdlib.h>
main()

y>oplaceholder0;
*************;
*************;
system (” pause “); // Don’t forget the semicolon
}

Flash back after vs compiler running, processing method

How to solve the problem of flash back!!!!!!
I habitually deal with this problem in three ways
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –
1, system (” pause “);
2, getchar ();
3, Ctrl + F5
Let’s discuss the pros and cons of each separately: Welcome to the land of the Kings
System (” pause “) : need to include the preprocessing header #include< stdlib.h> ||#include< windows.h>
Usage: put return (); Before the return statement, otherwise there is no meaning
Advantages: solve the flashback problem, small side effects
Disadvantages: call system function, large memory overhead

Getchar () :

Usage: put return (); Before the return statement, otherwise there is no meaning
Advantages: low overhead, receive a character to end the program
Cons: Use it on a case-by-case basis, because getchar (); You need to accept a string to represent the end, and some functions explicitly need to accept a string as the return value, if you use getchar (); To solve the flashback problem, which can be confusing and introduce errors when the program is running.
can see, although can solve the running results of the flashback problem, but in the processing is completely different, although in the operation is also approximately the same, but in the memory, CPU and other levels, overhead is sometimes different, so you can choose according to your preferences for a way for you !!!!! System (” pause “) is recommended; This way, the introduction and processing of files is also more careful, if there is a heavy use of system functionality is, of course. windows.h> #include< stdlib.h> Of course, it is suitable for those who use a lot of library functions

 

C language — to solve the problem of program flashback when programming (in VS)

How can we make the application pause so that we can see the effect
First let’s use a simple program to explain the flashback problem, when we run the following program, we do not see the actual display effect
We can only see the result if we pause the program, so how do we pause the program?We will solve this problem in three ways.

Return 0; return 0; Add getchar() to the preceding line;
This function is waiting for input of a character, not input will always wait, so play a pause effect, to exit any key can!

View the results after running: press any key to exit after running

Return 0; return 0; I’m going to add while(1) to the front line;
Infinite loops don’t let you quit the program, you’re stuck here all the time, which is very useful. The only way to exit the program is to click the close button X in the black window in the upper right corner

View the results after running, only click the close button to end the program running.

Third, you need a special Windows header file and then add the statement system (” pause “).
Note: this method is more time-consuming, more harmful, try not to use this method

The result of operation,

 

Some common problems in the use of vs2017

1. Compile in VS2017 environment. When using scanf, write scanf as scanf_s and you can input from the keyboard.
When using VS2017, the debug window will flash and you need to write a header file #include <; stdlid.h> , and then in return 0; Before writing system ("pause"); This phenomenon can be avoided. Three, the following is a few classic examples 1. Can receive keyboard characters, if it is lowercase, then output uppercase; If it is uppercase, output lowercase; If it is a number, it is not printed
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
		if (ch >= 65 && ch <= 90)
		{
			ch = ch + 32;
			putchar(ch);
		}
		else if (ch >= 97 && ch <= 122)
		{
			ch = ch - 32;
			putchar(ch);
		}
		else if (ch >= '0' && ch <= '9')
		{
			;
		}
		else
		{
			putchar(ch);
		}
	}
	system("pause");
	return 0;
}

2. For example
Output a diamond

#include<stdio.h>
#include<stdlib.h>
int main()
{
     int line = 0;
     int i = 0;
     scanf_s("%d", &line);
     for (i = 0; i < line; i++)
     {
          int j = 0;
          for (j = 0; j < line-1-i ; j++)
          {
               printf(" ");
          }
          for (j = 0; j < 2 * i + 1; j++)
          {
               printf("*");
          }
          printf("\n");
     }
     for (i = 0; i < line-1; i++)
     {
          int j = 0;
          for (j = 0; j <=i; j++)
          {
               printf(" ");
          }
          for (j = 0; j < (line-1-i)*2 - 1; j++)
          {
               printf("*");
          }
          printf("\n");
     }
     system("pause");
     return 0;
}

 

Cause of runtime error on OJ

Except for the five cliches
1. Array size is too small, resulting in access to the memory should not be accessed
2. A division by zero error occurred
3. A large array is defined inside a function, causing the program stack to run out
4, The pointer is used incorrectly, resulting in access to the memory should not be accessed
5. It is also possible that the program threw an unreceived exception
The author also found that sometimes open array on OJ is too large (global variables, can run normally in the local) will cause runtime error, we need to pay attention to.

When IntelliJ idea is programmed, 1. Black thick line or black cursor appears; 2. The solution of carriage return unable to wrap. (practical recommendation method 2)

Intellij idea programming 1. Black thick lines or black cursor 2. A solution that cannot wrap a line by carriage return. (Practical Recommendation Method 2)

reason: it is usually caused by accidentally pressing insert. Some computers abbreviate insert, putting Del and Ins together, and Ins is the insert key.
Method 1. Click Intellij Idea on the upper left corner of the main interface, File> Settings> The diagram below * * * *
Put him on the Use block caret, and you can get back to your old self. ***

Method 2 (recommended) : Press and hold the Fn+ INSERT key to restore directly.
note: do not press Fn+insert if Use block caret is checked, otherwise the enter key will fail to wrap.
PS: restore the enter enter key newline function solution:
cancel Use block caret in the check box (default is no check, no change can be ignored), and then hold down Fn+insert key, so that the cursor and return will return to the original.