C语言示例

来源:互联网 发布:hdr 电视 推荐 知乎 编辑:程序博客网 时间:2024/05/17 19:58

c语言程序示例

(1)使用数组求连续出现的最长的相同的数字。

#include <stdio.h>int main(){    char str[100];    char c;    int i = 0;    int count = 1;    int maxcount = 0;    printf("please input character string:");    scanf("%s",str);    while(str[i] != '\0')    {        if((str[i] == str[i + 1]) || (str[0] = '\0'))        {            count++;        }        if(count > maxcount)        {            maxcount = count;            c=str[i];        }        if(str[i] != str[i + 1])        {            count = 1;        }            i++;    }    printf("%c连续出现了%d次\n",c,maxcount);}

运行结果
这里写图片描述
(2)冒泡排序法(循环语句的嵌套使用)

#include <stdio.h>int main(){     int a[6] ;    int i ;    int j ;    int p ;    int temp;    for( i = 0 ; i < 6 ; i++)    {        scanf("%d",&a[i] );    }    for( i = 0 ; i < 5 ; i++)    {           p=i;        for( j = i + 1 ; j < 6 ; j++)        {           if( a[j ] < a[p] )           {               p = j;           }        }      if( p!= i)      {             temp = a[p];          a[p] = a[i];          a[i] = temp;      }  }  for( i = 0 ; i < 6 ; i++)  {    printf("%3d\n", a[i]);  }}

运行结果:
这里写图片描述

0 0