C language must memorize classic program code

1. The number of daffodils

Topic: Print out all the "Daffodil Numbers". The so-called "Daffodil Number" refers to a three-digit number whose cube sum is equal to the number
   itself. For example: 153 is a "daffodil number", because 153=1 cube +5 cube +3 cube.

方法一:

#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. Integral function summation of integer array.

Use function encapsulation to realize the summation of data in an integer array.

 #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

The Fibonacci sequence, also known as the golden section sequence, was introduced by the mathematician Leonardo Fibonacci using rabbit rabbit sequence " . Refers to such a sequence: 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. Yang Hui Triangle

The numbers at the beginning and end of each line of Yang Hui's triangle are 1. Moreover, each number is equal to the sum of the upper left and the upper two numbers .

#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. Monkeys eat peaches


A little monkey picked a lot of peaches in one day, and ate half of them on the first day, and then couldn't help eating one more. On the second day, he ate half of it again, and added another one; after that, he ate like this every day. On the 10th day, the little monkey found that there was only one peach.
Ask how many peaches the little monkey picked on the first day?
Use a recursive function to find out how many peaches the little monkey picked on the first day.

#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. Write a clock

#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. Count the number of occurrences of a character in a string


Write a function with two parameters, the first parameter is a character, the second parameter is a char *, the
function function is to return the number of the character in this string.

#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. Output in reverse order

一、逆序输出字符串

#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. Delete repeated characters in the string

#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. Use function encapsulation to realize string splicing

#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. Delete the spaces in the string

#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. Find the number of numeric characters in a string and convert the numeric characters into numbers and sum them.

 

#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. Find the maximum value and the number of rows and columns where the maximum value is located in the two-dimensional array.

#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. Bubble sort

#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. Realize the exchange of two numbers (function encapsulation)

#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. Simple supermarket checkout system

#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, perfect number

A number is said to be "perfect" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Programming
Find all the perfect numbers within 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. Simple Selection Sort

#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. Realize strncpy with function encapsulation idea

#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. Realize the function of 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. Realize the function of strcat with function encapsulation

方式一(最后添加'\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. Use sqrt to find the area of ​​a triangle

 #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. Convert the input integer to a string

#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. Simple implementation of mobile phone shopping mall function

#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");
}
}
}
                                                                                                                         
                                                                                                                         

Guess you like

Origin blog.csdn.net/qq_52049228/article/details/130159806
Recommended