A
//提交c++ (这里我们把i定义在了for循环内部
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[1100];
while (gets(a) != NULL){
int len = strlen(a);//a字符串的长度
for (int i = 0; i < len; i ++ ){//判断有没有you有替换成we
// you your yyou只有you可以输出we
//所以我们要找的是一个单词you 这个单词会出现在四个地方
/*
第一种地方:you在开头you后面是空格
第二种地方:you在开头但是这个句子中只有you一个单词所以you后面没有字符
第三种地方:you在中间you的前面是空格后面也是空格
第四种地方:you在结尾you的前面是字符后面没有字符
*/
if (i == 0 && a[i] == 'y'){//1 2种
if (i + 2 < len && a[i + 1] == 'o' && a[i + 2] == 'u'){
if (i + 2 == len - 1){//第二种
printf("we");
i += 2;
}
else if (a[i + 3] == ' '){//第一种
printf("we ");
i += 3;
}
else printf("%c",a[i]);
}
else printf("%c",a[i]);
}
else if (a[i] == ' '){//3 4
if (i + 3 < len&&a[i + 1] == 'y'&&a[i + 2] == 'o'&&a[i + 3] == 'u')
{
if (i + 3 == len - 1){//第四种
printf(" we");
i += 3;
}
else if (a[i + 4] == ' '){//第三种
printf(" we ");
i += 4;
}
else printf("%c",a[i]);
}
else printf("%c",a[i]);
}
else printf("%c",a[i]);
}
puts("");
}
return 0;
}
短的代码
#include <stdio.h>
#include <string.h>
int main()
{
char str[1010] = {0}, ans[1010] = {0};
while (scanf("%s", str) != EOF)
{
if (strcmp(str, "you") == 0) strcat(ans, "we");
else strcat(ans, str);
strcat(ans, " ");
}
ans[strlen(ans) - 1] = '\0';
printf("%s\n",ans);
return 0;
}
B
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[1100], b[1100];
scanf("%s%s",a, b);//strcmp比较函数
/*
字典序:两个字符串a,b比较两个字符串的长度如果字符串的长度不相同
字符串长的字典序大
如果字符串长度相同的话我们比较每一位的字符,如果在i位上a[i] > b[i]
a的字典序大于b,反之a[i] < b[i]的话a的字典序小于b
*/
/*
strcpy复制函数这里是用不了strcpy的
strcat函数连接字符串 s2 到字符串 s1 的末尾
所以如果想用函数来解决这道题的话要用strcat函数
*/
if (strcmp(a, b) < 0){//a和b比较:a 小于 b
int len = strlen(a) + strlen(b);
printf("%d %s%s\n",len, a,b);
}
else if (strcmp(a, b) == 0){//a = b
int len = strlen(a);
printf("%d %s\n",len, a);
}
else if (strcmp(a, b) > 0){//a > b
int len = strlen(b) + strlen(a);
printf("%d %s%s\n",len, b, a);
}
return 0;
}
C
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
scanf("%d",&t);
while (t -- )//循环t次
{
char a[1100];
int st[30] = {0};//把数组初始化为0
scanf("%s",a);
int len = strlen(a);
for (int i = 0; i < len; i ++ ){//字母映射到数字
//a对应的是0 b对应的是1 .....
st[a[i] - 'a'] ++;
}
int maxx = 0, j = 0;
//maxx存的是次数 j存的是最多的那个字符映射到的数字
for (int i = 0; i < 26; i ++){
if (st[i] > maxx){
maxx = st[i];
j = i;
}
}
char c = j + 'a';//数字映射回来变成字符
printf("%c\n",c);
}
return 0;
}
D
#include <stdio.h>
#include <string.h>
int main(void)
{
int n;
scanf("%d",&n);
while (n -- )//循环t次
{
char a[1100];
scanf("%s",a);
int len = strlen(a);
int ans = 0;
for (int i = 0; i < len; i ++ )
{
if (a[i] >= 'a' && a[i] <= 'z')//a[i]是小写字母
ans ++;
}
int k = ans % 26;
if (k == 0) printf("z\n");
else{
//1表示a 2表示b...
k --;
char c = k + 'a';
printf("%c\n",c);
}
}
return 0;
}
E
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
scanf("%d",&t);
while (t -- )//循环t次
{
char a[210];
scanf("%s",a);
int len = strlen(a);
int maxx = 0;//最多出现的次数
char c;//最多出现的字符
for (int i = 0; i < len; i ++ )
{
int ans = 0;
while(a[i + ans] == a[i] && i + ans < len) ans ++;
//从a[i]开始连续的长度
if (maxx < ans){
maxx = ans;
c = a[i];
}
i += (ans - 1);
}
printf("%c %d\n",c, maxx);
}
return 0;
}
F
#include <stdio.h>
#include <string.h>
int main(void)
{
long long ans = 0;//防止超出int范围所以我在这里开longlong
int j = 0,b;
while (scanf("%d",&b)){//输入b
if (b == 0) break;//如果b = 0停止输入
if (b == 1) {
j = 0;
ans ++;
}
else if (b == 2){
//当 j = 0时 +2,当j = 1时 +4,j = 2时 +6 ...
ans += (j + 1) * 2;
j ++;
}
}
printf("%lld\n",ans);
return 0;
}