2017.12.16

过了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 <stdio.h>
#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[])
{
 char splus[3*N+1];
 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(where!=NULL) //where为第一次遇见相同数组的首元素地址
    {
        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);
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值