ACM程序设计(一)

5 篇文章 0 订阅
1 篇文章 0 订阅

ACM程序设计(一)

1.1004

题目描述:Calculate a + b.a and b are integers. (0a, b≤1030).

输入:The input may contain several test cases.In each test case, 

there are two integers, a and b, separated by a space.
Input is terminated by EOF.

输出:For each test case, print out the sum of a and b.

样例输入

1234 5678
45 56
456129871123456789 789765412765432121

样例输出

6912
101
1245895283888888910
//************************1004***************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char a[31],b[31],c[31],smallStr[31],bigStr[31];
	int small,big,temp,str1_len,str2_len,i,j,goHead=0,k;
	while(scanf("%s %s",a,b)!=EOF)
	{
		str1_len=strlen(a);//输入a的长度
		str2_len=strlen(b);//输入b的长度
		i=str1_len;
		j=str2_len;
		if(i>=j) 
		{
			small=j;
			big=i;
			for(i=0;i<big;i++)
			{
				bigStr[i]=a[i];
			}
			for(i=0;i<small;i++)
			{
				smallStr[i]=b[i];
			}
		}
		else 
		{
			small = i;
			big = j;
			for(i=0;i<big;i++)
			{
				bigStr[i]=b[i];
			}
			for(i=0;i<small;i++)
			{
				smallStr[i]=a[i];
			}
		}
		for(k=big;k>0;k--)
		{
			if(small>0)
			{
				temp=bigStr[k-1]-48+smallStr[small-1]-48+goHead;
				small--;
			}
			else
			{
				temp=bigStr[k-1]+goHead-48;
			}
			if(temp>=10)
			{
				goHead =1;
				c[k] = temp+38;
			}
			else
			{
				goHead=0;
				c[k] = temp+48;
			}
		}
		c[big+1]='\0';
		if(goHead==1)
			c[0]=49;
		else
		{
			for(i=0;i<=big;i++)
			{
				c[i]=c[i+1];
			}
		}
		printf("%s\n",c);
		goHead=0;
	}
}

2.1011

题目描述

A word defined as a string that contains only letters. Its length is defined as the string length. Two adjacent valid words are separated by spaces or (and) digits.

Write a program to figure out the longest word and its length in a one-line text. Suppose the text is shown by a string, comprised of letters, spaces and digits.

输入

Several strings, one string on each line (the number of characters on each line does not exceed 200). Each string is a test case (text). Input is terminated by EOF.

输出

For each test case, print out the longest word and its length with the format: word[space]:[space]length.

If there are n (n >= 2) words have the same longest length, print them with the format:

word1[space]:[space]length, [space]word2[space]:[space]length, [space]……, [space] wordn[space]:[space]length.

样例输入

Without such efforts the observers pointed out even such an accord would
not have been possible given the vastly different positions of the developed
and developing nations The world could hardly afford a no deal scenario
what with 119 global leaders attending the talks during the final stages

样例输出

observers : 9
different : 9, positions : 9, developed : 9
developing : 10
attending : 9

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char line[200];
	char getN;
	int first[200];
	int length[200];
	int i,n,num1,j,k,num2;
	int tempf,templ,m,flag;
	while(scanf("%[^\n]",line)!=EOF)
	{
		gets(&getN);
		k=-1;m=1;n=0;
		memset(first,0,sizeof(first));
		memset(length,0,sizeof(length));
		//scanf("%[^\n]",line);
		num1=strlen(line);
		for(i=0;i<num1;)
		{
			if(line[i]!=' ')
				i++;
			else
			{
				if(n==0){first[n]=0;length[n]=i;}
				else
				{
					first[n]=k+1;
					length[n]=i-first[n];
				}
				k=i;
				i++;
				n++;
			}
		}
		if(i==num1)
		{
			first[n]=k+1;
			length[n]=i-first[n];
		}
		num2=sizeof(length)/sizeof(int);
		for(i=0;i<num2;i++)
		{
			if(length[i]==0){flag=i;break;}
		}
		//printf("%d\n",flag);
		for(i=0;i<flag;i++)
			for(j=0;j<flag-i-1;j++)
			{
				if(length[j]<length[j+1])
				{
					templ=length[j+1];
					length[j+1]=length[j];
					length[j]=templ;


					tempf=first[j+1];
					first[j+1]=first[j];
					first[j]=tempf;
				}
			}
			//	for(i=0;i<flag;i++)printf("%d %d\n",first[i],length[i]);
			for(i=0;i<flag;i++)
			{
				if(length[i]==length[i+1]){m=i+2;}
				else break;
			}
			if(m==1)
			{
				for(j=0;j<length[0];j++)
					printf("%c",line[first[0]+j]);
				printf(" : %d",length[0]);
			}
			else
			{
				for(i=0;i<m;i++)
				{


					for(j=0;j<length[i];j++)
					{
						printf("%c",line[first[i]+j]);
					}
					if(i==m-1)
						printf(" : %d",length[i]);
					else printf(" : %d, ",length[i]);
				}
			}
			printf("\n");
	}


}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值