#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//1.给定两个整形变量的值,将两个值的内容进行交换。
int main(){
int a = 0;
int b = 0;
int c = 0;
printf("请输入两个数,并用空格隔开。\na=\tb=\n");
scanf("%d %d", &a, &b);
c = a;
a = b;
b = c;
printf("交换后两数为:a=%d,b=%d\n", a, b);
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//2.不允许创建临时变量,交换两个数的内容。
int main(){
int a = 0;
int b = 0;
printf("请输入两个数,并用空格隔开\na=\tb=\n");
scanf("%d %d", &a, &b);
a = a + b; // a = a * b;
b = a - b; // b = a / b;
a = a - b; // a = a / b; 加法和乘法都可以
printf("交换后的两数为:\na=%d\nb=%d\n", a, b);
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//3.求十个整数中的最大值。
int main(){
int arr[] = { 1, 23, 3, 47, 5, 6, 7, 8, 2, 55 };
int max = 0;
for (int i = 0; i < 9; i++){
if (arr[i] >= arr[i + 1]){
arr[i + 1] = arr[i];
}
else{
max = arr[i + 1];
}
}
printf("这十个数中最大的数为:%d\n", max);
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//4.将三个数按从小到大输出。
int main(){
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数,并用空格隔开。\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b){
int t = b;
b = a;
a = t;
if (c > b){
printf("从小到大为:%d %d %d\n", a, b, c);
}
else if (c < b&&a < c){
printf("从小到大为:%d %d %d\n", a, c, b);
}
else if (c < a){
printf("从小到大为:%d %d %d\n", c, a, b);
}
}
else{
if (c > b){
printf("从小到大为:%d %d %d\n", a, b, c);
}
else if (c < b&&a < c){
printf("从小到大为:%d %d %d\n", a, c, b);
}
else if (c < a){
printf("从小到大为:%d %d %d\n", c, a, b);
}
}
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//5.求两个数的最大公约数。
int main(){
int a = 0;
int b = 0;
printf("请输入两个正整数,并用空格隔开。\n");
scanf("%d %d", &a, &b);
if (a > b){
if (a%b == 0){
printf("两数的最大公约数为:%d\n", b);
}
else{
for (int i = b; i >= 1; i--) {
if (b%i == 0 && a%i == 0){
printf("两数的最大公约数为:%d\n ", i);
}
}
}
}
else{
if (b%a == 0){
printf("两数的最大公约数为:%d\n ", a);
}
else{
for (int i = a; i >= 1; i--) {
if (b%i == 0 && a%i == 0){
printf("两数的最大公约数为:%d\n ", i);
}
}
}
}
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
//6.将数组a中的内容与b中的内容交换(数组一样大)。
int main(){
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 5, 4, 3, 2, 1 };
int c[5];
for (int i = 0; i < 5; i++){
c[i] = a[i];
a[i] = b[i];
b[i] = c[i];
}
printf("数组a为:%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]);
printf("数组b为:%d %d %d %d %d\n", b[0], b[1], b[2], b[3], b[4]);
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
//7.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
int main(){
double sum = 0;
double reduce = 0;
double result = 0;
for (int i = 1; i <= 100; i += 2){
sum += 1 / i;
}
for (int j = 2; j <= 100; j += 2){
reduce -= 1 / j;
}
result = sum + reduce;
printf("1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值为:%f", result);
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
//8.编写程序数一下 1到 100 的所有整数中出现多少次数字9。
int main(){
int count = 0;
for (int i = 1; i <= 100; i++){
if ((i % 10 == 9) || (i / 10 == 9)){
count++;
}
}
printf("1-100中所有整数出现数字9的次数为:%d\n", count);
system("pause");
return 0;
}
C语言(不允许创建临时变量交换两个数的内容;求十个整数中的最大值;求两个数的最大公约数;1/1-1/2+1/3-1/4+1/5……+1/99-1/100的值;1-100中9出现次数)
猜你喜欢
转载自blog.csdn.net/wener521/article/details/88632292
今日推荐
周排行