输入一串仅包含 'A' - 'Z'的字符串, 用下面的方法进行重新编码:
-
若子串包含连续k个相同的字符,则改为kX; 其中X为这个子串中唯一的字符.
-
如果子串中字符仅为1个时, 则前面的'1'则省去.
输入
第一行输入为一个整数N (1 <= N <= 100)代表有几组测试数据. 下面紧跟N行字符串. 每个字符串仅包含'A' - 'Z' 并且长度都小于 10000.
输出
对于每个字符串, 输出对应解码后的字符串.
第一种思路
#include<stdio.h>
#include<string.h>
int main()
{
int N,Count_;//N:数据组数 Count_相同的字母个数
char A[10000];//存放测试数据
while(scanf("%d",&N)!=EOF)
{
for(int i=0;i<N;i++)
{
//输入数据
scanf("%s",A);
//初始化Count_
Count_=1;
//输出结果
for(int j=0;j<strlen(A);j++)
{
if(A[j]==A[j+1]) //若A[j]==A[j+1] 则Count_++;
{
Count_++;
}else
{
//不相等则输出
if(Count_==1)
{
printf("%c",A[j]);
//输出一个字符后初始化Count_,这里其实可以不初始化
Count_=1;
}else
if(Count_>1)
{
printf("%d%c",Count_,A[j]);
//初始化Count_
Count_=1;
}
}
}
//输出一组结果后换行
printf("\n");
}
}
return 0;
}
第二种
#include<stdio.h>
#include<string.h>
int fun(char str[])
{ int i,j;
int count=0;
strlen(str);
for(i=0;i<strlen(str);i++)
{
j=i+1;
if(str[i]==str[j])
{
count++;
//printf("%c",str[i]);
//count=0;
}
else
{
if(count!=0)
{
printf("%d%c",count+1,str[i]);
count=0;
}
else
{
printf("%c",str[i]);
}
}
}
printf("\n");
return 0;
}
int main()
{
char str[10000];
int count=0;
gets(str);
fun(str);
return 0;
}
人们常说 Knowledge(知识)= 96 分,Workhard(努力工作)= 98 分,Attitude(态度)= 100 分,这是怎么算的呢,
是把这个单词的每个字母对应成它在字母表出现的位置,A,a对应 1 ,B,b 对应 2 ,C,c 对应 3 ,..., Z,z 对应 26 ,然后加起来。
现在小C想让你帮忙计算一个单词的分数(单词里只包含大写字母和小写字母)。
输入
输入一行包含一个仅由大写字母和小写字母组成的字符串,长度不超过 10^5 。
输出
输出一行,包含 1 个整数,表示这个单词的分数。
#include "stdio.h"
#include "string.h"
int main()
{
char a[100001];
int i, sum = 0;
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>='A' && a[i]<='Z')
sum=sum+a[i]-'A'+1;
else
sum=sum+a[i]-'a'+1;
}
printf("%d",sum);
return 0;
}
现有一有序数列,3,7,5,13,25,45,78,23,8,33,9,19,28,41,50。从键盘输入一个整数,如果既是奇数,又在数列里,则输出其在序列中的位置(3为第1个);如果输入整数是偶数或不在数列里,则输出Error。
#include "stdio.h"
int main()
{
int a[15]={3,7,5,13,25,45,78,23,8,33,9,19,28,41,50};
int N,i;
scanf("%d",&N);
for(i=0;i<15;i++)
{
if(a[i]==N && N%2!=0)
{
printf("%d",i+1);
return 0;
}
}
printf("Error");
return 0;
}
网友评论