codeforce727b---Bill total Value (string-handling regular expression)

Source: Internet
Author: User

Let's start with the regular expression.

%*[a-z] means ignoring the preceding lowercase characters,%[0-9] means to deposit the contiguous number of non-characters into the T string;

Get "123" from "abc123de4f";

#include <stdio.h>int main () {    char s[50] = "abc123de4f", t[50] = {0};    SSCANF (S, "%*[a-z]%[0-9]", t);    printf ("%s\n", t);//-Result: 123    return 0;}

Get "12.3" from "abc12.3de4f"

#include <stdio.h>int main () {    char s[50] = "abc12.3de4f", t[50] = {0};    SSCANF (S, "%*[^0-9]%[0-9.]", t);    printf ("%s\n", t);//-Result: 12.3    return 0;}

  

Get "@qq. com" from "[email protected]"

#include <stdio.h>int main () {    char s[50] = "[email protected]", t[50] = {0};    SSCANF (S, "%*[^@]%s", t);    printf ("%s\n", t); results: @qq. com    return 0;}

  Get "qq.com" from "[email protected]"

#include <stdio.h>int main () {    char s[50] = "[email protected]", t[50] = {0};    SSCANF (S, "%*[^@]@%s", t);    printf ("%s\n", t);//-Result: 12.3    return 0;}

There are a lot of things you can try slowly.

Title Link: Http://codeforces.com/contest/727/problem/B

Test instructions: There is a purchase order in the form of a string, in order to include the "Item name + Price", there is no "+" number, the item name is composed of lowercase English characters, the price has two forms, with cents and no cent, of which with cent is a decimal point plus two digits of the structure

, the dollar part is divided by the decimal point every 3 bits, for example, 123.45 for $123.45, 12.345 for $12345, and 1.234.45 for $1234.45, which now requires the sum of the prices of all items

The output format is processed according to the input format;

There are two types of output formats:

1. All integers, no. 00 this form

2. There are fractional parts, the fractional part is not 0 reserved two decimal places;

#include <stdio.h>#include<string.h>#defineN 5200intMain () {Chars[n]={0}, next[n]={0}, p[n]={0}, T[n] = {0};  while(SCANF ("%s", s+1) !=EOF) {s[0] ='a';///The rules for the following regular expressions are the beginning of a character, ending with a number;        Doublesum =0; intf =0; ///The regular expression asks the number part in the s string to be stored in the form of a string in P, and the remainder is deposited in next; ///         while(SSCANF (s),"%*[^0-9]%[0-9.] %[^\0]", p, next) >0)        {            DoubleNUM1 =0, num2 =0;///NUM1 is the value of fractional parts, and num2 is the value of the integer part;            intlen = strlen (p), flag =0; if(len>3&& p[len-3] =='.') {sscanf (P+len-3,"%LF", &AMP;NUM1);///converts a decimal part into a double;Flag = f =1;///description is 2 decimal places;            }            if(Flag = =1) len = len-3;  for(intI=0; i<len; i++)///processing integer parts;            {                if(P[i] = ='.')Continue; Num2= num2*Ten+ (p[i]-'0'); } Sum+ = Num1 +num2; strcpy (S, next);///cyclic processing of the remaining parts;Memset (Next,0,sizeof(next));///to be emptied;} sprintf (T,"%.2f", sum);///And then save the result as a string to facilitate the output;        intLen = strlen (t), k =0; CharAns[n];///Save the results;        if(f = =0) Len-=3;///having no part of a small number;        if(f = =1)///2 decimal places;{ans[k+ +] = t[--Len]; Ans[k+ +] = t[--Len]; Ans[k+ +] = t[--Len]; if(ans[0] =='0'&& ans[1] =='0') K =0;///the decimal part is divided into 0, not output;        }         for(inti=len-1, counts=1; i>=0; I--, counts++)        {            if(counts%3==1&& counts!=1) Ans[k++] ='.'; Ans[k++] =T[i]; }         for(inti=k-1; i>=0; i--) printf ("%c", Ans[i]); printf ("\ n"); }    return 0;}
View Code

codeforce727b---Bill total Value (string-handling regular expression)

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.