UVa 111,111-like Matrioshka and scanf processing line

Source: Internet
Author: User

Title: Nest, Nesting

Ideas: Thinking figured out is actually very simple: the negative pressure stack, when the input positive, check the stack top element, if the top of the stack is positive, then the stack, always check the top of the stack until the top of the stack is negative, to determine whether the number and the top of the stack is the opposite number, and determine whether the sum of those positive sums of the stack is less than the positive number of the input (this is the condition that satisfies the problem of adding less than M). If satisfied, the stack top negative number is out of the stack, and the input positive number is entered into the stack.    This loops to the end, and if you keep matching, only one element is left in the last stack. The idea is clear, but the implementation is more troublesome, mainly EOF and \ n judgment. I always thought that scanf could not be used to distinguish a line, because it would skip whitespace characters. Read someone else's, only to find scanf and GetChar collocation or can handle a row, because scanf each read after is stuck in the mismatched character or white space character, so scanf after using GetChar can be judged to read a line end or EOF or normal place. Here I'm dividing each row into a char+int group (int is read-only when the char is judged not to be wrapped), so that when char is wrapped, the loop condition fails to read the number again, and the last number of the row has been processed in the previous loop. However, this will result in the first int without char, which is handled separately, the case is relatively simple, if the Int+char group is divided (that is, at the end of the while loop to read int and then read char), this will lead to line breaks, the last int is not processed, The same statement needs to be handled in the while loop for more trouble. ( the idea here is to first judge the process, that is, try not to input at the end of the loop ) here, according to the AC program, it should be the line has a newline, not the last line only EOF no line break.

The groove is, at the beginning of the thought that the integer is a number, wrote a function with GetChar () and then return int, found unable to handle the multi-digit number after the change, the more trouble to change. To the end also re ... Find this again to judge EOF inside and Judge \ \ GetChar is very troublesome need very careful. Perhaps like this program, it is better to save each line before processing it.

Summary: scanf followed by GetChar is able to distinguish one row.

Code:

AC   
#include <stdio.h> #include <string.h>int stack[10000];int main () {int num; while (scanf ("%d", &num)! = EOF) {  char c=getchar ();  int flag=1;  memset (stack,0,sizeof (stack));  int top=0;  if (num<0) stack[++top]=num;  else {flag=0;};  while (c!= ' \ n ')  {   scanf ("%d", &num);   if (num<0) stack[++top]=num;   else   {    int sum=0;    while (top>0 && stack[top]>0) {sum+=stack[top--];}    if (num+stack[top]==0&&sum<num) stack[top]=num;    else flag=0;      }          if (flag==0) while ((C=getchar ())! = ' \ n ');   else C=getchar ();        } Whilec    if (flag&&top==1) printf (":-) Matrioshka!\n ");  else printf (":-( Try again.\n ");                          } return 0;   }

re#include<stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h>int stack[   10000];int Getin (int* flag); int main () {int A; int* fg; int next1=1; while ((A=getin (FG)) && Next1) {//*fg==2 is EOF  int next2=1;  if (*fg==2) next1=0;  else if (*fg==1) next2=0;  memset (stack,0,sizeof (stack));  int top=0;  int flag=1;   while (NEXT2) {//*fg==1 is the line break if (*fg==1) next2=0;   else if (*fg==2) {next1=0; break;}   if (a<0) stack[++top]=a;    else {//a>0 int sum=0;    while (top>0 && stack[top]>0) sum+=stack[top--];        if (top>0 &&stack[top]+a==0 && sum<a) {stack[top]=a;} else {flag=0;}   The break is not required here} if (flag==0 && next2) {while ((A=getin (FG)) && *fg==0);      else if (next2!=0) A=getin (FG); }//while if (flag==0| | top!=1) printf (":-(  Try again.\n "); else printf (":-)                           Matrioshka!\n ");   }//while return 0; }int Getin (int* flag) {//read in a positive or negative number, flag flag 1 means read to a line, 2 for ReadEOF Char str[9]; int k=0; Char C=getchar (); while (IsDigit (c) | | |  c== '-') {str[k++]=c;                  C=getchar (); } str[k]= ' + '; *flag=0; if (c== ' \ n ') *flag=1; else if (c==eof) *flag=2; return atoi (str);}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.