基础编程02
2018-03-23 09:40
30 查看
1. 给定两个整形变量的值,将两个值的内容进行交换。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
int temp;
temp = i;
i = j;
j = temp;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
2. 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i + j;
j = i - j;
i = i - j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i ^ j;
j = i ^ j;
i = i ^ j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[10] = { 40, 35, 2, 34, 54, 67, 23, 42, 46, 78 };
int i,temp;
int max = arr[0];
for (i = 0; i < 10; i++)
{
if (arr[i] >= max)
{
temp = arr[i];
arr[i] = max;
max = temp;
}
}
printf("max=%d\n", max);
system("pause");
return 0;
}
4.将三个数按从大到小输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input three numbers:");
scanf("%d,%d,%d", &a, &b, &c);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if (a < c)
{
temp = a;
a = c;
c = temp;
}
if (b < c)
{
temp = b;
b = c;
c = temp;
}
printf("%d,%d,%d\n", a, b, c);
system("pause");
return 0;
}
5.求两个数的最大公约数。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input two numbers:");
scanf("%d,%d", &a, &b);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
while(b!=0)
{
c = a%b;
a = b;
b = c;
}
printf("a,b的最大公约数为%d\n", a);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
int temp;
temp = i;
i = j;
j = temp;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
2. 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i + j;
j = i - j;
i = i - j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i ^ j;
j = i ^ j;
i = i ^ j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[10] = { 40, 35, 2, 34, 54, 67, 23, 42, 46, 78 };
int i,temp;
int max = arr[0];
for (i = 0; i < 10; i++)
{
if (arr[i] >= max)
{
temp = arr[i];
arr[i] = max;
max = temp;
}
}
printf("max=%d\n", max);
system("pause");
return 0;
}
4.将三个数按从大到小输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input three numbers:");
scanf("%d,%d,%d", &a, &b, &c);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if (a < c)
{
temp = a;
a = c;
c = temp;
}
if (b < c)
{
temp = b;
b = c;
c = temp;
}
printf("%d,%d,%d\n", a, b, c);
system("pause");
return 0;
}
5.求两个数的最大公约数。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input two numbers:");
scanf("%d,%d", &a, &b);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
while(b!=0)
{
c = a%b;
a = b;
b = c;
}
printf("a,b的最大公约数为%d\n", a);
system("pause");
return 0;
}
相关文章推荐
- 超基础的网络编程02:基于TCP的Socket通信
- Docker之基础篇
- Java基础之—反射(非常重要)
- Linux 基础教程 33-硬盘分区及挂载
- c++基础(一)
- homerHEVC代码阅读(7)——基础结构之hvenc_engine_t
- 使用Java进行图像处理的一些基础操作
- [数据挖掘]数学基础---距离度量方式(马氏距离,欧式距离,曼哈顿距离)
- 网络基础与网络分层
- java中的几种基础排序,查找方法
- jdk8相关基础(1-1. 注解之NPE)
- HTML5基础知识2
- c++模板与泛型编程基础
- JavaScript基础-JSON
- opengl基础之--齐次坐标
- 2014-10-21《网页基础(2)》
- Struts2的配置文件基础(好文分享)
- java 从零开始,学习笔记之基础入门<IO流>(十八)
- [妙味JS基础]第十课:日期对象、时钟倒计时
- 黑马程序员 Day1 Java基础常识总结