ACM程序设计(一)
1.1004
题目描述:Calculate a + b.a and b are integers. (0≤a, 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");
}
}
输出:For each test case, print out the sum of a and b.
样例输入
样例输出
//************************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.
样例输入
样例输出
#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");
}
}