hihocoder1871: Heshen's Account Book

http://hihocoder.com/problemset/problem/1871

描述

Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole country's wealth by corruption. So he was known as the most corrupt official in Chinese history. But Emperor Qianlong liked, or even loved him so much that was not punished during Qianlong's regime even everybody knew he was totally corrupted.

After Qianlong quit his job, his son Jiaqing took the throne. The new Emperor hated Heshen very much, so he threw Heshen into jail and awarded him death immediately after Qianlong died. Of course Jiaqing would not forget to raid Heshen's house for money --- that was the story of "Heshen fell, Jiaqing full."

Jiaqing's man got a notebook from Heshen's home which was obviously an account book.But the text of the book was written in English! Jiaqing thought all numbers in that account book should stand for money. Please find out all numbers for Jiaqing.

The text of the account book consists only lower case letters, spaces, and digits
('0' - '9'). One or several consecutive digits form a number. But Jiaqing only cared about the ACTUAL numbers among all numbers. Only if a number DOESN'T satisfy any of the conditions below, it is an ACTUAL number:

1) The character to the left of the number is a lower case letter, for example: a123

2) The character to the right of the number is a lower case letter, for example: 123b

3) The number has one or more extra leading zeros, such as 01 , 0012….

Please note that if the last character of a line is a digit, and the first character of the next line is also a digit, those two digits are considered consecutive.

输入

There are no more than 200 lines. The length of each line is no more than 1000 characters.

And it is guaranteed that every number's length is no more than 18.

There may be spaces at the end of a line, and those spaces matter.

No blank lines in the input. A line consisting of only spaces is not a blank line.

输出

Print all ACTUAL numbers in a single line in the original order.
Then, count the number of ACTUAL numbers of each line, and print them. A number X only belongs to the line which contains the first digit of X.

样例解释

We assume there is no spaces at the end of each line in the Sample Input.

In the sample input, the '3' at the end of the second line and the '2' at the beginning of the third line are considered consecutive, so "1323" is a number.  But this number only belongs to the second line, so the third line has only 2 numbers ---- 14 and 344..

样例输入

a19 01 17b
12 bdc 13
23 14 344 bc

样例输出

12 1323 14 344
0
2
2

题意:

有多行字符串,现在要求找出其中数字的个数。

有以下条件的不算成数字:

(1)第一个为小写字母如a123

(2)最后一个为小写字母如415sd

(3)有前补0的如001,023

 

单独的一个0算是一个数字,10dh123也算数字10123。

把所有字符串连接起来,再把每个字符串开头位置记下来,从头到尾遍历一遍即可。

#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 1020
char str[N];
char strs[N*1000]; 
long long ans[1000*N];    //符合条件的数字 
int te[N];               //每行字符串开头位置 
int data[N];		//每行有多少数字 
int main()
{
	int len,lens,s=0,i,j,temp,anslen,start;
	long long sum;
	while(gets(str)!=NULL)
	{
		s++;	
		lens=strlen(strs);
		if(s==1||strs[lens-1]>='0'&&strs[lens-1]<='9'&&str[0]>='0'&&str[0]<='9')
		{
			strcat(strs,str);
			te[s]=lens;          //记录字符串开头位置 
		}
		else
		{		
			strs[lens]=' ';
			strs[lens+1]='\0';
			strcat(strs,str);
			te[s]=lens+1;
		}
	}
	lens=strlen(strs); 
	strs[lens]=' ';
	strs[lens+1]=' ';
	strs[lens+2]='\0';
	te[s+1]=99999999;
	lens=strlen(strs);           //总字符串长度 
	len=0;                     //数字长度 
	anslen=0;                   //数字数量 
	temp=0;                 
	start=0;                     //当前数字头位置 
	sum=0;
	for(i=0;i<lens-1;i++)	
	{
		if(strs[i]==' ')
		{	
			start=i+1;
			continue;
		} 
		if(strs[i]>='a'&&strs[i]<='z'&&len==0)
		{
			for(j=i;j<lens;j++)
			{
				if(strs[j]==' ')
				{
					i=j;
					start=i+1;
					break;
				}
			}
			continue;
			if(j==lens)
			break;
		}	
		
		if(strs[i]>='0'&&strs[i]<='9')
		{
			sum=sum*10+(strs[i]-'0');
			len++;
		}	
		
		if(strs[i+1]==' ')
		{
			
			if(strs[i]>='a'&&strs[i]<='z')
			{
				sum=0;
				len=0;
				start=i+1;
				continue;
			}
			if((len==1&&sum==0)||((long long)(sum/pow(10,len-1))))
			{
				ans[anslen]=sum;
				anslen++;
				for(j=1;j<=s+1;j++)
				{
					if(te[j]>start)
					{
						data[j-1]++;
						break;
					}
				}
			}
			start=i+1;
			sum=0;
			len=0;
			continue;
		}
	}
	for(i=0;i<anslen-1;i++)
		printf("%lld ",ans[i]);
	if(anslen)
		printf("%lld\n",ans[anslen-1]);

	for(i=1;i<=s;i++)
		printf("%d\n",data[i]);
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值