C-入门

C语言入门习题,对变量的赋值,交换操作以及简单循环语句
#include <stdio.h> //临时变量法交换两个变量的值
int main()
{
int a = 4;
int b = 5;
int temp;
temp = a;
a = b;
b = temp;
printf(“a=%d\n”, a);
printf(“b=%d\n”, b);

system("pause");
return 0;

}

#include <stdio.h> //临时变量法交换两个变量的值
int main()
{
int a = 4;
int b = 5;
int temp;
temp = a;
a = b;
b = temp;
printf(“a=%d\n”, a);
printf(“b=%d\n”, b);

system("pause");
return 0;

}

#include <stdio.h> //二进制按位异或的方法
int main()
{
int a = 3; //二进制数为011
int b = 5; //二进制数为101
a = a^b; //a,b按位异或为 110; 相同为0,相异为1
b = a^b;
a = a^b;
printf(“a=%d\n”, a);
printf(“b=%d\n”, b);
system(“pause”);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> //把三个数按从大到小输出
int main()
{
int a;
int b;
int c;
int m; //定义四个变量
printf(“Please enter a,b,c:\n”);
scanf("%d,%d,%d", &a, &b, &c); //变量未被初始化,所以取其地址
if (a < b)
{
m = a;
a = b;
b = m;
}
if (a < c)
{
m = a;
a = c;
c = m;
}
if (b < c)
{
m = b;
b = c;
c = m;
}
printf(“The order of the number is:\n”);
printf("%d,%d,%d", a, b, c);
system(“pause”);
return 0;
}

#include <stdio.h> //求十个整数中最大者
int main()
{
int i;
int a[10] = { 1, 3, 5, 7, 9, 10, 19, -2, 7, 4 };
int max = a[0];
for (i = 0; i < 10; i++)
{
if (max < a[i + 1])
{
max = a[i + 1];
}
printf("%d\t", a[i]);
}
printf(“max=%d”, max);
system(“pause”);
return 0;

}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //求两个数的最大公约数
int main()
{
int a, b;
printf(“Please enter a,b:”);
scanf("%d %d", &a, &b);
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
printf(“The greatest common divisor is:%d\n”, a);
system(“pause”);
return 0;

}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //穷举法 求两个数的最大公约数
int main()
{
int a, b, c;
printf(“Please enter a,b:”);
scanf("%d %d", &a, &b);
c = (a > b) ? b: a;
while (a%c != 0 || b%c != 0)
{
c–;
}
printf(“The greatest common divisor is:%d\n”, c);
system(“pause”);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> //用辗转相除法求两个数的最大公约数
int main()
{
int a, b, c;
while (1)
{
printf(“Please enter a,b:”);
scanf("%d %d", &a, &b);
c = a%b;
while (c != 0)
{
a = b;
b = c;
c = a%b;
}
printf(“The greatest common divisor is:%d\n”, b);
}
system(“pause”);
return 0;
}

发布了39 篇原创文章 · 获赞 32 · 访问量 1518

猜你喜欢

转载自blog.csdn.net/weixin_44780625/article/details/88703117
C-