哈工大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;
}
  1. 純數字字符串檢驗
    題目內容

    按給定函數原型編程檢查一個字符串是否全由數字組成。
    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;
}
  1. 孿生素數
    題目內容

    相差爲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;
}
  1. 求解不等式
    題目內容

    對指定正實數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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章