输入三个整数x,y,z,把这三个数从小到大输出
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//输入三个整数x,y,z,请把这三个数由小到大输出
int main(){
int x, y, z;
int tmp;
printf("请输入三个整数: ");
scanf("%d %d %d", &x, &y, &z);
if (x > y){
tmp = x;
x = y;
y = tmp;
}
if (x > z){
tmp = x;
x = z;
z = tmp;
}
if (y > z){
tmp = y;
y = z;
z = tmp;
}
printf("这三个整数从小到大排列为:%d %d %d\n", x, y, z);
system("pause");
return 0;
}
将数的交换用函数来实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//输入三个整数x,y,z,请把这三个数由小到大输出
//把数字的交换通过函数来实现,简化代码
void Swap(int* x, int* y){
int tmp = *x;
*x = *y;
*y = tmp;
}
//除此之外,我们还可以通过按位异或的方式来实现两个数的交换
void Swap1(int* x, int* y){
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
//通过两个数的关系实现两个数的交换
void Swap2(int* x, int* y){
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}
int main(){
int x, y, z;
printf("请输入三个整数: ");
scanf("%d %d %d", &x, &y, &z);
if (x > y){
Swap(&x, &y);
}
if (x > z){
Swap(&x, &z);
}
if (y > z){
Swap(&y, &z);
}
printf("这三个整数从小到大排列为:%d %d %d\n", x, y, z);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//用*输出字母C的图案
int main(){
printf("*********\n");
printf("**\n");
printf("**\n");
printf("*********\n");
system("pause");
return 0;
}
输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(){
SetConsoleOutputCP(437); //显示大于127的ASCII码
char a = 219, b = 176;
printf("%c%c%c%c%c\n", a, b, a, b, a);
printf("%c%c%c%c%c\n", b, b, b, a, b);
printf("%c%c%c%c%c\n", a, b, b, b, a);
printf("%c%c%c%c%c\n", b, a, a, b, a);
printf("%c%c%c%c%c\n", a, a, a, a, b);
system("pause");
return 0;
}
打印9*9乘法口诀表
#include <stdio.h>
#include <stdlib.h>
//打印9*9乘法口诀表
int main(){
int i, j;
for (i = 1; i < 10; ++i){
for (j = 1; j <= i; ++j){
printf("%d*%d=%2d ", i, j, i*j);
}
printf("\n");
}
system("pause");
return 0;
}