过了10天才比较正式的做题,内疚。。。
ECNU_OJ 3160(无聊的水题)
统计并输出一段文本的字符个数 (不包括换行符),行数以及最长一行的长度。输入保证最后一个字符是换行符。输出的 3 个数字以逗号分隔。
例如:
Input:
The C
Programming Language
Output:
25,2,20
c++:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; string s; int len,maxl,numl,rst; int main() { while(getline(cin,s)){ len=s.length(); if(len>maxl)maxl=len; ++numl,rst+=len; } cout<<rst<<','<<numl<<','<<maxl; return 0; }
c:
#include<stdio.h>
#include<string.h>
int main()
{
int countall=0,sizeline=0,countline=0,lenmax=0;
char c;
while(scanf("%c",&c)!=EOF)
{
if(c!='\n')
{
countall++; //all characters
sizeline++; //characters of one line
}
else
{
countline++; //num of lines
if(sizeline>lenmax)
lenmax=sizeline;
sizeline=0;
}
}
printf("%d,%d,%d",countall,countline,lenmax);
return 0;
}
ECNU_OJ 3445 循环有点烦(特殊情况)
题目:
//********** Specification of replace **********
void replace(chars[],char x[],char y[]);
/* Precondition: s, x and y are three strings,
and s has enough memory to store modified string
Postcondition: replace all substring x with y in string s
*/
void replace(chars[],char x[],char y[]){ //TODO: your function definition
}
#include <stdio.h>
#define N 80
int main(){
char s[3* N+ 1],x[N+ 1],y[N+ 1];
scanf("%s%s%s",s,x,y);
replace(s,x,y);
printf("%s\n",s);
return 0;
}
C:
#include <string.h>
#define N 80
void replace(char s[], char x[], char y[]);
int main()
{
char s[3 * N+ 1], x[N + 1], y[N + 1];
scanf("%s%s%s", s, x, y); //scanf %S can not read BLANK SPACE
replace(s, x, y);
printf("%s\n", s);
return 0;
}
void replace(char s[], char x[], char y[])
{
int i=0;
while(i<(3*N+1))
{
splus[i++]=0;
}
char *v=s;
char *where=strstr(s,x);
int lenx=strlen(x);
int leny=strlen(y);
char *splusp=splus;
{
while(v!=where)
{
*splusp=*v;
v++;
splusp++;
}
*splusp='\0'; //v和where应该同步变化,where提供应交换的地址,v向新数组传递数据
v+=lenx;
strcat(splus,y);
splusp+=leny; //splusp为新数组新生成数的地址,应在strcat函数后加上y的长度
where=strstr(where+lenx,x);
}
if(strlen(v)>0){ //where此时为NULL,但是v还没有到达s数组的末尾,说明漏了数据
strcat(splus,v);
}
strcpy(s,splus);
}