最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

初学c语言,给定两个整形变量的值,将两个值的内容进行交换。不允许创建临时变量,交换两个数的内容。求10 个整数中最大值。将三个数按从大到小输出。求两个数的最大公约数。

互联网 admin 5浏览 0评论

初学c语言,给定两个整形变量的值,将两个值的内容进行交换。不允许创建临时变量,交换两个数的内容。求10 个整数中最大值。将三个数按从大到小输出。求两个数的最大公约数。

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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b, temp = 0;scanf("%d %d", &a, &b);temp = a;a = b;b = temp;printf("%d\n%d\n", a, b);system("pause");return 0;
}


2. 不允许创建临时变量,交换两个数的内容(附加题)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b;scanf("%d %d", &a, &b);a = a + b;b = a - b;a = a - b;printf("%d\n%d\n", a, b);system("pause");return 0;
}


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

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


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

#define _CRT_SECURE_NO_WARNINGS
#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;}if (b < c){t = b; b = c; c = t;}if (a < b){t = a; a = b, b = t;}printf("从大到小:%d %d %d\n", a, b, c);system("pause");return 0;
}


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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);if (a < b){int temp = a;a = b;b = temp;}while ((a%b) != 0){int t = b;b = a % b;a = t;}printf("%d\n", b);system("pause");return 0;
}

 

初学c语言,给定两个整形变量的值,将两个值的内容进行交换。不允许创建临时变量,交换两个数的内容。求10 个整数中最大值。将三个数按从大到小输出。求两个数的最大公约数。

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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b, temp = 0;scanf("%d %d", &a, &b);temp = a;a = b;b = temp;printf("%d\n%d\n", a, b);system("pause");return 0;
}


2. 不允许创建临时变量,交换两个数的内容(附加题)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b;scanf("%d %d", &a, &b);a = a + b;b = a - b;a = a - b;printf("%d\n%d\n", a, b);system("pause");return 0;
}


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

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


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

#define _CRT_SECURE_NO_WARNINGS
#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;}if (b < c){t = b; b = c; c = t;}if (a < b){t = a; a = b, b = t;}printf("从大到小:%d %d %d\n", a, b, c);system("pause");return 0;
}


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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);if (a < b){int temp = a;a = b;b = temp;}while ((a%b) != 0){int t = b;b = a % b;a = t;}printf("%d\n", b);system("pause");return 0;
}

 

发布评论

评论列表 (0)

  1. 暂无评论