编程日常day1

  1. 给定两个整形变量的值,将两个值的内容进行交换。
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    int main(){
    int a = 1;
    int b = 2;
    int c = 0;
    c = a;
    a = b;
    b = c;
    printf(“a=%d b=%d”, a, b);
    system(“pause”);
    return 0;
    }

  2. 不允许创建临时变量,交换两个数的内容
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    int main(){
    int c = 1;
    int d = 2;
    c = c + d;
    d = c - d;
    c = c - d;
    printf(“c=%d d=%d”, c, d);
    system(“pause”);
    return 0;
    }

3.求10 个整数中最大值。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
int a[10];
int t = 0;
printf("请输入十个数字:");
for (int i = 0; i < 10; i++){
scanf_s("%d", &a[i]);
}
for (int j = 0; j < 10; j++){
	if(a[j] < a[j - 1])
	a[j] = a[j - 1];
}
printf("输出最大的数%d ", a[9]);
  
 system("pause");
return 0;

}

4.将三个数按从大到小输出。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){

int a = 7;
int b = 4;
int c = 9;
int t = 0;
if (a > b){
t = a;
a = b ;
b = t;
}
if (a>c){
t = a;
a = c;
c = t;
}
if (b>c){
t = b;
b= c;
c = t;
}
printf(“按从大到小顺序输出为:%d %d %d”, c,b,a);
system(“pause”);
return 0;
}

5.求两个数的最大公约数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
 int a, b,c;
printf("请输入俩个数:");
scanf("%d %d", &a, &b);
//辗转相除法
while(b!=0){
	int t = b;
	b = a%b;
	a = t;
}

printf("最大公约数为%d", a);
 system("pause");
return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_44778236/article/details/88684801