哈工大C语言程序设计精髓第十四周

由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。

练兵区——编程题

2. 字符串中的字符排序


题目内容
编写一个函数,对一个字符串中的字符进行升序排序,并输出字符排序后的字符串,字符串长度小于20。
程序运行结果如下:
Input a string:
friend↙
definr
输入提示信息:“Input a string:\n”
输入格式:
字符串输入采用:gets()函数
输出格式:"%s"

代码实现

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100],temp;
    int i,j;
    printf("Input a string:\n");
    gets(str);
    for(i=0;i<strlen(str)-1;i++)
    {
        for(j=i+1;j<strlen(str);j++)
        {
            if(str[i]>str[j])
            {
                temp=str[i];
                str[i]=str[j];
                str[j]=temp;
            }
        }
    }
    printf("%s",str);
    return 0;
}

3.纯数字字符串检验


题目内容
按给定函数原型编程检查一个字符串是否全由数字组成。
int IsAllDigit(char p[]);/ 若全由数字组成,则函数返回1,否则返回0/
在主函数中,从键盘输入一个字符串(假设字符串的最大长度为20个字符),调用函数IsAllDigit(),检查该字符串是否全由数字组成,然后在主函数中根据函数IsAllDigit()的返回值输出相应的提示信息。
程序运行结果示例1:
Please input a string:
help456↙
The string is not digit string.
程序运行结果示例2:
Please input a string:
20150216↙
The string is digit string.
字符串输入提示信息:“Please input a string:\n”
输入格式: 字符串输入采用 gets()函数
输出格式
判断是纯数字字符串:“The string is digit string.”
判断不是纯数字字符串:“The string is not digit string.”

代码实现

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100];
    int i,t=1;
    printf("Please input a string:\n");
    gets(str);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>'9' || str[i]<'0')
        {
            printf("The string is not digit string.");
            t=0;
            break;
        }
    }
    if(t==1)
        printf("The string is digit string.");
    return 0;
}

4. 孪生素数


题目内容
相差为2的两个素数称为孪生素数。例如,3与5,41与43等都是孪生素数。设计程序求出指定区间上的所有孪生素数对。区间上限和下限由键盘获取。
程序运行示例如下:
please input c,d(c>2):
10,200↙
(11,13)
(17,19)
(29,31)
(41,43)
(59,61)
(71,73)
(101,103)
(107,109)
(137,139)
(149,151)
(179,181)
(191,193)
(197,199)
total=13
区间上限和下限的输入提示信息:“please input c,d(c>2):\n”
输入格式:
区间上限和下限的输入格式: “%ld,%ld”
输出格式
孪生素数的输出格式:"(%ld,%ld)\n"
所有孪生素数对的总数输出格式: “total=%d\n”

代码实现

#include <stdio.h>
#include <string.h>
long FF(long x);
int main()
{
    long c,d,i;
    int t=0;
    printf("please input c,d(c>2):\n");
    scanf("%ld,%ld",&c,&d);
    for(i=c;i<d;i++)
    {
        if(FF(i)==1 && FF(i+2)==1){
            printf("(%ld,%ld)\n",i,i+2);
            t++;}
    }
    printf("total=%d\n",t);
    return 0;
}
long FF(long x)
{
    long i;
    for(i=2;i<x;i++)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}

5. 求解不等式


题目内容
对指定正实数n(采用双精度浮点型表示),试求满足下面平方根不等式的最小整数m,并输出不等式左边的值。程序中浮点数的数据类型均为double。
程序运行示例如下:
Input n:
5.1↙
Result:m>=2
s=5.15
输入提示信息:“Input n:\n”
输入格式: “%lf”
输出格式
整数m的输出格式:“Result:m>=%d\n”
不等式左边的值的输出格式:“s=%.2f\n”

代码实现

#include<stdio.h>
#include<math.h>

int main()
{
    double n;
    int m=1;
    double i;
    double sum=0.0;
    printf("Input n:\n");
    scanf("%lf",&n);
    while(sum<=n)
    {
        sum=0.0;
        for(i=m;i<=2*m;i++)
        {
            sum += sqrt(i);
        }
        m++;
    }
    printf("Result:m>=%d\n",m-1);
    printf("s=%.2f\n",sum);
    return 0;
}
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值