11、 here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to det
12、ermine if it is a particular group's turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.
Both
13、the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47
14、is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)
Write a program which reads in the name of the comet and the name of the group and figures out whether according to the
15、 above scheme the names are a match, printing "GO" if they match and "STAY" if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.
INPUT FORMAT
Line 1:
An upper case character string of length 1..6 that is the na
16、me of the comet.
Line 2:
An upper case character string of length 1..6 that is the name of the group.
COMETQ
HVNGAT
OUTPUT FORMAT
A single line containing either the word "GO" or the word "STAY".
#include
#include
void main()
{
char a[7],b[7];
int i,pa=1,pb=1
17、
gets(a);
gets(b);
i=0;
while(a[i]!='\0')
{
pa=pa*(a[i]-'A'+1)%47;
i++;
}
i=0;
while(b[i]!='\0')
{
pb=pb*(b[i]-'A'+1)%47;
i++;
}
if(pa==pb)printf("GO\n");
else
printf("STAY\n");
}
10.8
18、 大数相加
问题描述: 编写C程序,它能以字符串形式读入两个无符号正整数m和n,计算并输出这两个整数之和
输入格式: 输入由两行组成,第一行为无符号整数m,第二行为无符号整数n,且m和n的值最长25位
输出格式: 输出为一行,即两个无符号整数m和n之和
#include
#include
int main()
{
char a[5001],b[5001];
int s1[5001],s2[5001],k,n=0;
int ans[5001];
int c,ale
19、n,blen,i,maxlen,minlen;
scanf("%s%s",&a,&b);
alen=strlen(a);
blen=strlen(b);
maxlen = alen > blen ? alen : blen;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
for( i=alen-1;i>=0;i--)
s1[alen-i]=a[i]-'0';
for( i=blen-1;i>=0;i--)
20、 s2[blen-i]=b[i]-'0';
memset(ans,0,sizeof(ans));
for(i=1;i<=maxlen;i++)
{
ans[i] += s1[i] + s2[i];
if(ans[i]>9)
{
if(i==maxlen)
maxlen++;
ans[i+1]++;
21、 ans[i]-=10;
}
}
for( i=maxlen;i>=1;i--)
printf("%d",ans[i]);
printf("\n");
if (k!=c)
printf("\n");
return 0;
}
10.9 字符串重排列
判断一个字符串是否可以由另一个字符串通过重排字符而得到。注意,此处区分字符大小写!
输入 输入只有一行,为两个字符串,字符串之间以一个空格分隔。
输出
如果两个字
22、符串由同一组字符组成(且每一个字符出现次数相同),则输出“YES”;
否则输出“NO”。注意YES和NO都是大写字母!
#include
void main()
{
char a[1000],b[1000];
int i,j,k,m=0;
scanf ("%s%s",a,b);
for(i=0;a[i]!='\0';i++)
{ k=0;
for(j=0;b[j]!='\0';j++)
if(a[i]==b[j])
{
b[j]='?';
k++;
m++;
break;
}
23、
if (k==0) break;
}
if (k==0) printf("NO\n");
else printf("YES\n");
}
10.10上课啦!要点名啊!
小凡的老师每次上课前都要点名,但是这样就浪费了老师的上课时间。所以老师让小凡来完成点名,让小凡在早自习的时候就点好名。老师给了小凡名单,小凡只要照着名单点名就好了是不是很简单啊。
输入
输入有多组数据,直到文件结束。每组测试数据有三行,第一行为两个整数m, n(50 >= m >= n)。第二行有m个名字,名字之间用空格隔开,是小凡班上同学的名单。后面有n个名字是来上课的同学。名字间用
24、空格隔开。名字的长度不超过20个字符。
输出
按照第一行的名单,每个人对应输出是否到了。到的人输出Yes,没到的人输出No。
#include
#include
int main()
{
int i,j,m,n,p;
char a[5][50],b[5][50];
scanf("%d%d",&m,&n);
for(i=0;i25、)
scanf("%s",b[i]);
for(i=0;i26、 }
if(j==n)
printf("NO\n");
}
}
10.11找第一个只出现一次的字符
问题描述:
给定t个字符串,这个字符串只可能由26个小写字母组成,请你找到第一个仅出现一次的字符,如果没有符合要求的字符,就输出no。
输入: 第一行是t,接下来是t个字符串,每个字符串长度小于100
输出:
你的输出需要由t行组成。
对于每个字符串,输出第一个仅出现一次的字符,没有输出NO。
#include
#include<
27、string.h>
int main()
{
int i,j,n,t,m,b[200]={0};
char a[1000][1000];
scanf("%d",&t);
for(i=0;i28、 {
b[j]++;
}
}
}
for(j=0;a[i][j]!='\0';j++)
{
if(b[j]==1)
{
printf("%c\n",a[i][j]);
break;
}
}
if(j==m)
printf("NO\n");
for(j=0;a[i][j]!='\0';j++)
b[j]=0;
}
}
10.12 提取数据
输入一个字符串,长度不超过30,内有数字字符和非数字字符,统计其中包含了多少个非负整数,并输出这样的非负整数。
输入
29、
一个字符串,最大长度为30
输出
输出字符串中包含的数据,一个数据一行. (不用输出总数)
#include
int main()
{
int i,sum=0;
char a[30];
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
sum=sum*10+(a[i]-'0');
if((a[i]>='0'&&a[i]<='9')&&(a[i+1]<'0'||a[i+1]>'9'))
{
printf("%d\n",sum);
30、sum=0;
}
}
}
10.13 判断字符串是否为回文
编程,输入一个字符串,输出该字符串是否回文。
输入
输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。
输出
如果字符串是回文,输出yes;否则,输出no。
#include
#include
int main()
{
int i,j,t,p=0;
char a[100];
gets(a);
t=strlen(a);
j=t-1;
for(i=0;i<=(t/2-1);i++)
{
if(a[
31、i]!=a[j])
{
p=1;
break;
}
else j--;
}
if(p==1)
printf("no\n");
else printf("yes\n");
}
10.14 首字母大写
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
输入
输入一行:待处理的字符串(长度小于80)。
输出
输出一行:转换后的字符串。
#include32、h>
int main()
{
int i;
char a[100];
gets(a);
if(a[0]>=97&&a[0]<=122)
a[0]=a[0]-32;
for(i=1;a[i]!='\0';i++)
{
if(a[i]==' '&&a[i+1]>=97&&a[i+1]<=122)
a[i+1]=a[i+1]-32;
}
puts(a);
}
10.15 绕口令
规则是:主持人给出一串字符串,要求把这串字母简化。该串字符串全部为小写英文字母。
比如:aaabbbaa,则简化为3a3b2a;zzzz
33、eeeeea,则简化为4z5e1a。依次类推。
Input
第一行为一个整数n,表示共有n组测试数据(1<=n<=100)。每组测试数据有一行,该行第一个数为字符串长度t( t <= 1,000,000),然后为一行长度为t的字符串。
Output
对于每组输入数据输出一行,即简化后的字符串。
#include
#include
int main()
{
int i,n,t,l,j,k=0,count;
char a[100][100];
scanf("%d",&n);
for(i=0;i34、anf("%d",&t);
scanf("%s",a[i]);
}
for(i=0;i35、删除指定的字符。同一字母的大、小写按照不同的字符处理。只需要提交fun函数
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
void fun(char str[100],char ch)
{
int i,count=0;
for(i=0;str[i]!='\0';i++)
if(str[i]==ch)
str[i]='0';
for(i=0;str[i]!='\
36、0';i++)
if(str[i]!='0')
str[count++]=str[i];
str[count]='\0';
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
int main()
{
char str[80];
char ch;
gets(str);
scanf("%c",&ch);
fun(str,ch);
printf("%s\n",str);
return 0;
}
/* PRESET CODE END
37、 - NEVER TOUCH CODE ABOVE */
10.17处理字符串
编写函数fun,其功能是:将数组s存放的字符串中的所有数字字符移到所有非数字字符之后,并保持数字字符串和非数字字符串原有的先后次序。
例如,s中的字符串为:def35adh3kjsdf7。执行结果为:defadhkjsdf3537。
提醒:本题提交的时候,只需要提交fun函数就可以。
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include
/* PRESET CODE END - NEVER TOUCH CODE ABOV
38、E */
void fun(char s[])
{
char a[80],b[80];
int i,j,x=0,y=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='0'&&s[i]<='9')
{
a[x]=s[i];
x++;
}
else
{
s[y]=s[i];
y++;
}
}
a[x]='\0';
s[y]='\0';
strcat(s,a);
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
int main()
{
char s[80];
gets(s);
fun(s);
puts(s);
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
20