Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

C语言练习一

Jacob posted @ Apr 06, 2017 06:20:17 PM in C语言 with tags C语言 排序 bubble 小白进阶 哈工大 , 1287 阅读

1.

奥运参赛国出场次序:
输入奥运会参赛国国名,并按照字典序对其进行排序。
要求:参赛国数量不超过150个,每个国家的名字不超过9个字符。
提示:‘\0’占一个字符。

#include <string.h>
#include <stdio.h>
#define N 151
#define MAX_LEN 10
void SortString(char str[][MAX_LEN], int n);

int main()
{
    int i, n;
    char name[N][MAX_LEN];
    printf("How many countries?");
    scanf("%d",&n);
    getchar();
    printf("Input their names\n");

    for(i=0;i<n;i++)
        gets(name[i]);

    SortString(name, n);
    printf("Sorted results:\n");
    for(i=0;i<n;i++)
    {
        puts(name[i]);
    }
    return 0;

}

void SortString(char str[][MAX_LEN], int n)
{
    int i,j;
    char temp[MAX_LEN];

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(strcmp(str[j],str[i])<0)
            {
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }
        }
    }
}

2.

统计正整数中指定数字的个数
从键盘输入一个正整数number,求其中含有指定数字digit的个数。例如:从键盘输入正整数number=1222,若digit=2,则1223中含有 3个2,要求用函数实现。
函数原型为:int CountDigit(int number,int digit);

程序运行结果示例1:
Input m,n:
1222,2↙
3

程序运行结果示例2:
Input m,n:
1234,6↙
0

输入格式: "%d,%d"
输出格式:
输入提示信息:"Input m,n:\n"
输出格式:"%d\n"

#include<stdio.h>
int CountDigit(int number,int digit);
main()
{
    printf("Input m,n:\n");
    int m,n;
    scanf("%d,%d",&m,&n);
    printf("%d\n",CountDigit(m,n));
}
int CountDigit(int number,int digit)
{
    int c=0;
    while(number)
    {
        if(digit==number%10)
        {c++;
        number=number/10;
        }
    }
    return c;
}

3.冒泡程序

#include<stdio.h>
#define LEN 10
void bubble(int dat[], int length);
main( )
{
    int  data[LEN]={0}, i;
    for (i=0; i<= LEN-1; i++)
    {
        scanf("%d",&data[i]);
    }
    bubble(data,LEN);
    for(i=0; i<= LEN-1; i++)     
        {
            printf("%8d", data[i]);
        }
}
void bubble(int dat[], int length)
{
        int head, tail,round,i;
        int t;
        head = 0;
        tail = length - 1;
 
    for (round = head; round <= tail; round++)
        for (i = tail; i >= round+1; i--)
            if ( dat[i] < dat[i-1] )
            {
                t=dat[i];             
                dat[i]=dat[i-1];
                dat[i-1]=t;
            }
}

4.实验题

编写程序统计从键盘输入的一行文本中各个字母的个数。
输入以回车键结束。
不区分大小写,大写字母与相应的小写字母按照同一个字母看待。
要求输出按照各个字母出现的个数从大到小进行排序,出现的个数相同的,按照字母在字母表中的先后顺序进行排序。
***输入格式:调用getchar()函数依次输入每个字符
***输出格式:"%c(%c):%d\n"
例如输入以下文本:
Hello World
程序将输出:
L(l):3
O(o):2
D(d):1
E(e):1
H(h):1
R(r):1
W(w):1
A(a):0
B(b):0
C(c):0
F(f):0
G(g):0
I(i):0
J(j):0
K(k):0
M(m):0
N(n):0
P(p):0
Q(q):0
S(s):0
T(t):0
U(u):0
V(v):0
X(x):0
Y(y):0
Z(z):0

#include <stdio.h>
#include<stdlib.h>
void bubble_sort(int a[], int c[],int n);
int main(void)
{int i;
int c[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
 char j;
 char a;                                       
 int b[26]={0};                      
 int count[26]={0};
 while ((a = getchar()) != '\n')     
   {
   for (j='A';j<='Z';++j)               
       {
           if (a == j || a == j+32)
           {
               b[j-'A']++;count[j-'A']++;
           }
   
       }
   }
   bubble_sort(b,c,26);
   for (i=0;i<26;++i)
   {
        printf("%c(%c):%d\n",c[i]-32,c[i],b[i]);
           
   }
}
    
void bubble_sort(int a[],int c[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(a[i] < a[i + 1])
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                temp = c[i];
                c[i] = c[i + 1];
                c[i + 1] = temp;
            }
        }
}

这道题是我做过后觉得比较难的,可能算法并不是很简便,如果各位大神有什么好的方法请一定告诉我。

Avatar_small
AP SSC fa 2 Question 说:
2022年9月09日 20:20

Formative Assessment means not only an examination, it includes various aspects such as Examination in completed lessons, Reflections, Project work done on the allotted topic and Self also Prepared notes etc. AP SSC fa 2 Question Paper Candidates of Telugu Medium, English Medium & Urdu Medium of the AP State can download the AP 10th Class FA 4 Model Paper 2023 Pdf with answers for the regular exams conducted by the Directorate of Government Examinations, Andhra Pradesh.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter