zoukankan      html  css  js  c++  java
  • Work_2

    1. 给定两个整形变量的值,将两个值的内容进行交换。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(){
        int a, b, c;
        printf("请输入两个整数:
    ");
        scanf("%d %d", &a, &b);
        c = a;
        a = b;
        b = c;
        printf("交换后的结果为:
    ");
        printf("%d %d", a, b);
        printf("
    ");
        system("pause");
        return 0;
    }

    2. 不允许创建临时变量,交换两个数的内容。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(){
        int a;
        int b;
        printf("请输入两个整数:
    ");
        scanf("%d %d", &a, &b);
        a = a^b;        
        b = a^b;
        a = a^b;
        printf("交换后的结果为:
    ");
        printf("%d %d", a, b);
        printf("
    ");
        system("pause");
        return 0;
    }

    3.求10 个整数中最大值。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(){
        int arr[10];
        int i;
        printf("请输入十个整数:
    ");
        for (i = 0; i < 10; i ++){
            scanf("%d", &arr[i]);
        }
        int max = arr[0];
        for (i = 1; i < 10; i++){
            if (arr[i]>max){
                max = arr[i];
            }
        }
        printf("十个数的最大值为:
    ");
        printf("%d",max);
        printf("
    ");
        system("pause");
        return 0;
    }

    4.将三个数按从大到小输出。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(){    
            int a, b, c;
        int temp;
        printf("请输入三个整数:
    ");
        scanf("%d %d %d",&a, &b, &c);
        if (a < b){
            temp = a;
            a = b;
            b = temp;
        }
        if (a < c){
            a = a + c;
            c = a - c;
            a = a - c;
        }
        if (b < c){
            b = b ^ c;
            c = b ^ c;
            b = b ^ c;
        }
        printf("三个数从大到小排列为:%d %d %d",a, b, c);
        printf("
    ");
        system("pause");
        return 0;
    }        

    5.求两个数的最大公约数。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(){
        int i, j;
        int x = 2, n = 1;
        printf("请输入两个整数:
    ");
        scanf_s("%d %d", &i, &j);
        while (x <= ((i<j) ? i : j)){
            if (i%x == 0 && j%x == 0){ 
                n = x; 
            }
            x++;
        }
        printf("这两个数的最大公约数为:%d", n);
        printf("
    ");
        system("pause");
        return 0;
    }

     注释:

      

      VS中认为scanf()函数为一个不安全的函数,此时有两种解决方法:1.将scanf()函数替换为scanf_s()函数(scanf_s()函数为微软自定义创建的输入函数);2.创建一个宏来将此警告屏蔽,将报错语句中的use后的语句设置成一个宏,即 #define _CRT_SECURE_NO_WARNINGS 。

  • 相关阅读:
    Java Web 网络留言板2 JDBC数据源 (连接池技术)
    Java Web 网络留言板3 CommonsDbUtils
    Java Web ConnectionPool (连接池技术)
    Java Web 网络留言板
    Java Web JDBC数据源
    Java Web CommonsUtils (数据库连接方法)
    Servlet 起源
    Hibernate EntityManager
    Hibernate Annotation (Hibernate 注解)
    wpf控件设计时支持(1)
  • 原文地址:https://www.cnblogs.com/lkimprove/p/10053213.html
Copyright © 2011-2022 走看看