大一C语言程序设计练习题(三)


A - ASCII码排序

Description

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input

输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output

对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z

代码如下(示例):

#include <stdio.h>
void main()
{
 char a,b,c,d,x,y,z;
 while(scanf("%c%c%c",&a,&b,&c)!=EOF)
 {

  getchar();
  x=a<b?a:b;
  x=x<c?x:c;
  z=a>b?a:b;
  z=z>c?z:c;
  y=a+b+c-x-z;
  printf("%c %c %c\n",x,y,z);
 }
}

B - 字符串统计

Description

对于给定的一个字符串,统计其中数字字符出现的次数。

Input

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output

对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9

代码如下(示例):

#include <stdio.h>
int main()
{
	char s[1000];
	int i,count,n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		count=0;
		scanf("%s",&s);
		for(i=0;s[i]!='\0';i++)
		{
			if(s[i]>=48&&s[i]<=57)
				count++;
		}
		printf("%d\n",count);
	}	
	return 0;
}

C - 统计元音

Description

统计每个元音字母在字符串中出现的次数。

Input

输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。

Output

对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。
请特别注意:最后一块输出后面没有空行:)

Sample Input

2
aeiou
my name is ignatius

Sample Output

a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1

代码如下(示例):

#include<stdio.h>
#include<string.h>
int main()
{
    char a[200];
    int i, k, n, b[5];
    scanf_s("%d", &n);
    getchar();
    while (n--)
    {
        gets_s(a);
        k = strlen(a);
        for (i = 0; i < 5; i++)
            b[i] = 0;
        for (i = 0; i < k; i++)
        {
            if (a[i] == 'a')
                b[0]++;
            if (a[i] == 'e')
                b[1]++;
            if (a[i] == 'i')
                b[2]++;
            if (a[i] == 'o')
                b[3]++;
            if (a[i] == 'u')
                b[4]++;
        }
        printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", b[0], b[1], b[2], b[3], b[4]);
        if (n != 0)
            printf("\n");
    }
    return 0;
}

D - 回文串

Description

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。

Input

输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。

Output

如果一个字符串是回文串,则输出"yes",否则输出"no".

Sample Input

4
level
abcde
noon
haha

Sample Output

yes
no
yes
no

代码如下(示例):

#include <stdio.h>
#include <string.h>
int main()
{
	int n, i, k;
	scanf_s("%d", &n);
	getchar();
	while (n--)
	{
		char a[100], b[100];
		int x = 0;
		gets_s(a);
		k = strlen(a);
		for (i = 0; i < k; i++)
			b[i] = a[k - 1 - i];
		for (i = 0; i < k; i++)
			if (a[i] == b[i])
				x++;
		if (x == k)printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

E - 让气球升起来!

Description

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

代码如下(示例):

#include<stdio.h>
#include<string.h>
int main()
{
	int N=0;
	int i=0;
	int j=0;
	int tmp=0;
	int math=0;
	int cnt=0;
	char ch[1000][15];
	while(scanf("%d",&N))
	{
		if(N==0)
		return 0;
	for(i=0;i<N;i++)
	{
		scanf("%s",&ch[i]);
	}       
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
			if(strcmp(ch[i],ch[j])==0)
				tmp++;
		if(math<tmp)
		{
			math=tmp;
			cnt=i;
		}
		tmp=0;
	}
	printf("%s\n",ch[cnt]);
	math=0;
	memset(ch, 0, sizeof(ch));
	}
	return 0;	
} 

F - C语言合法标识符

Description

输入一个字符串,判断其是否是C的合法标识符。

Input

输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。

Output

对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。

Sample Input

3
12ajf
fi8x_a
ff ai_2

Sample Output

no
yes
no

代码如下(示例):

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
	int n,i,k,c;
	char a[50];
	while(~scanf("%d",&n))
	{
		getchar();
		while(n--)
		{
			gets(a);
			k=strlen(a);
			if( isalpha(a[0]) || a[0]=='_' )
			{
				c=0;
				for(i=0;i<k;i++)
				{
					if( isalnum(a[i]) || a[i]=='_' )
						c++;
					else
					{
						printf("no\n");
						break;
					}
				}
				if(c==k)
					printf("yes\n");
			}
			else
				printf("no\n");
		}
	}
	return 0;
}

G - 统计字符(选做)

Description

统计一个给定字符串中指定的字符出现的次数

Input

测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到’#'时输入结束,相应的结果不要输出。

Output

对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
c0 n0
c1 n1
c2 n2

其中ci是第1行中第i个字符,ni是ci出现的次数。

Sample Input

I
THIS IS A TEST
i ng
this is a long test string

Sample Output

I 2
i 3
5
n 2
g 2
注:第2个测试用例中,空格也是被统计的字符之一。

代码如下(示例):

#include <stdio.h>
#include <string.h>
int main()
{
	int sum[5]={0,0,0,0,0};
	int i,x,j,k;
	char a[5],b[80];
	while ( gets(a) && strcmp(a,"#") )
	{
		gets(b);
		j=strlen(a);
		k=strlen(b);
		for (i=0;i<j;i++)
		{
			for(x=0;x<k;x++)
				if(a[i]==b[x])
					sum[i]++;
		}
		for (i=0;i<j;i++)
			printf("%c %d\n",a[i],sum[i]);
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(sum,0,sizeof(sum));
	}
	return 0;
}

H - Encoding(选做)

Description

Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:

1. Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.

2. If the length of the sub-string is 1, ‘1’ should be ignored.

Input

The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only ‘A’ - ‘Z’ and the length is less than 10000.

Output

For each test case, output the encoded string in a line.

Sample Input

2
ABC
ABBCCC

Sample Output

ABC
A2B3C

代码如下(示例):

#include<stdio.h>
#include<string.h>
char a[10005];
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int i,la;
        scanf("%s",a);
        la=strlen(a);
        for(i=0; i<la;)
        {
            int k,s=1;
            for(k=i; k<=la; k++)
            {
                if(a[k]!=a[k+1])
                    break;
                s++;
            }
            if(s>1)
                printf("%d",s);
            printf("%c",a[k]);
            i+=s;
        }
        printf("\n");
    }
    return 0;
}

I - Text Reverse(选做)

Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m’I morf .udh
I ekil .mca

Sample Output

hello world!
I’m from hdu.
I like acm.

Hint

Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.

代码如下(示例):

#include <stdio.h>
#include <string.h>

int main()
{
    int n, i, j, len, a, b;
    char str[1000], s[1000][1000], t;

    scanf_s("%d", &n);
    getchar();
    while (n--)
    {
        gets_s(str);
        memset(s, '\0', sizeof(s));
        a = b = 0;
        len = strlen(str);
        for (i = 0; i < len; i++)
        {
            if (str[i] == ' ')
            {
                a++;
                b = 0;
                continue;
            }
            s[a][b] = str[i];
            b++;
        }
        for (i = 0; i <= a; i++)
        {
            len = strlen(s[i]);
            for (j = 0; j < len / 2; j++)
            {
                t = s[i][j];
                s[i][j] = s[i][len - j - 1];
                s[i][len - j - 1] = t;
            }
            printf("%s", s[i]);
            if (i != a)
                putchar(' ');
        }
        printf("\n");
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值