1.有两个瓶子A和B,分别盛放者醋和酱油,要求将他们互换(即A中原来盛放醋,现该盛放酱油,B瓶则相反)
#include <stdio.h>
#include <string.h>
int main(){
#if(1)
//注意数组的长度定长一点,否则装不下数据
char A[10]="醋";
char B[10]="酱油";
char C[10];
printf("交换前\nA瓶装的是%s;\nB瓶装的是%s.\n\n",A,B);
//库函数char *strcpy(char *dest, const char *src) 把src 所指向的字符串复制到dest
strcpy(C, A);
strcpy(A, B);
strcpy(B, C);
printf("交换后\nA瓶装的是%s;\nB瓶装的是%s.\n",A,B);
#endif
#if(0)
//利用函数指针
void Swap();
char A[10]="醋";
char B[10]="酱油";
printf("交换前\nA瓶装的是%s;\nB瓶装的是%s.\n\n",A,B);
Swap(A,B);
printf("交换后\nA瓶装的是%s;\nB瓶装的是%s.\n",A,B);
#endif
}
void Swap(int *p1,int *p2)
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
2.依次将10个数输入,要求输出其中的最大数.
#include <math.h>
#include <stdio.h>
int main(){
#if(0)
int a,i,max;
printf("请输入第1个数:\t");
scanf("%d",&a);
printf("\n");
max = a;
for (i=2; i<=10; i++) {
printf("请输入第%d个数:\t",i);
scanf("%d",&a);
max = a>max?a:max;
printf("\n");
}
printf("输入的十个数中最大的是%d!\n",max);
#endif
//利用数组
#if(1)
int b[10];
int i,max;
for (i=0; i<10; i++) {
printf("请输入第%d个数:\t",i+1);
scanf("%d",&b[i]);
printf("\n");
}
max=b[0];
for (i=0; i<10; i++) {
max = b[i]>max?b[i]:max;
}
printf("输入的十个数中最大的是%d!\n",max);
#endif
}
3.有三个数a,b,c,要求按大小顺序将他们输出.
#include <stdio.h>
int main(){
double a,b,c,temp;
printf("请输入三个数:\t");
scanf("%lf %lf %lf",&a,&b,&c);
printf("\n");
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 = b;
}
printf("按大到小的顺序 %.2lf > %.2lf > %.2lf\n",a,b,c);
}
4.求1+2+3+~~+100.
#include <stdio.h>
int main(){
int i,sum=0;
//注意sum一定要有初始值;
for (i=1; i<=100; i++) {
sum+=i;
}
printf("1+2+3+~~+100=%d\n",sum);
}
5.判断一个数n能否同时被3和5整除.
#include <stdio.h>
#if(0)
int main(){
int a;
printf("请输入一个整数:\t");
scanf("%d",&a);
printf("\n");
if (a%3==0 && a%5==0) {
printf("%d能被3和5同时整除.\n",a);
}else {
}
}
#endif
//利用函数
#if(1)
int main(){
int pan();
int a, whether;
printf("请输入一个整数:\t");
scanf("%d",&a);
printf("\n");
whether=pan(a);
if (whether) {
printf("%d能被3和5同时整除.\n",a);
}else {
printf("%d不能被3和5同时整除.\n",a);
}
}
int pan(int x){
int flags;
if (x%3==0 && x%5==0) {
flags=1;
}else {
flags=0;
}
return flags;
}
#endif
6.将100~200之间的素数输出.
#include <stdio.h>
#include <math.h>
int main(){
int i,j,temp=0;
for (i=100; i<200; i++) {
//sqrt() 用来求给定值的平方根
for (j=2; j<sqrt(i); j++) {
if (i%j==0) {
break;
}
}
//i%j!+0,j>sqrt(i),结束循环
if (j>sqrt(i)) {
printf("%d\t",i);
temp+=1;
if (temp%5==0) {
printf("\n");
}
}
}
}
7.求两个数的最小公约数,和最大公倍数.
#include <stdio.h>
#if (0)
int main() {
//辗转相除法
int n, m;
int temp, num1, num2;
printf("请输入两个数:\t");
scanf("%d %d", &n, &m);
printf("\n");
num1 = n;
num2 = m;
if (num1 >= num2) {
while (m) {
temp = n % m;
n = m;
m = temp;
}
printf("最大公约数是:%d\n", n);
printf("最小公倍数为:%d\n", num1 * num2 / n);
} else {
while (n) {
temp = m % n;
m = n;
n = temp;
}
printf("最大公约数是:%d\n", m);
printf("最小公倍数为:%d\n", num1 * num2 / m);
}
}
#endif
#if (1)
//暴力穷举法
int main() {
int gcd();
int n, m;
int a;
printf("请输入俩个数:\t");
scanf("%d %d", &n, &m);
a = gcd(n, m);
printf("最大公约数是:%d\n", a);
printf("最小公倍数为:%d\n", n * m / a);
}
int gcd(int x, int y) {
int result;
for (result = x; result >= 0; result -= 1) {
//因为 两数的最大公约数必定小于或等于两数中较小的一个
//所以 无需判定x y大小关系
//只需要 拿两数之一做除数,
//若 除数不符合最大公约数
//则 每次循环体结束后除数减一,被除数不变
if (0 == x % result && 0 == y % result)
break;
}
return result;
}
#endif
8.求方程a*x*x+b*x+c=0的根x1和x2.
/*****************************************************
1.如果b*b-4*a*c>0:
x1=(-b)/(2*a)+sqrt(b*b-4*a*c)/(2*a)
x1=(-b)/(2*a)-sqrt(b*b-4*a*c)/(2*a)
2.如果b*b-4*a*c==0:
x1=(-b)/(2*a)
x1=(-b)/(2*a)
3.如果b*b-4*a*c<0:
"x1=(-b)/(2*a)+(sqrt(-(b*b-4*a*c))/(2*a))i"
"x1=(-b)/(2*a)-(sqrt(-(b*b-4*a*c))/(2*a))i"
*****************************************************/
#include <math.h>
#include <stdio.h>
#if (0)
int main() {
double a, b, c;
double x1, x2;
double d, f, p, q;
printf("判断方程 ax*x-bx-c=0 的根的情况:\n");
printf("\n");
printf("请输入a,b,c三个数的值:\t");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\n");
q = (-b) / (2 * a);
f = b * b - 4 * a * c;
p = sqrt(f) / (2 * a);
d = sqrt(-f) / (2 * a);
if (f >= 0) {
if (f == 0) {
x1 = q;
x2 = q;
printf("方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf,x2=%.2lf\n", a, b, c,
x1, x2);
} else {
x1 = q + p;
x2 = q - p;
printf("方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf,x2=%.2lf\n", a, b, c,
x1, x2);
}
} else {
printf(
"方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf+%.2lfi,x2=%.2lf-%.2lfi\n",
a, b, c, q, d, q, d);
}
}
#endif
#if (1)
void pa(); //当b*b-4*a*c>0
void pb(); //当b*b-4*a*c==0
void pc(); //当b*b-4*a*c<0
int main() {
double a, b, c, d;
printf("判断方程 ax*x-bx-c=0 的根的情况:\n");
printf("\n");
printf("请输入a,b,c三个数的值:\t");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\n");
d = b * b - 4 * a * c;
if (d > 0)
pa(a, b, c, d);
else if (d == 0)
pb(a, b, c);
else if (d < 0)
pc(a, b, c, d);
}
void pa(double a, double b, double c, double m) {
double q, p;
q = -b / (2 * a);
p = sqrt(m) / (2 * a);
printf("方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf,x2=%.2lf\n", a, b, c,
q + p, q - p);
}
void pb(double a, double b, double c) {
double q, p;
q = -b / (2 * a);
printf("方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf,x2=%.2lf\n", a, b, c, q,
q);
}
void pc(double a, double b, double c, double m) {
double q, p;
q = -b / (2 * a);
p = sqrt(-m) / (2 * a);
printf(
"方程 %.2lfx*x+%.2lfx+%.2lf=0 的根\t x1=%.2lf+%.2lfi,x2=%.2lf-%.2lfi\n",
a, b, c, q, p, q, p);
}
#endif
10,输出1900~2000年中的闰年.
/**************************************
输出1900~2000年中是闰年的年份
1.能被被4整除,但不能被100整除
2.能被100整除且能被400整除
**************************************/
#include<stdio.h>
int main(){
void ge();
int i,n=0;
printf("输出1900~2000年中的闰年:\n");
for (i=1900; i<= 2000; i++) {
if (i%4==0 && i%100!=0) {
printf("%d年\t",i);
n+=1;
ge(n);
}else if (i%100==0 && i%400==0) {
printf("%d年\t",i);
n+=1;
ge(n);
}
}
}
void ge(int n){
if (n%5==0) {
printf("\n");
}
}