头图

(Continuously updated, the latest time is August 25, 2022)

1. Sort the three numbers from small to large

Input any 3 integers, the programming implements sorting the 3 integers from small to large and displays the sorted results on the screen

 #include <stdio.h>
#include <stdlib.h>
int main()
{
    int a, b, c, t;
    printf("请任意输入三个数(空格分隔):\n");
    scanf("%d %d %d", &a, &b, &c);
    if (a < b) 
    { t = a;
     a = b;
     b = t;
    }    
    else if (a < c) 
    { t = a;
    a = c;
    c = t; 
    }    
    else if (b < c)
    { t = b; 
    b = c;
    c = t;
    }   
    printf("从大到小排列为:%d %d %d\n", a, b, c);
    system("pause");
    return 0;
}

2. a²+b²

Require input of integers a and b, if the result of a²+b² is greater than 100, then output the value of a²+b², otherwise output the result of a+b

 #include <stdio.h>
void main()
{
    int a,b;
    printf("请输入两个整数:\n");
    scanf("%d,%d,",&a,&b);
    if(a*a+b*b>100)
{
        printf("(a*a+b*b)/100=%d",(a*a+b*b)/100);
    }
    else
{
        printf("a+b=%d",a+b);
    }
    return 0;
}

3. Determining leap years

To determine whether any year is a leap year, one of the following conditions must be met:
The year is divisible by 4 and not divisible by 100;
The year is divisible by 400

 #include <stdio.h>
    int main()
    {
        int year,a;
        printf("请输人年份:\n");
        scanf("%d",&year);
        if(year%400==0)
            a=1;
        else
        {
            if(year%4==0&&year%100!=0)
                a=1;
            else
                a=0;
        }
        if(a==1)
        {
            printf("%d 此年是闰年\n",year);
        }
        else
        {
            printf("%d 此年非闰年\n",year);
        }
        return 0;
    }

4. Program to judge whether the input number is both 5 and an integer multiple of 7, if so, output yes, otherwise output no

 int main(){
int n;
 scanf("%d",&n);
 if(n%5==0&&n%7==0)
{
 printf("yes");
   }
 else{
 printf("no");
   }
 printf("\n");
 return 0;
}

5. Score ranking, the programming gives the corresponding grade to the input score, the score greater than or equal to 90 is "A", 80-89 is "B", 70-79 is "C", 60-69 is "D", 60 Divide the following as "E".

 #include<stdio.h>
int main()
{
    int a;
    printf("请输入成绩:");
        scanf("%d",&a);
    if(a>=90&&a<=100)
         printf("等级是A\n");
    else if(a>=80&&a<=89)
         printf("等级是B\n");
    else if(a>=70&&a<=79)
         printf("等级是C\n");
    else if(a>=60&&a<=69)
         printf("等级是D\n");
    else if(a>=0&&a<=59)
         printf("等级是E\n");
    else if(a<0)
         printf("输入错误,请输入1~100的成¦绩");
      getchar();
        return 0;
}

6. Print the multiplication table

 #include<stdio.h> 
 
int main(){  
    //外层循环变量,控制行  
    int i = 0;  
    //内层循环变量,控制列   
    int j = 0;   
    for(i=1;i<=9;i++){  
        for(j=1;j<=i;j++){  
            printf("%dx%d=%d\t",j,i,i*j);  
        }  
        //每行输出完后换行   
        printf("\n");     
    }  
}

7. Print the Triangle

 #include <stdio.h>
int main() {
  int i, j;
  int line;
  printf("请输入行数:");
  scanf("%d", &line);
  for (i = 0; i <= line; i++) {
    for (j = line - i + 1; j <= line; j++) {
      printf("* ");
    }
    printf("\n");
  }
  return 0;
}

8. Sequence summation

Use a while loop to do a simple calculation s=1 + 1/2+ 1/3+ ... + 1/n.

 #include <stdio.h>

int main()

{

    int i=1;

    double sum=0;

    while(i<=100){

        sum += 1.0/i;

        i++;

    } 

    printf("sum=%0.2lf",sum);
return;
}

(If it's not for the title limit while, a for loop can also be used here, which should be more intuitive)

9. Integer addition and subtraction exercises

The practitioner chooses whether to perform addition or subtraction, and then input the number of additions or subtractions to be performed. The specific value will be randomly generated by the computer. Enter the answer, and the computer will judge whether the result is correct according to the input data.

 #include<stdio.h>
#include<stdlib.h>        //srand()
#include<time.h>          //time()
int main()
{
    int sign,a,b,c;
    char sign1;
    int max;            //最大范围
    scanf("%d", &sign);
    scanf("%d", &max);
    srand((unsigned long)time(0));
    a = rand() % max;
    b = rand() % max;
    while (sign==1&&a<b)    //如果为减,a又比b小重新生成随机数
    {
        a = rand() % max;
        b = rand() % max;
    }
    sign1 = (sign == 1 ?'-':'+');
    printf("%d%c%d=", a, sign1, b);
    scanf("%d", &c);
    if ((sign == 1) && (a - b == c) || (sign != 1) && (a + b == c))
        printf("right");
    else
        printf("error");
    return 0;
}

10. Monkey eating peach problem

The monkey picked several peaches on the first day, ate half of the peaches immediately, and was not addicted to it, so he ate another one, and the next morning he ate half of the remaining peaches and ate another one. After that, I ate half and one of the leftovers from the previous day every morning. On the 10th morning, when I wanted to eat again, I saw that there was only one peach left. Find out how many you picked on the first day.

 include<stdio.h>
int main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
 {x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
 x2=x1;
 day--;
 }
printf("the total is %d\n",x1);
return 0;
}

11. Sort the array in reverse order

 #include<stdio.h>
int main()
{
    int a[10], i, temp;
    printf("请输入一串要逆序输出的数字:"); //输入一串数字
    for (i = 0; i < 10; i++)
        scanf("%d", &a[i]);
    putchar('\n');
    for (i = 0; i < 10 / 2; i++)        //将数字逆序输出
    {
        temp = a[i];
        a[i] = a[10 - i - 1];
        a[10 - i - 1] = temp;
    }
    printf("逆序输出后的结果为:");
    for (i = 0; i < 10; i++)
        printf("%3d", a[i]);

    return 0;
}

12. Concatenate two strings without strcat

Idea: first understand the characteristics of strcat, first look at a demo found on the Internet:

 #include <stdio.h>
#include <string.h>
 
int main ()
{
   char src[50], dest[50];
 
   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");
 
   strcat(dest, src);
 
   printf("最终的目标字符串: |%s|", dest);
   
   return(0);
}

Output result:
|This is destinationThis is source|

So it is clear that the two paragraphs are linked

 #include <stdio.h>
main()
{
    char a[100],b[100];
    int i=0,j=0;
    printf("Please input 2 strings:\n");
    gets(a);     //输入字符串啊,a,b
    gets(b);
    while(a[i]!='\0')
        i++;       //  i 表示字符串a的'\0'所在的位置
    while(b[j]!='\0')   //将b中字符一个个赋值给'\0'以及其后面的位置,i++,j++控制一一对应
        a[i++]=b[j++];
    a[i]='\0'     //给一个结束标志
    puts(a);    //输出
    return 0;
}

13. The issue of corporate profit distribution

Bonuses issued by enterprises are based on profit. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be raised by 10%; when the profit is higher than 100,000 yuan and less than 200,000 yuan, the part less than 100,000 yuan will be commissioned by 10%, and if it is higher than 100,000 yuan 7.5% of the cocoa commission; between 200,000 to 400,000 yuan, the part higher than 200,000 yuan can get a 5% commission; between 400,000 to 600,000 yuan and more than 400,000 yuan, 3% can be commissioned ; When between 600,000 and 1,000,000 yuan, the part higher than 600,000 yuan can be commissioned by 1.5%, and when it is higher than 1,000,000 yuan, the part exceeding 1,000,000 yuan will be commissioned at 1%, and the current month's profit I will be input from the keyboard. The total number of bonuses issued?

 #include <stdio.h>
int main()
{
    unsigned long int i = 0;
    int bonus = 0;
    printf("输入利润:");
    scanf("%ld", &i);
    if (i <= 100000)
        bonus = 100000 * 0.1;
    else if (100000 < i <= 200000)
        bonus = (i - 100000) * 0.075 + 100000 * 0.1;
    else if (200000 < i <= 400000)
        bonus = (i - 200000) * 0.005 + (i - 100000) * 0.075 + 100000 * 0.1;
    else if (400000 < i <= 600000)
        bonus = (i - 400000) * 0.003 + (i - 200000) * 0.005 + (i - 100000) * 0.075 + 100000 * 0.1;
    else if (600000 < i <= 1000000)
        bonus = (i - 600000) * 0.015 + (i - 400000) * 0.003 + (i - 200000) * 0.005 + (i - 100000) * 0.075 + 100000 * 0.1;
    else if(i > 1000000)
        bonus = (i - 600000) * 0.015 + (i - 400000) * 0.003 + (i - 200000) * 0.005 + (i - 100000) * 0.075 + 100000 * 0.1 + (i - 1000000) * 0.001;
    printf("bonus=%d", bonus);

    return 0;
}

14. According to the following functional relationship, input X, calculate Y, and require the output result to retain two decimal places

(1) When x is less than 5, y=0
(2) When x is greater than 5 and less than or equal to 10, y=sin(x)
(3) When x is greater than 10, y=cos(x)+sin(x)

 #include <stdio.h>
#include <math.h>
int main() {
    float X;
    double Y;
    scanf("%f",&X);
    if(X<5)
        Y=0;
    else if(X>5&&X<10)
        Y=sin(X);
    else if(X>10)
        Y=cos(X)+sin(X);
    printf("%.2f",Y);
    return 0;
}

15. Find the maximum of 10 numbers

This question is similar to the first question, but the difficulty is a little bigger than the first question. The specific idea is to build an array that stores 10 numbers, and then set a maximum value max. Then build two loops, the first loop to input data, the second loop compares the two numbers, the largest is taken out (if it is to compare the minimum value, the size sign is changed)

 #include <stdio.h>

 

int main() {

    int a[10];

    int Max = 0;

    for (int i = 0; i < 10; i++) {

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

    }

    for (int i = 0; i < 10; i++) {

      if(a[i] > Max) {

          Max = a[i];

      }

    }

    printf("十个数中最大的是:%d", Max);

    return 0;

}

16. Determine whether the positive integer is a palindrome

The difficulty of some questions has been reduced and changed to (input a 3-digit positive integer to judge the palindrome number). I personally think that if it is three digits, the method will be very simple. The idea is to enter three numbers, the first number and the third number. The numbers are compared, if they are not the same, output No, otherwise yes.
This question does not specify the number of input numbers. I saw a good method on the Internet.

Number method:

 #include<stdio.h>
int main()
{
    int x,newed,t,n;
    while(scanf("%d",&x)!=EOF)
    {
        newed=0;
        n=x;
        do
        {
            newed=newed*10+x%10;
            x/=10;
        }while(x>0);
        if(n==newed)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

String handling:

 #include<stdio.h>
#include<string.h>
int main()
{
    int i,length,flag=1;
    char a[100];
    gets(a);
    length=strlen(a);
    for(i=0;i<=length/2;i++) {
       if(a[i]!=a[length-i-1]) {
           flag=0;
           break;
       }
    }
    if(flag==1)
      printf("yes");
    else
      printf("no");
    return 0;
}

17. Case conversion

Test center: ASCII value of AZ: 65-90
The ASCII value of az is: 97-122
The difference between the two is 32

 #include<stdio.h>
#include<stdlib.h>
 
int main()
{
 char str[] = "AbCdEf";
 char c;
 int i = 0;
 while (str[i] != '\0')
 {
  c = str[i];
  if (c >= 'A' && c <= 'Z')
  {
   c = c + 32;
  }
  else if (c >= 'a' && c <= 'z')
  {
   c = c - 32;
  }
      printf("%c",c);
  i++;
 }
 printf("\n");
 system("pause");
 return 0;
}

18. Calculate the bounds of three-digit integers

 void main()
{
int x;
int max,min;
printf("请输入三位数 x:");
scanf("%d",&x);
x=x/100;
min=(int)x;
min=min*100;
max=min+99;

19. Triangle judgment

The coordinates of any three points on the given plane (x​1​​ ,y​1​​ ), (x​2​​ ,y​2​​ ), (x​3​​ ,y​3​​ ) to test whether they form a triangle.
图片.png

 #include "stdio.h"
#include "math.h"
int main()
{
    float x1, x2, x3, y1, y2, y3;
    float a, b, c, s, l, area;  //定义三边,半周长,周长,面积 
    scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);
    a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
    c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
    if (a + b > c && a + c > b && b + c > a) {
        s = (a + b + c) / 2;
        area = sqrt(s * (s - a) * (s - b) * (s - c));  //海伦公式求面积 
        l = a + b + c;
        printf("L = %.2f, A = %.2f\n", l, area);
    } else {
        printf("Impossible\n");
    }
    return 0;
}

20. Summation of special a string series

Given two positive integers a and n not exceeding 9, it is required to write a program to find the sum of a+aa+aaa++...+aa...a (n a's).

 #include "stdio.h"
#include "math.h"
int main()
{
    int a,n;
    int num=0;
    int sum = 0;
    scanf("%d %d",&a,&n);
    for (int i=1;i<=n;i++){   
        num=num+pow(10,i-1)*a;
        sum+=num;
    }
    printf("s = %d",sum);
    return 0;
    
}

21. Find the sum of odd numbers

 #include"stdio.h"
int main()
{
    int num,sum=0;
    for (int i=1; ;i++){
       scanf("%d",&num);
       if(num<=0){
            break;
       }
       if(num%2!=0){
           sum+=num;
       }    
    }
    printf("%d",sum);
    return 0;
}

22. Number of daffodils

Question: Print out all the "Daffodil Numbers". The so-called "Daffodil Numbers" refers to a three-digit number whose cubic sum of the digits is equal to the number itself. For example: 153 is a "daffodil number", because 153=1 cube + 5 cube + 3 cube.
Program analysis: Use the for loop to control 100-999 numbers, and each number is decomposed into units, tens, and hundreds.

 #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-i*100)/10;
  k=n%10;
  if(i*i*i+j*j*j+k*k*k==n)
    printf("%d\n",n);
  }
}

## 23. Time conversion This problem requires writing a program to output the time value of a given time after n seconds in the format of hh:mm:ss (the time will start from 0 when it exceeds 23:59:59)
(ps: the topic is relatively friendly)

 #include<stdio.h>
int main(void){
    int a,b,c;
    int k;
    
    scanf("%d:%d:%d\n",&a,&b,&c);
    scanf("%d",&k);
    c=k+c;
    if(c>=60){
        c=c-60;
        b=b+1;
        if(b>=60){
            b=b-60;
            a=a+1;
            if(a>=24)
                a=a-24;
        }
    } 
    printf("%02d:%02d:%02d",a,b,c);
    
    return 0;    
}

24. Read the result of this code

 程序源代码:
#include <stdio.h>
#define M 5
int main()
{
int a[M]={1,2,3,4,5};
int i,j,t;
i=0;j=M-1;  
while(i<j)
{t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;      //指针相互传参
i++;j--;
}
for(i=0;i<m;i++)
printf("%d",*(a+i));

return 0;
}

The input result is:
54321

25.Static defines the usage of static variables (just observe)

This topic focuses on reading and understanding. When defining variables, add the static keyword before the data type name, and the defined variables are static variables. Well, "static local variables" and "static global variables" can be included.

 #include <stdio.h>
void varfunc()
{
    int var = 0;
    static int static_var = 0;
    printf("\40:var equal %d \n", var);
    printf("\40:static var equal %d \n", static_var);
    printf("\n");
    var++;
    static_var++;
}
void main()
{
    int i;
    for (i = 0; i < 3; i++)
        varfunc();

    return;
}

Output result:
图片.png

26. Learn to use auto to define the usage of variables (just observe)

auto function:

 #include <stdio.h>
int main()
{
    int i, num;
    num = 2;
    for (i = 0; i < 3; i++)
    {
        printf("\40: The num equal %d \n", num);
        num++;
        {
            auto  num = 1;
            printf("\40: The internal block num equal %d \n", num);
            num++;
        }
    }

    return 0;

}

operation result:
图片.png

27. Ball falling problem

A ball falls freely from a height of 100 meters, and bounces back to half of its original height after each landing; again, how many meters does it travel when it hits the ground for the 10th time? How high is the 10th rally?

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

int main()
{
    float high = 100;  //总高度
    float s, h;    //h代表当前高度,s代表总长度

    h = high * pow(0.5, 10); //0.5是这个里面的规律,一半,10代表十次
    s = 2 * (50 - h * 0.5 * 2) / (1 - 0.5) + 100;
    printf("第十次的高度为%f;总路程为%f", h, s);

    return 0;
}

28. Find all perfect numbers within 1000

A number is called "complete" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Program to find all the numbers within 1000.

 #include<stdio.h>//头文件 
int main()//主函数 
{
  int number,s,i;//定义变量 
  for(number=2;number<1000;number++)//for循环 
  {
    //直接从2开始
    s=0;
    for(i=1;i<number;i++)
    {
      //检查i是否是m的因子 
      if((number%i)==0)
      { 
      //如果是的话 
        s=s+i;
      } 
      }
    if(s==number)
    {
      printf("%d的因子为:",number);
      for(i=1;i<number;i++)
      { 
        if(number%i==0)
        { 
          //判断是否是因子,是的话就输出 
          printf("%d ",i);
        }
      } 
        printf("\n");//换行 
      }
    }
  return 0;//主函数返回值为0 
}

29. Guess the numbers

 #include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{if(guess>i)
{printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
goto loop;
}

30. (first project) address book design

Frankly speaking, I didn't do it very well (because I am not familiar with pointers), here I quoted someone else's from the Internet and modified it myself. There are many pointer applications in it, thank you for the author's code.

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
typedef struct student     //定义结构体
{
    char name[20];    //姓名
    char num[15];      //学号
    char sex[10];     //性别
    char from[20];    //籍贯
    char political[10];    //政治面貌
    char phone[15];      //手机号
    char QQ[15];         //QQ号
    char dorm[10];     //宿舍
    struct student* next;  //结构体指针
}stu;
stu* head;

void print()    //主菜单
{
    system("cls");
    printf("\n\n\n");
    printf("      ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
    printf("      ***********************************************************\n");
    printf("      \t\t\t班级通讯录管理系统\n");
    printf("\n");
    printf("      \t\t1. 输入数据");
    printf("\t\t2. 显示数据\n");
    printf("      \t\t3. 插入数据");
    printf("\t\t4. 删除数据\n");
    printf("      \t\t5. 查看数据");
    printf("\t\t6. 修改数据\n");
    printf("      \t\t7. 保存数据");
    printf("\t\t8. 返回主菜单\n");
    printf("      ***********************************************************\n");
    printf("      ~~~~~退~~~~~~~~~~出~~~~~~~~~~请~~~~~~~~~~按~~~~~~~~~~9~~~~~\n");
    printf("      -----------------------------------------------------------\n\n");
}


void input(stu* p1)    //输入相关数据
{
    printf("姓名:");
    scanf("%s", &p1->name);
    printf("学号:");
    scanf("%s", &p1->num);
    printf("性别:");
    scanf("%s", &p1->sex);
    printf("籍贯:");
    scanf("%s", &p1->from);
    printf("政治面貌:");
    scanf("%s", &p1->political);
    printf("手机号:");
    scanf("%s", &p1->phone);
    printf("QQ号:");
    scanf("%s", &p1->QQ);
    printf("宿舍:");
    scanf("%s", &p1->dorm);
}


stu* inputdata()     //数据输入的函数
{
    stu* p1, * p2;
    int i = 1;
    p1 = (stu*)malloc(sizeof(stu));
    if (p1 != NULL)
    {
        head = p1;
        printf("\n\t\t\t☆☆☆输入数据☆☆☆\n");
        printf("------------------------------------------------------------------\n");
        printf("在姓名处输入“ok”代表输入数据结束\n");
        while (i)
        {
            printf("姓名:");
            scanf("%s", &p1->name);
            if (strcmp(p1->name, "ok") == 0)
            {
                printf("\n输入完毕!\n");
                printf("========================================================================\n");
                i = 0;
                p2->next = NULL;
                free(p1);
                p1 = p2;
            }
            else
            {
                printf("学号:");
                scanf("%s", &p1->num);
                printf("性别:");
                scanf("%s", &p1->sex);
                printf("籍贯:");
                scanf("%s", &p1->from);
                printf("政治面貌:");
                scanf("%s", &p1->political);
                printf("手机号:");
                scanf("%s", &p1->phone);
                printf("QQ号:");
                scanf("%s", &p1->QQ);
                printf("宿舍:");
                scanf("%s", &p1->dorm);
                printf("=====================================\n");
                p2 = p1;
                p1 = (stu*)malloc(sizeof(stu));
                if (p1 != NULL)
                {
                    p2->next = p1;
                }
            }
        }
        return(p1->next);
    }
}


stu* lookdata(stu* p1)     //查看数据的函数
{
    printf("\n\t\t\t☆☆☆显示数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    while (p1 != NULL)
    {
        printf("姓名:%s\n", p1->name);
        printf("学号:%s\t", p1->num);
        printf("性别:%s\t", p1->sex);
        printf("籍贯:%s\t", p1->from);
        printf("政治面貌:%s\t", p1->political);
        printf("手机号:%s\t", p1->phone);
        printf("QQ号:%s\t", p1->QQ);
        printf("宿舍:%s\n", p1->dorm);
        printf("======================================================================\n");
        p1 = p1->next;
    }
    return p1;
}


void insert()      //插入数据
{
    int i;
    char named[20];
    stu* p1, * p2, * p3;
    p1 = head;
    p3 = (stu*)malloc(sizeof(stu));
    p3->next = NULL;
    printf("\n\t\t\t☆☆☆插入数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("请输入插入者的资料:\n");
    input(p3);
    printf("\n插入选项\n");
    printf("1.首位置插入\t2.尾位置插入\t3.前插\n");
    printf("请输入你的选择:");
    scanf("%d", &i);
    switch (i)
    {
    case 1:p3->next = p1;
        head = p3;
        break;
    case 2:while (p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;
    }
          p1->next = p3;
          break;
    case 3:printf("请输入姓名(前插):");
        scanf("%s", named);
        while (strcmp(named, p1->name) != 0)
        {
            p2 = p1;
            p1 = p1->next;
        }
        p2->next = p3;
        p3->next = p1;
        break;
    }
    printf("插入成功!\n");
    printf("======================================================================\n");
    return;
}

void deleted()          //删除数据
{
    stu* p1, * p2;
    char Name[20];  //想要删除的人的姓名
    printf("\n\t\t\t☆☆☆删除数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("请输入要删除的姓名:");
    scanf("%s", Name);
    p1 = head;
    if (head == NULL)
    {
        printf("内存空空神马都没有!\n");
        printf("======================================================================\n");
        return;
    }
    if (strcmp(Name, p1->name) == 0)
    {
        head = p1->next;
        printf("删除成功!\n");
        printf("======================================================================\n");
        return;
    }
    while (p1 != NULL && (strcmp(Name, p1->name) != 0))
    {
        p2 = p1;
        p1 = p1->next;
    }
    if (p1 == NULL)
    {
        printf("此人不存在!\n");
        printf("======================================================================\n");
        return;
    }
    if (p1->next != NULL)
    {
        p1 = p1->next;
        p2->next = p1;
        printf("删除成功!\n");
        printf("======================================================================\n");
        return;
    }
    else
    {
        p2->next = NULL;
        printf("删除成功!\n");
        printf("======================================================================\n");
        return;
    }
}


void find(stu* p2)        //通过姓名查找查看数据的函数
{
    char name[20];
    int b = 0;
    printf("\n\t\t\t☆☆☆查看数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("请输入您想查找人的姓名:");
    scanf("%s", name);
    while (p2 != NULL)
    {
        if (strcmp(name, p2->name) == 0)
        {
            printf("你要找到的数据\n");
            printf("姓名:%s\n", p2->name);
            printf("学号:%s\t", p2->num);
            printf("性别:%s\t", p2->sex);
            printf("籍贯:%s\t", p2->from);
            printf("政治面貌:%s\t", p2->political);
            printf("手机号:%s\t", p2->phone);
            printf("QQ号:%s\t", p2->QQ);
            printf("宿舍:%s\n", p2->dorm);
            printf("======================================================================\n");
            b = 1;
        }
        p2 = p2->next;
    }
    if (b == 0)
    {
        printf("\n您要查找的人不存在!\n");
    }
}


void update(stu* p2)   //通过姓名查找修改数据
{
    char name[20];
    int b = 0, i;
    printf("\n\t\t\t☆☆☆修改数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("请输入将要修改人的姓名:");
    scanf("%s", name);
    while (p2 != NULL)
    {
        if (strcmp(name, p2->name) == 0)
        {
            printf("该同学的基本信息\n");
            printf("姓名:%s\n", p2->name);
            printf("学号:%s\t", p2->num);
            printf("性别:%s\t", p2->sex);
            printf("籍贯:%s\t", p2->from);
            printf("政治面貌:%s\t", p2->political);
            printf("手机号:%s\t", p2->phone);
            printf("QQ号:%s\t", p2->QQ);
            printf("宿舍:%s\n", p2->dorm);
            printf("\n请选择要修改的信息\n");
            printf("\t1.姓名\t2.学号\t3.性别\t4.籍贯\n\t5.政治面貌\t6.手机号\t7.QQ\t8.宿舍\n");
            printf("\n您的选择是(1~8):");
            scanf("%d", &i);
            printf("请输入修改之后的内容\n");
            switch (i)
            {
            case 1:printf("姓名:");
                scanf("%s", &p2->name);
                break;
            case 2:printf("学号:");
                scanf("%s", &p2->num);
                break;
            case 3:printf("性别:");
                scanf("%s", &p2->sex);
                break;
            case 4:printf("籍贯:");
                scanf("%s", &p2->from);
                break;
            case 5:printf("政治面貌:");
                scanf("%s", &p2->political);
                break;
            case 6:printf("手机号:");
                scanf("%s", &p2->phone);
                break;
            case 7:printf("QQ:");
                scanf("%s", &p2->QQ);
                break;
            case 8:printf("宿舍:");
                scanf("%d", &p2->dorm);
                break;
            }
            printf("\n修改成功!\n");
            printf("=========================================================================\n");
            b = 1;
        }
        p2 = p2->next;
    }
    if (b == 0)
    {
        printf("没有找到该人的资料!\n");
    }
}


void save(stu* p2)   //保存数据
{
    FILE* fp;
    char file[15];
    printf("\n\t\t\t☆☆☆保存数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("输入文件名:");
    scanf("%s", file);
    if ((fp = fopen(file, "w")) == NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    fprintf(fp, "姓名\t学号\t性别\t籍贯\t政治面貌\t手机号\tQQ号\t宿舍\n");
    while (p2 != NULL)
    {
        fprintf(fp, "%s\t", p2->name);
        fprintf(fp, "%s\t", p2->num);
        fprintf(fp, "%s\t", p2->sex);
        fprintf(fp, "%s\t", p2->from);
        fprintf(fp, "%s\t", p2->political);
        fprintf(fp, "%s\t", p2->phone);
        fprintf(fp, "%s\t", p2->QQ);
        fprintf(fp, "%s\n", p2->dorm);
        p2 = p2->next;
    }
    printf("\n保存成功!\n");
    printf("======================================================================\n");
    fclose(fp);
}


void screen()
{
    int i;
    char s[251] = { "欢迎使用由ZM制作班级通讯录管理系统,\n\n\t\t\t本系统用于通讯录管理----排序,打印\n\n\n\t\tWelcome to use produced by ZM class address book\n\n\t\t management system,sorting,printing" };

    printf("\n================================================================================\n");
    printf("\n\n\n\t\t\t");
    for (i = 0; s[i] != NULL; i++)
    {
        Sleep(30);
        printf("%c", s[i]);
    }
    printf("\n\n\n\n\n\n\n\n\t\t ~ Hi~ o(* ̄▽ ̄*)ブ~ ~ ~祝您旅途愉快~ ~\n");
    printf("================================================================================\n");

}


void main()
{
    int i;
    system("color 4e");
    screen();
    Sleep(3000);
    print();
    while (1)
    {
        printf("请输入你的选择(1~9):");
    loop:scanf("%d", &i);
        if (i < 1 || i>9)
        {
            printf("输入有误,请在1~9中进行选择:");
            goto loop;
        }
        switch (i)
        {
        case 1:
            inputdata();
            break;
        case 2:
            lookdata(head);
            break;
        case 3:
            insert();
            break;
        case 4:
            deleted();
            break;
        case 5:
            find(head);
            break;
        case 6:
            update(head);
            break;
        case 7:
            save(head);
            break;
        case 8:
            print();
            break;
        case 9:
            exit(1);
            break;
        }
    }
}

31. Find the sum of two arrays

It is known that an array a[5]={1,2,3,4,5}, b[5]={11,4,2,7,9} The array c[5] is equal to the sum of the corresponding elements of the arrays a and b and. Output the value of the elements in the array c.

 #include<stdio.h>
`
int main()`
{
    int a[5] = { 1,2,3,4,5 };
    int b[5] = { 11,4,2,7,9 };
    int c[5];
    int i;
    for (i = 0; i < 5; i++)
    {
        c[i] = a[i] + b[i];

        printf("%d\n", c[i]);
    }

    return 0;
}

32. Custom Function Reverse Array

Write a function void change(int array[],int n), which can store the n elements in the array array in reverse order. That is, array[0] is interchanged with a[n-1], array[1] is interchanged with array[n-2]... .

 void change(int array[], int n)
{
    int i, temp;   //i在数组内,temp负责交换
    for (i = 0; i < n / 2; i++)
    {
        temp = array[i];
        array[i] = array[n - i - 1];
        array[n - i - 1] = temp;
    }
    for (i = 0; i < n; i++)
        printf("%4d", array[i]);
}

33. Find the product c of two matrices. Know the values ​​of the matrices a and b

 #include<bits/stdc++.h>
using namespace std;

int main(){
    int a,b,c;
    cout<<"请输入两矩阵的行列:"<<endl;
    cin>>a>>b>>c;                           //分别录入第一个矩阵的行,两矩阵共同行列,第二个矩阵的列
    int X[a][b],Y[b][c],Z[a][c];            //开辟三个二维数组存储矩阵,注意相乘结果的行列值
    cout<<"请输入第一个矩阵:"<<endl;
    for(int i=0;i<a;i++){                   //矩阵的行
        for(int j=0;j<b;j++){               //矩阵的列
            cin>>X[i][j];
        }
    }
    cout<<"请输入第二个矩阵:"<<endl;
    for(int i=0;i<b;i++){                   //矩阵的行
        for(int j=0;j<c;j++){               //矩阵的列
            cin>>Y[i][j];
        }
    }
    memset(Z,0,sizeof(Z));          //将二维数组Z初始化为0
    //int temp=0;
    cout<<"矩阵相乘的结果为:"<<endl;
    for(int i=0;i<a;i++){
        for(int j=0;j<c;j++){
            for(int k=0;k<b;k++){
                Z[i][j]=Z[i][j]+X[i][k]*Y[k][j];      //行与列的乘积和为相应结果值
                //temp=temp+X[i][k]*Y[k][j];
            }
            cout<<Z[i][j]<<" ";                  //计算完一个后输出      
            //cout<<temp<<" ";
            //temp=0;
        }
        cout<<endl;                   //计算完一列后输出换行

    }
    return 0;
}

34. Apply the array to input the year, month, and date, and calculate the day of the year that the date is.

Requirement: Define a two-dimensional array to find the total number of days

 #include<stdio.h>
int day_of_year(int year, int month, int day);  //自定义函数
int main()
{
    int year, month, day;
    scanf("%d %d %d", &year, &month, &day);
    printf("%d", (day_of_year(year, month, day)));
    return 0;
}
int day_of_year(int year, int month, int day)

    int i, leap;   //i指输入月份-1,其他月的天数遍历,leap根据闰年平年切换一维二维
    int tab[2][13] = {
        {0,31,28,31,30,31,30,31,31,30,31,30,31},//非 
        {0,31,29,31,30,31,30,31,31,30,31,30,31}//闰年 
    };
    leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);//1为闰年 0为非 
    for (i = 1; i < month; i++) {
        day += tab[leap][i];
    }


    return day;
}

35. Output student number and average score in descending order of average grade.

Tip: Sort the average score. When the elements are exchanged, the elements of the student number are exchanged correspondingly.

Idea: Bubble sort, a sample of the bubble sort algorithm is as follows:

 #include <stdio.h>
int main()
{
    int i,j,t,a[11];    //定义变量及数组为基本整型
    printf("请输入10个数:\n");
    for(i=1;i<11;i++)
        scanf("%d",&a[i]);    //从键盘中输入10个数
    for(i=1;i<10;i++)    //变量i代表比较的趟数
        for(j=1;j<11-i;j++)    //变最j代表每趟两两比较的次数
            if(a[j]>a[j+1])
            {
                t=a[j];    //产利用中间变童实现两值互换
                a[j]=a[j+1];
                a[j+1]=t;
            }
            printf("排序后的顺序是:\n");
            for(i=1;i<=10;i++)
                printf("%5d",a[i]);    //将胃泡排序后的顺序输出
        printf("\n");
    return 0;
}

This code and gif animation are quoted from:

https://www.runoob.com/w3cnote/bubble-sort.html
http://c.biancheng.net/view/524.html

So the code for this question is:

 #include<stdio.h>
struct student
{
    int num;  //学号
    char name[20];   //姓名
    float score;     //分数
};  //先构造结构体,把需要的东西放一起,方便同时排序调换
int main()
{

    struct student stu[5] = { { 17,"keen",97.5 }, { 18,"tom",59 }, { 19,"wangli",31 }, { 20,"lihua",54 }, { 21,"yuzhou",98 }
    };
    struct student t;
    int i, j, k;
    printf("成绩由大到小的顺序:\n");
    for (i = 0; i < 4; i++)   //代表的比较趟数
    {
        k = i;
        for (j = i + 1; j < 5; j++)
        {
            if (stu[j].score > stu[k].score)
            {
                k = j;
            }
            t = stu[k];
            stu[k] = stu[i];
            stu[i] = t;
        }
        for (i = 0; i < 5; i++)//循环输出5个人的成绩 
        {
            printf("%d,%10s,%6.2f分\n", stu[i].num, stu[i].name, stu[i].score);//输出结果 
        }
        return 0;//主函数返回值为0 

    }
}

36. Write a function to realize the exchange of characters in str. For example, replace "abcde" with "edcba".

Several ways to reverse the order of arrays

1. Non-recursive approach

Reverse the existing string

 #include <stdio.h>
int main()
{
    char arr[] = "abcdef";
    int sz = sizeof(arr) / sizeof(arr[0]); //求的是数组包含的元素个数,'\0'也包括在内
    int left = 0;
    int right = sz - 2;   //减2是因为求得的sz包含了'\0'这个元素。
    while (left < right)
    {
        char tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left++;
        right--;
    }
    printf("%s", arr);
    return 0;
}

input in reverse order

 #include <stdio.h>
#include <string.h>
int main()
{
    char arr[101] = { 0 };     //要给字符数组一定的内存大小,如果写成char arr[] = { 0 };,当在给数组输入的时候就会造成越界访问。
    scanf("%s", arr);
    int sz = strlen(arr);    //在给定字符数组的大小为101的情况下,只能用strlen求输入字符串长度。
                           //用sizeof(arr)/sizeof(arr[0])求出来的是数组大小,为101。
    int left = 0;
    int right = sz - 1;
    while (left < right)
    {
        char tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left++;
        right--;
    }
    printf("%s", arr);
    return 0;
}

encapsulated as function

 #include <stdio.h>
#include <string.h>
void reverse(char arr[])
{
    
    int left = 0;
    int right = strlen(arr) - 1;      //封装成函数只能用库函数求字符串长度,不能用sizeof(arr)/sizeof(arr[0])-1这种方式。
                                    //因为数组形参就是个地址。sizeof(arr)与sizeof(arr[0])的大小都是四个字节或者八个字节。
    while (left < right)
    {
        char tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left++;
        right--;
    }
}
int main()
{
    char arr[] = "abcdef";
    reverse(arr);
    printf("%s", arr);
    return 0;
}

2. Recursive approach

 #include <stdio.h>
#include <string.h>
void reverse(char str[], int left, int right)
{
    if (left < right)
    {
        char tmp = str[left];
        str[left] = str[right];
        str[right] = tmp;
        reverse(str, left + 1, right - 1);
    }

}
int main()
{
    char arr[101] = { 0 };
    scanf("%s", arr);
    int left = 0;
    int right = strlen(arr) - 1;
    reverse(arr, left, right);
    printf("%s\n", arr);
    return 0;
}

37. Find the specified character from the string and delete it

 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
int main()
{
    char str[100];
    char* p = str;
    int z = 0;
    int t = 0;
    char c;
    printf("输入字符串:");
    gets(str);
    printf("输入删除的字符:");
    scanf("%c", &c);
    t = strlen(str);
    for (z = 0; z <= t; z++)
    {
        *p = str[z];
        if (*p != c)
            p++;
    }
    printf("%s", str);
    return 0;
}

38. Write a function int isprime(int x) that returns 1 if x is prime, and 0 otherwise.

Here is an excerpt from an online summary of the laws of prime numbers:
Think in terms of the definition of prime numbers. A prime number is a natural number greater than 1 that is not a factor of it except 1 and itself.
Then we can use a loop to traverse from 2 to this number minus 1. If this number is not divisible, then this number is a prime number.
That is:
Given a number n, i starts from 2 and takes values ​​until n - 1 (integer), if n % i != 0 , n is a prime number. Further thinking, is it necessary to traverse to n - 1?
The smallest factor of any composite number except 1 is 2, and the largest factor is n/2
Then it is enough for us to traverse to n/2

 nt isPrime(int target) {

    int i = 0;

    if (target <= 1) {
        printf("illegal input!\n");//素数定义
        return -1;
    }

    for (i = 2; i <= target / 2; i++) {
        if (target % i == 0)
            return 0;//不是素数直接返回0
    }

    return 1;//是素数返回1
}

39. Write the function fib(int n) to find the value of the nth item of the Fibonacci sequence with a recursive algorithm, and use the main function to output its first 20 items to verify the function

Regarding the Fibonacci sequence, as programmers, we only need to know the formula (or find a suitable formula on the Internet, just calculate it)
F(0)=0, F(1)=1, F(n)= F(n - 1)+ F(n - 2) ( n ≥ 2, n ∈ N*)

 #include <stdio.h>
int fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fib(n - 1) + fib(n - 2);   //公式输进去
}
}
int main(int argc, char* argv[])
{
for (int i = 1; i <= 20; i++)  //第一个到第20个
{
printf("%d\n", fib(i));
}

return 0;
}

Output on vc6.0:
图片.png

40. Define 3 integer variables and pointer variables pointing to integer variables, and use 3 pointer variables to complete data input, sorting and output from small to large.

 #include<stdio.h>
#include<stdlib.h>

int main()

{
    
    int a, b, c;
    int* p1 = &a,* p2 = &b, * p3 = &c;
    
    int temp;
    
    printf("请输入三个整型变量:\n");
    
    scanf("%d%d%d", p1, p2, p3);
    
    if (*p1 > *p2) 
    {
        
        temp = *p1;
        
        *p1 = *p2;
        
        *p2 = temp;
        
    }
    
    if (*p1 > *p3)
    {
        
        temp = *p1;
        
        *p1 = *p3;
        
        *p3 = temp;
        
    }
    
    if (*p2 > *p3) 
    {
        
        temp = *p2;
        
        *p2 = *p3;
        
        *p3 = temp;
        
    }
    
    printf("%d %d %d\n", *p1, *p2, *p3);
    
    return 0;
    
}

Output result:
图片.png

41. Given an integer array a[10], it is required to define two pointer variables max and min that point to integer variables, so that they respectively point to the maximum and minimum numbers of the array.

 //设定一个数组
//设定两个指针
//将两个指针定义初始化
//循环
//比较大小
//输出
#include<stdio.h>
int main()
{
    int a[5] = { 11,15,99,24,35 }, i;
    int* max, * min;
    max = min = &a[0];
    for (i = 1; i < 5; i++)
    {
        if (*max < a[i])
            max = &a[i];
        if (*min > a[i])
            min = &a[i];
    }
    printf("max=%d,min=%d\n", *max, *min);
}

42. Implement string search, delete, and replace

 //实现字符串的查找、删除、替换
# include<stdio.h>
# include<string.h>
 
//字符串的查找
int find(char *str1,char *str2)//在str1中寻找str2
{
    int len1=strlen(str1);
    int len2=strlen(str2);
    int i=0,index;
    if(len1<len2)
        return -1;//查找失败
    else
    {
        index=0;
        for(i=0;i<len1;i++)
        {
          if(str1[i]==str2[index])
          {
              index++;
              if(index==len2)
                  return i-len2+1;//返回第一次碰到str2的索引
          }
          else
          {
              index=0;
          }
        }
        
        return -1;
    }
}
//字符串的删除
char* delet(char* str,int start,int step)//在str1中删除从start到start+step的子串
{
    int len=strlen(str);
    int i=0;
    if(len<start+step)
        return NULL;//char *返回值NULL
    else
    {
       for(i=start;i<len-step;i++)
       {
           str[i]=str[i+step];
       }
       str[len-step]='\0';//末尾补0,补全新的字符串
       return str;
    }
}
 
//字符串的替换
char *replace(char *str1,int start,char *str2)
{
   int len1=strlen(str1);
   int len2=strlen(str2);
   int i=0,index=0;
   if(len1<start+len2)
       return NULL;
   else
   {
      for(i=start;i<start+len2;i++)
      {
         str1[i]=str2[index++];
      }
 
      str1[len1]='\0';
      return str1;
   }
 
}
int main()
{
    char str1[40];
    char str2[40];
    //删除重复子串
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        int t=find(str1,str2);
    //    printf("%d\n",t);
        while(t!=-1)
        {
            //删除
            char *p=delet(str1,t,strlen(str2));
            t=find(p,str2);
        }
        printf("%s\n",str1);
    }
 
    return 0;
}

43. Please write a function fun. The function of this function is to move the content of the string. The rules of movement are as follows: move the 1st to nth characters to the end of the string, and move the n+1th to the last character move to the front of the string

Thinking... If you have ideas, you can talk about it in the comment area, thank you

give it a blank

44. Xiaoming has 5 new books, and he wants to lend them to 3 children A, B, and C. If each person can only borrow 1 book at a time, how many different ways can they borrow them?

I owe the question just now, I didn't think it out, relax.... This question is very simple, if you are a master, you can skip it

 #include<stdio.h>
int main()
{
    int a, b, c, x = 0;
    for (a = 1; a <= 5; a++)
        for (b = 1; b <= 5; b++)
            for (c = 1; c <= 5; c++)
                if (a != b && a != c && b != c)
                    x++;
    printf("有%d种不同的借法\n", x);
}

45. A city's sports lottery uses integers 1, 2, 3, ..., 36 to represent 36 kinds of sports, and one lottery ticket can choose 7 kinds of sports. Write a program to select the number of a lottery ticket so that the sum of the 7 numbers of this lottery ticket is 105 and the difference between two adjacent numbers is 1, 2, 3, 4, 5, 6 in order. For example, if the first number is 1, the subsequent numbers should be 2, 4, 7, 11, 16, 22.

Program analysis: If the first number in the 7 numbers is k0, the relationship between the subsequent numbers is ki-ki-1=i. where i=1,2,3,4,5,6.
(To be honest, I personally think that the meaning of this question is a bit unclear. How did the difference order of numbers 1, 2, 3 and 2, 4, 7 come from, I thought about it for a long time....)

 #include<stdio.h> 
int main() 
{
    int a[7];
    int i,sum;
    for(a[0]=1;a[0]<=15;a[0]++)
    {
        for(i=1;i<=6;i++)
            a[i]=a[i-1]+i;
        sum=0;
        for(i=0;i<=6;i++)
            sum+=a[i];
        if(sum==105)
        {
            for(i=0;i<=6;i++)
                printf("%d ",a[i]);
            printf("\n");
        }
    }
    return 0;
}

46. ​​Factor a positive integer into prime factors.

For example: 输入90,打印出90=2*3*3*5
Idea: Short Division
图片.png

 #include<stdio.h>
int main()
{
    int n, i;   
    printf("请输入整数:");
    scanf("%d", &n);   //输入这个数
    printf("%d=", n);   //输出左侧n的值
    for (i = 2; i <= n; i++)  //从2开始依次循环
    {
        while (n % i == 0)    
        {
            printf("%d", i);
            n /= i;
            if (n != 1) printf("*");
        }
    }

    printf("\n");
    return 0;
}

47. Find the palindromic prime numbers within 1-1000. A palindromic prime means that the number is both a palindrome and a prime.

 #include <stdio.h>
int main()
{
    int i, a, b, k;
    for (i = 1; i <= 1000; i++)
    {
        b = 0;
        if ((i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0) || i == 2 || i == 3 || i == 5 || i == 7)  //素数条件
        {
            k = i;
            while (1)
            {
                a = k % 10;
                b = 10 * b + a;
                if (k < 10)
                    break;
                k = k / 10;

            }
            if (b == i)
                printf("%d\n", i);

        }
    }
    return 0;
}

Output screenshot:
图片.png

46. ​​The Millionaire Question

A millionaire met a stranger, and the stranger approached him to talk about a money exchange plan, the plan was as follows: I would give you 100,000 yuan a day, and you only need to give me a penny on the first day; Give you 100,000 yuan, you give me 2 cents; on the third day I still give you 100,000 yuan, you give me 4 cents; ..., you give me twice the amount of the previous day until the full month (30 days). The millionaire was happy and accepted the contract. Please write a program to calculate: how much money strangers gave to millionaires and how much money millionaires gave to strangers during the month

 #include<stdio.h>
void main()
{
    double i, sum = 0, x = 0.01;
    for (i = 1; i <= 30; i++)
    {

        x *= 2;
        sum += x;
    }
    printf("这一个月中陌生人给了百万富翁%d元\n", 30 * 100000);
    printf("这一个月中百万富翁给了陌生人%f元\n", sum);



    return;
}

vs running result:
图片.png

47. If the sum of all the factors of an integer A (including 1, excluding A itself) is equal to B; and the sum of all the factors of an integer B (including 1, excluding B itself) is equal to A, then the integers A and B are called integers A and B. Intimacy number. Find all intimacy numbers up to 3000

Program analysis: find the sum of the factors of A to be B, and then find the sum of the factors of B to be C. Determine if A is equal to C.

 #include <stdio.h>

int main()
{
    int a, b, i, j, c;
    for(a=1;a<3000;a++)
    {
        b = 0;
        for (i = 1; i <= a / 2; i++)
            if (a % i == 0)
                b += i;
        c = 0;
        for (j = 1; j <= b / 2; j++)
            if (b % j == 0)
                c += j;//    c=b的因子和
        if (a == c && a < b)//    排除重复
            printf("%d和%d为亲密数\n", a, b);
    }       
    return 0;
}

48. One count three squares

Find the number that meets the following conditions in the range of [100000,999999] and output:

  1. The number itself is a square
  2. The first 3 digits of the number are also a square number
  3. The last 3 digits of the number are also a square number
 #include <stdio.h>
#include <math.h>

int main()
{
    int i, num0, num1, num2;
    for (i = 100000; i < 999999; i++)  //范围确定
    {
        num0 = (int)sqrt(i);       //平方根,int强制整型
        num1 = (int)sqrt(i / 1000);
        num2 = (int)sqrt(i % 1000);
        if (num0 * num0 == i && num1 * num1 == i / 1000 && num2 * num2 == i % 1000)  //如果这些条件都满足
            printf("%d\n", i);
    }
    return 0;
}

49. Simplified Insertion Sort

图片.png

 #include<stdio.h>
main()
{
    int N, X, i, t, k, j;
    scanf("%d", &N);
    
        for (i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d", &X);
    N += 1;
    a[N - 1] = X;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N - i - 1; j++)
            if (a[j] > a[j + 1])
            {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
    }
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);
    return 0;
}

50. Find the maximum value and its subscript

 #include <stdio.h>
#define N 10
int main()
{
    int i, n, max;
    int index = 0;
    int arr[N];
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    max = arr[0];
    for (i = 1; i < n; i++)
    {
        if (max < arr[i])
        {
            max = arr[i];
            index = i;
        }
    }
    printf("%d %d\n", arr[index], index);
    return 0;
}

51. Store the numbers in the array in reverse order

Set an array of 10 values

 #include<stdio.h>
#define N 10
int main()
{
    int i, n;
    int temp;
    int arr[N];
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (i = 0; i < n / 2; i++)
    {
        temp = arr[i];
        arr[i] = arr[n - i - 1];
        arr[n - i - 1] = temp;

    }
    for (i = 0; i < n; i++)
    {
        printf(" %d", arr[i]); //因为行末尾不能有空格
    }
    return 0;
}

52. Find elements that are not common to two arrays

Input sample:

 10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1
 #include <stdio.h>
int main()
{
    int n1, n2;
    int a[20], b[20], i, j;  //分别遍历a,b两个数组

    scanf("%d", &n1);

    for (i = 0; i < n1; i++)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d", &n2);
    for (j = 0; j < n2; j++)
    {
        scanf("%d", &a[j]);
    }

    //两个数组带入输入
    int c[40] = { 0 };
    int flag = 1;
    int mark;
    int k;


    for (i = 0; i < n1; i++)
    {
        flag = 1;
        for (j = 0; j < n2; j++)
        {
            if (a[i] = b[j])
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
        {
            c[mark] == b[j];
            mark++;
        }
    }
    for (i = 0; i < mark; i++)
    {
        for (j = i + 1; j < mark; j++)
        {
            if (c[j] == c[i])
            {
                for (k = j; k < mark - 1; k++)
                {
                    c[k] = c[k + 1];
                }
                mark--;
                j--;
            }
        }
    }

    //完成筛查c[]重复的数

    printf("%d", c[0]);
    for (i = 1; i < mark; i++) {
        printf(" %d", c[i]);
    }
    return 0;
}

53. Caesar Cipher

In order to prevent the information from being easily stolen by others, it is necessary to convert the plaintext of the code into ciphertext through encryption. Enter a string (less than 80 characters) terminated by a carriage return, and then enter an integer offset, which is encrypted with a Caesar cipher and output. The Caesar cipher is a simple replacement encryption technology. All letters in the plaintext are offset by offset bits on the alphabet and then replaced with ciphertext. When the offset is greater than zero, it means a backward offset; when the offset is less than zero , indicating a forward offset.

It is also called cyclic shift cipher. Its encryption method is to replace each letter in the plaintext with the kth letter after this character in the alphabet. Its encryption process can be expressed as the following function:
E(m)=(m+k) mod n
Among them: m is the number of positions of plaintext letters in the alphabet; n is the number of letters in the alphabet; k is the key; E(m) is the number of corresponding positions of the ciphertext letters in the alphabet.
For example, for the plaintext letter H, the number of positions in the alphabet is 8, and if k=4, the ciphertext calculated according to the above formula is L:
E(8) = (m+k) mod n = (8+4) mod 26 = 12 = L

 #include <stdio.h>
#include <string.h>

int main()
{
    char password[100];
    int i, move, tmp;
    printf("加密选择1,解密选择2");
    scanf("%d", &tmp);
    switch (tmp)
    {
    case(1):               //加密
        printf("输入原文:");
        scanf("%s", &password);
        printf("自定义密钥");
        scanf("%d", &move);
        for (i = 0; i < strlen(password); i++)
        {
            if (password[i] >= 'A' && password[i] <= 'Z')
            {
                password[i] = ((password[i] - 'A') + move) % 26 + 'A';
            }
            else if (password[i] >= 'a' && password[i] <= 'z')
            {
                password[i] = ((password[i] - 'a') + move) % 26 + 'a';
            }
        }
        printf("加密后的密文");
        printf("%s\n", password);
        break;
    case(2):                            //解密
    {
        printf("输入密文:");
        scanf("%s", &password);
        printf("密匙为(1-25):");
        scanf("%d", &move);
        for (i = 0; i < strlen(password); i++)
        {
            if (password[i] >= 'A' && password[i] <= 'Z')
            {
                password[i] = ((password[i] - 'A') + 26 - move) % 26 + 'A';
            }
            else if (password[i] >= 'a' && password[i] <= 'z')
            {
                password[i] = ((password[i] - 'a') + 26 - move) % 26 + 'a';
            }
        }
        printf("解密后的原文");
        printf("%s

瞿小凯
1.3k 声望593 粉丝