C语言必背经典程序代码_c语言代码-程序员宅基地

技术标签: 算法  c语言  linux  数据结构  开发语言  

1、水仙花数

题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

方法一:

#include <stdio.h>

int main(int argc, const char *argv[])
{ 
    for(int i=1;i<10;i++){
        for (int j=0;j<10;j++){
            for (int k=0;k<10;k++){
                if(i*i*i+j*j*j+k*k*k==i*100+j*10+k)
            
                printf("%d\n",i*100+j*10+k);                    
                
    
            }

        }
}
    return 0;
} 

方法二:

#include <stdio.h>
int main()
{
int i,j,k,n;
printf("'water flower'number is:");
 for(n=100;n<1000;n++)
 {
  i=n/100;/*分解出百位*/
  j=n/10%10;/*分解出十位*/
  k=n%10;/*分解出个位*/
  if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
   {
   printf("%-5d",n);
   }
 }
printf("\n");
}

2、整型数组内函数求和。

使用函数封装,实现一个整型数组内数据的求和。

 #include <stdio.h>
 
 int array_sum(int *data,int n);
 
 int main(int argc, const char *argv[])
 {
 int a[]={1,2,3,4,5,6,7,8};
 int sum=0;
 sum=array_sum(a,sizeof(a)/sizeof(int));
 printf("sum=%d\n",sum);
 return 0;
 }                                        
 
 
 int array_sum(int *data,int n){
 int ret=0;
 int i;
 for(i=0;i<n;i++){
 
 ret+=data[i];
 
 }

3、斐波那契数列

斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)以子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……

#include <stdio.h>
                                          
int main(int argc, const char *argv[])
{
    int arr[15];
    arr[0]=1;
    arr[1]=1;
    for(int i=2;i<15;i++)
    {
arr[i]=arr[i-2]+arr[i-1];
    }
    for(int i=0;i<15;i++){
printf("%d、",arr[i]);
    }
    printf("\n");
    return 0;
}
                                          

4、杨辉三角

杨辉三角的每行行首与每行结尾的数都为1.而且,每个数等于其左上及其正上二数的和

#include <stdio.h>

int main(int argc, const char *argv[])
{
    int a[15][15]={
   {0}};
    int i,j;
    for(i=0;i<15;i++)
    {
        a[i][0]=1;
        for(j=1;j<=i;j++)
            a[i][j]=a[i-1][j-1]+a[i-1][j];

                                                        
    }
    for(i=0;i<15;i++){
        for(j=0;j<=i;j++)
            printf("%8d",a[i][j]);
            printf("\n");

    }

    return 0;
}

5、猴子吃桃子


一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住多吃了一个。第二天又吃了一半,再加上一个;后面每天都是这样吃.到第10天的时候,小猴子发现只有一个桃子了。
问小猴子第一天共摘了多少个桃子?
用递归函数求得小猴子第一天共摘了多少个桃子。

#include <stdio.h>

int tao(int n);
                                               
int main(int argc, const char *argv[])
{
printf("%d\n",tao(10));

    return 0;
}

int tao(int n){

    if(n==1){
return 1;
}


return (tao(n-1)+1)*2;


}

6、编写一个时钟

#include <unistd.h>
int main(int argc, const char *argv[])
{
int year,month,day, hour,min,sec;
scanf("%d %d %d %d %d %d",&year,&month,&day,&hour,&min,&sec);
while(1){
sleep(1);
if(sec<59){
    sec++;}
else if(min<59){
min++;
sec=00;}
else if(hour<23){
hour++;
min=0;
sec=0;
}else if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<31){
    day++;
hour=00;
min=00;
sec=00;

}
                                                                                                 

else if(month==02&&day<28){
    day++;
hour=00;
min=00;
sec=00;

}


else if((month==4||month==6||month==9||month==11)&&day<30){     
    day++;
hour=00;
min=00;
sec=00;

}
else if(month<12){
month++;
day=01;
hour=00;
min=00;
sec=00;
}else {
year++;
month=01;
day=01;
hour=00;
min=00;
sec=00;
}

printf("%02d:%02d:%02d:%02d:%02d:%02d\r",year,month,day,hour,min,sec);
fflush(stdout);


}
    return 0;
}

7、字符串中计算某字符出现的次数


写一个函数有两个参数,第一个参数是个字符,第二个参数是个char *,
函数功能为返回这个字符串中该字符的个数。

#include <stdio.h>

int func(char a,char *b);

int main(int argc, const char *argv[])
{
char c;
char s[30]={0};

printf("Please input char:\n");
scanf("%c",&c);
getchar();                                  
printf("Please input string:\n");
gets(s);
func(c,s);
    return 0;
}

int func(char a,char *b){
int i=0;
while(*b){
    if(a==*b){
i++;
}
*b++;
}
printf("%d\n",i);
}

8、逆序输出

一、逆序输出字符串

#include <stdio.h>

int main(int argc, const char *argv[])
{
int arr[10];
for(int i=0;i<10;i++){
    scanf("%d",&arr[i]);
}
printf("逆序输出为");
int n, i;
n=sizeof(arr)/sizeof(int);               
for(i=n-1;i>=0;i--)
    printf("%d\t",arr[i]);
putchar('\n');
    return 0;
}


二、逆序输出数组中的数据

 #include <stdio.h>
 
 int main(int argc, const char *argv[])
 {
     int a[]={1,2,3,4,5,6};
     int *p,*q,n,i,temp;
     n=sizeof(a)/sizeof(int);
     p=a;
     q=&a[n-1];
     while(p<q){
         temp=*p;
         *p=*q;
         *q=temp;
         p++;
         q--;
     }
 
     for (i=0;i<n;i++){
         printf("%d\n",a[i]);
     }
     return 0;
 }
                                         

9、字符串中删除重复字符

#include <stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
    char s[100];
    printf("input:");
    gets(s);
    int n=strlen(s);
    for(int i=0;i<n;i++){
        int k=i+1;
        for(int j=i+1;j<n;j++){
if (s[j]!=s[i])
        s[k++]=s[j];
        }                                
        s[k]='\0';
    }
    puts(s);
    return 0;
}
                                         

10、用函数封装实现字符串拼接

#include <stdio.h>

char *strcat(char *a,char *b);

int main(int argc, const char *argv[])
{
    char a[50]="hello";
    char b[]="word";
    puts(strcat(a,b));                      
    return 0;
}


char *strcat(char *a,char *b){
char *c=a;
while(*a){
a++;
}
while(*b){
*a=*b;
a++;
b++;
}
*a='\0';

return c;

}

11、删除字符串中的空格

#include <stdio.h>
#include <string.h>
void del_space(char *s1);

int main(int argc, const char *argv[])
{
    char s[]="a d  gg sd ";
    puts(s);
    del_space(s);
    puts(s);
    return 0;
}

void del_space(char *s1)
{
    char *s2;
    s2=s1;
    while(*s1){                               
        if(*s1==' ')
        {
            s1++;
        }else{
            *s2=*s1;
            s1++;
            s2++;
        }
    }
    *s2='\0';

}
                                              
                                              

12、求字符串中数字字符个数及把数字字符转换成数字求和。

 

#include <stdio.h>

int main(int argc, const char *argv[]){
    char s[100];
    int i=0;
    int j=0;
    int sum=0;
    gets(s);
    //puts(s);
    while(s[i]!='\0'){
        if('0'<=s[i]&&s[i]<='9'){
            j++;
        sum+=(s[i]-'0');
        }                                                             
        i++;
    }
    printf("字符串中数字字符的个数为:%d\n",j);

    printf("字符串中数字字符求和为:%d\n",sum);
    return 0;
}

13、二维数组中求出最大值及最大值所在的行数和列数。

#include <stdio.h>

int main(int argc, const char *argv[])
{
int a[3][3]={
   {1,2,3},{4,5,6},{7,8,9}};
int i,j,row,cloumn;
row=cloumn=0;
for(i=0;i<3;i++){
for (j=0;j<3; j++){
    if(a[row][cloumn]<a[i][j]){
        row=i;
        cloumn=j;
}

}                                                                        
}

printf("max=%d 最大值所在行为%d行 %d列\n",a[row][cloumn],row,cloumn);

    return 0;
}

14、冒泡排序

#include <stdio.h>

int main(int argc, const char *argv[])
{
    int i,j,temp;
    int arr[5]={0};
    printf("input:");                            
    for(i=0;i<5;i++){
        scanf("%d",&arr[i]);
    }
    for(i=0;i<4;i++){
        for (j=0;j<4-i;j++){
            if(arr[j]>arr[j+1]){

                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;

            }

        }

    }
    printf("排序后为:");
    for(i=0;i<5;i++){
        printf("%d ",arr[i]);

    }
    putchar(10);
    return 0;
}
                                                 

15、实现两个数的交换(函数封装)

#include <stdio.h>

void  swap(int *x, int *y);

int main(int argc, const char *argv[])
{
    int a;                                  
    int b;
    scanf("%d",&a);
    scanf("%d",&b);
swap(&a,&b);
printf("%d\n",a);
printf("%d\n",b);
    return 0;
}

void  swap(int *x, int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;


}

16、简易超市结账系统

#include <stdio.h>

int main(int argc, const char *argv[])
{   int n;
    double m;
    printf("***********************************************\n");
    printf("欢迎你来给我送钱,请按以下步骤进行送钱程序\n");
    printf("1、是尊贵会员请输入1以及消费金额\n");
    printf("2、不是尊贵会员请输入2以及消费金额\n");
    printf("***********************************************\n");
    scanf("%d %lf",&n,&m);
    if(n=1){
if(m<100){
printf("money is %lf\n",m*0.99);
}else if(m<200){

printf("money is %lf\n",m*0.95);
}else if(m<300){


printf("money is %lf\n",m*0.92);


}else if(m<500){

printf("money is %lf\n",m*0.88);
}else{
                                                                                                   

printf("money is %lf\n",m*0.8);
}

    }else{


if(m<100){
printf("money is %lf\n",m);
}else if(m<200){

printf("money is %lf\n",m*0.98);
}else if(m<300){


printf("money is %lf\n",m*0.95);


}else if(m<500){

printf("money is %lf\n",m*0.9);
}else{


printf("money is %lf\n",m*0.88);
}


    }
    return 0;
}
                                                                                                   
                                                                                                   
                                                                                                   

17、完数

一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
找出1000以内的所有完数。

 

#include <stdio.h>

int main(int argc, const char *argv[])
{
    int i,j,sum;
for (i=3;i<=1000;i++){
    sum=1;
for (j=2;j<i;j++){
                                          
    if((i%j)==0)
        sum+=j;
}

if(sum==i)
printf("%d\n",i);
}
    return 0;
}
                                          
                                          

18、简单选择排序

#include <stdio.h>
#define N 5
int main(int argc, const char *argv[])
{
    int i,j,k,temp;
    int num[N];
    printf("input num:",N);

    for(i=0;i<N;i++){

        scanf("%d",&num[i]);

    }

    for (i=0;i<N-1;i++){
        k=i;
        for(j=i+1;j<N;j++){            
            if (num[j]<num[k]){
                k=j;
            }
        }
        if(k!=i){
            temp=num[i];
            num[i]=num[k];
            num[k]=temp;

        }
    }

    printf("output:\n");
    for(i=0;i<N;i++){

        printf("%d",num[i]);


    }
    putchar(10);
    return 0;
}
                                       

19、用函数封装思想实现strncpy

#include <stdio.h>

int main(int argc, const char *argv[])
{printf("please input two string and n:\n");
    char a[100];
    gets(a);
    char b[100];
    gets(b);
    int n;
    scanf("%d",&n);
char *p=a;
char *q=b;

int i=0;                                            
while(*q&&i<n){
*p=*q;
i++;
p++;
q++;
}
printf("strncpy:\n");
puts(a);

return 0;
}
                                                    

20、实现strcmp的功能

 #include <stdio.h>
 
 int main(int argc, const char *argv[])
 {
     printf("please input two string :\n");
     char a[100];
     gets(a);
     char b[100];
     gets(b);
     char *p=a;
     char *q=b;
 
     while (*p&&*q){
         if(*p<*q){
             printf("-1\n");
             return 0;
         }else if(*p>*q){
 
             printf("1\n");
             return 0;
         }else{
             p++;
             q++;
             if((*p=='\0')&&(*q=='\0')){
                 printf("0\n");
                 return 0;
             }
         }
     }
     if(*p){
         printf("1");
     }else{
         printf("-1");
     }
 
     return 0;
 }                                                 
                                                   
                                                   

21、用函数封装实现strcat的功能

方式一(最后添加'\0'):

#include <stdio.h>
#include<string.h>
void mystrcat(char *x,char *y);
int main(int argc, const char *argv[])
{
char a[100];
char b[100];
gets(a);
gets(b);
mystrcat(a,b);
puts(a);
    return 0;
}

void mystrcat(char *x,char *y){
x=x+strlen(x);
while(*y){
*x=*y;
x++;
y++;

}
*x='\0';
}                                       


方式二(初始化数组'\0'):
#include <stdio.h>
#include<string.h>
void mystrcat(char *x,char *y);
int main(int argc, const char *argv[])
{
char a[100]={0};
char b[100]={0};
gets(a);
gets(b);
mystrcat(a,b);
puts(a);
    return 0;
}

void mystrcat(char *x,char *y){
x=x+strlen(x);
while(*y){
*x=*y;
x++;
y++;
  
}
//*x='\0';                                    
}
                                              
方式二(定义全局变量'\0'):  
                                           
#include <stdio.h>
#include<string.h>
void mystrcat(char *x,char *y);
char a[100];
char b[100];
int main(int argc, const char *argv[])
{
gets(a);
gets(b);
mystrcat(a,b);
puts(a);                                                   
    return 0;
}

void mystrcat(char *x,char *y){
x=x+strlen(x);
while(*y){
*x=*y;
x++;
y++;

}
//*x='\0';
}

22、用sqrt 实现求三角形面积

 #include <stdio.h>                   //编译时加-lm
 #include <math.h>
 int main(int argc, const char *argv[])
 {
     double area;
     double a,b,c,s,n;
     printf("请输入三角形的三边长\n");
     scanf("%lf",&a);
     scanf("%lf",&b);
     scanf("%lf",&c);                           
     if((a+b)>c&&(a+c)>b&&(b+c)>a){
         s=1.0/2*(a+b+c);
         area=sqrt(s*(s-a)*b*c);
         printf("%lf",area);
         putchar('\n');
     } else{
         printf("erro");
         putchar('\n');
     }
     return 0;
 }

23、输入整数转换为字符串

#include <stdio.h>
char *zhuan(char *p,int n);

int main(int argc, const char *argv[])
{char s[50],*r;
    int n;
    printf("请输入要转换为字符串的整数\n");
    scanf("%d",&n);
    r=zhuan(s,n);
    puts(r);
    puts(s);
    return 0;
}

char *zhuan(char *p,int n){

    int r,i=0,j;
                                                  
    while(n){

        r=n%10;
        n=n/10;
        p[i]=r+'0';
        i++;
    }

    p[i]='\0';
    j=i-1;
    i=0;
    while(i<j){
        r=p[i];
        p[i]=p[j];
        p[j]=r;
        i++;
        j--;
    }
    return p;

}

24、简易实现手机商城功能

#include <stdio.h>
typedef struct
{
    int ID;
    char Brand[10];
    char Model[20];
    char CPU[20];
    float Price;
}PH;

void ui();
void input(PH ph[],int a);
void output(PH ph[],int a);
void selec(PH *p);
int num;

int main(int argc, const char *argv[])
{

while(1){
 sleep(1);
    ui();
    int s;
    PH ph[10];                                                                                                           
    PH *p=ph;
    printf("please selec:\n");
    scanf("%d",&s);
switch(s)
{
case 1: input(ph,s);break;
case 2: output(ph,s);break;
case 3: return 0;
case 4:selec(p);break;
default:
       puts("xia hu shu");

}

       while(getchar()!='\n');
}
return 0;
}



void input(PH ph[],int a){
    int n=0;
    printf("please input iphone num:\n");
    scanf("%d",&n);
    printf("ID\tBrand\tModel\tCPU\tPrice\n");
    for(int i=num;i<num+n;i++){

        scanf("%d %s %s %s %f",&ph[i].ID,ph[i].Brand,ph[i].Model,ph[i].CPU,&ph[i].Price);

    }
puts("input success");
num=num+n;
}



void output(PH ph[],int a){
    printf("ID\tBrand\tModel\tCPU\tPrice\n");
    for(int i=0;i<num;i++){

        printf("%d\t%s\t %s\t %s\t %f\n",ph[i].ID,ph[i].Brand,ph[i].Model,ph[i].CPU,ph[i].Price);

    }
}



void ui(){

puts("*************************************");
puts("****iphone management systerm********");
puts("*************1、input****************");
puts("*************2、output***************");
puts("*************3、exit*****************");
puts("*************4、selec*****************");
puts("*************************************");


}


void selec(PH *p){
float min,max;
printf("please input max:\n");
scanf("%f",&max);

printf("please input min:\n");
scanf("%f",&min);
puts("the iphone in this rage have:i\n");
for (int i=0;i<num;i++){
if((p+i)->Price>=min&&(p+i)->Price<=max){
    printf("%d\n",(p+i)->ID);
}else{
printf("sorry,no phone");
}
}
}
                                                                                                                         
                                                                                                                         

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_52049228/article/details/130159806

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签