目录
P6 1-3:等差级数
P19 7.编写一个C程序,计算上底为a, 下底为b, 高为h的梯形的面积。其中a, b和h的值由用户从键盘上输入。
P19 8.编写一个C程序,求出半径为r的圆的周长和面积。
P53 3-6.判断给定的某一年是否是闰年。
P68 9. 利用三角形的三条边求其面积的公式是 f = sqrt(s*(s-a)*(s-b)*(s-c),s = (a+b+c)/2; 计算并输出三角形的面积f和周长.
P68 9. 编写一个程序,求出给定半径r的圆以及内接正n边形的面积,并且输出计算结果。
P74 4-1. 用辗转相除法,求两个正整数的最大公约数.
P80 4-2. 用二分法求方程的解。
P82 4-3. 百鸡问题
用100元钱买100只鸡,已知每只公鸡5元,每只母鸡3元,3只小鸡1元,问能买公鸡,母鸡和小鸡各多少只。
P83 4-1. 求各位数字的立方和等于1099的三位数。
P84 4-5.用迭代法求方程f(x) = 的根.
P86 4-7. 求
P91 4.(1)输入a, b, c三个数,按降序打印出来。
P91 4.(2)求的根,应分别考虑如下情况:
P92 4.(3)计算1-3+5-7+......-99+101。
P92 4.(4)计算。
P92 4.(5)计算,n=20。
P92 4.(6)将1到100之间能用3或5整除的数打印出来。
P96 5-1 利用行列式方法求解二元一次方程组.
P96 5-1 利用参考级数可以求出PI的近似值。
P96 5-8 将100分成4个数之和,使得第一个数加上4,第二个数减去4,第三个数乘以4,第四个数除以4后所得的和,差,积,商都相等。问这4个数各是多少?
P96 5-14 换零钱问题。把1元对换成1分,2分, 5分的硬币,共有多少种换法。
P124 4 输入两个正整数m,n,求其最大公因子和最小公倍数。
P124 5 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。
P124 6 电文加密。为了保密,往往对电码文(原文)进行加密并形成密码文。简单的加密算法是:将字母A变成I,a变成i,即变成其后的第八个字母。这样,U变成C,V变成D,等等。从键盘输入一串电文,输出其相应的密码。
P124 7. 求整数1~20的阶乘之和(即求1!+2!+3!+.......+20!)。
P124 8.编写一个程序,计算并输出小于给定整数的所有素数。给定的整数由用户从键盘上输入。
P124 9. 求一个奇特的整数x, 它满足下面的条件:对于用户随便输入两个整数a和b, 有x+a和x+b分别是完全平方数。如果在给定范围内不能找到符合条件的x,就要求用户重新输入一组整数a和b。
P125 11. 某人摘了一筐桃子,第一天卖掉一半,他又吃了一只;第二天卖掉剩下的一半,又吃了一只,以后三天都这样做,到了第六天一看,筐中只有一只桃子了。问:他一共摘了多少个桃子?
P125 12. 如果一共三位数等于它自己的每一位字的立方和,则称此数为“水仙花数”,如 ,求100~999之间的全部水仙花数。
P125 12. 打印出杨辉三角.
P125 14.用迭代法求给定整数的平方根。求平方根的迭代公式为 。
P125 15. 用二分法求方程在(-10,10)之间的根。
P124 16. 打印10行*号。第一行打印1个,第二行打印2个,.........,第10行打印10个。形状如下所示:
P124 18. 一只球从100m高度自由落下,每次落地后反跳回到原高度的一半,再落下。求:它在第10次落地时共经过多少米?第10次反跳多高?
P124 6-2. 设有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始起,每年年初也生一头小母牛。问在第20年时,共有多少头牛?分别输出从第1年至第20年每年的牛数。
P124 6-3. 计算2002年的某月某日是当年的第多少天?星期几(已知2002年元旦是星期二).
P124 6-7.写一个程序,计算两个矩阵相乘 得到的第三个矩阵,并打印计算结果。
P144 6-9 输入一个月份的数字,输出对应的英文名字。
P147 6-12 输入一组杂乱无序的整数,按从小到大的次序进行排列,并输出排序后的结果。
P147 6-12 使用Eractos thenes筛选法求出1000以内的素数。并按照每行放10个素数的格式把他们打印出来。
P150 6-14魔方问题。
P152 2. 将1~9这9个数字分成3组,使每组中的三个数排成一个三位的完全平方数。要求每个数字必须用一次,而且只能使用一次。
列出了全排列 + 判断条件
P152 5.从0°到用户给定的角度,计算并输出每隔1°对应的正弦函数值。
P152 6. 找出一个二维数组的靶点, 即该位置上的元素在该行上最大,但在该列上却最小,也可能没有靶点。
P152 7.求解汉诺塔问题。
P152 9.摆花方阵。
P170 7-8.写一个计算调和级数部分和的程序。
P188 7-15.八皇后问题。
P191 7-16. 汉诺塔游戏
P193 1.求n~m间的所有素数。n 和 m的值由用户输入。为了使程序结构清晰,设置一个函数prime(x),用来判断x是否是素数。
P194 1.编写一个程序,计算 的值。
P194 4. 写一个计算组合 C(m, n) 的程序。
原式 = .=C(m-1, n-1) + C(m-1, n);
P194 6.将字符串转换为与其等价的双精度浮点数,并能处理以科学计数法表示的数据。
P196 14.设a, b, c, d是四个不同的正整数,前三个组成等比数列,后三个组成等差数列,且c+d=44,求4个数。
P205 8-2.用指针变量方程编写函数reverse(s),实现字符串s的字符全部首尾颠倒。
P256 7.用梯形法求定积分:
P256 9.简单约瑟环问题
P215 8-7.写一个读入一串正文行并打印其中最长行的程序。
P228 8-13 指向行数组的指针变量。
给定等差级数的首项,公差和项数,计算该级数的第n项的值以及前n项和。
第n项的值 : an = a1 + (n-1)*d;
前n项和 :Sn = a1 +(a1+d) + (a1+2d)......+(a1+(n-1)d)
= na1 + n(n-1)d/2;
#include
/*给定等差级数的首项,公差和项数,计算该级数的第n项的值以及前n项和
第n项的值: an = a1 + (n-1)*d;
前n项和 :Sn = a1 +(a1+d) + (a1+2d)......+(a1+(n-1)d)
= na1 + n(n-1)d/2;
*/
int main()
{
int a1, d, n, an, sn;
scanf("%d%d%d",&a1, &n, &d);
an = a1+(n-1)*d;
sn = n*a1 + n*(n-1)*d/2;
printf("an = %d\n",an);
printf("sn = %d\n",sn);
return 0;
}
/*
运行结果
5 10 6
an = 59
sn = 320
*/
#include
int main()
{
double a, b, h, s;
scanf("%lf%lf%lf",&a, &b, &h);
s = (a+b)*h/2.0;
printf("%.2f\n",s);
return 0;
}
/*
运行结果
1 3 4
8.00
*/
#include
#include
#define PI acos(-1.0)
int main()
{
double r, L, S;
r = 1;
L = 2*PI*r;//周长
S = PI*r*r;
printf("周长为%.2f\n", L);
printf("面积为%.2f\n", S);
return 0;
}
/*
运行结果
周长为6.28
面积为3.14
*/
#include
int main()
{
char x, y;
x = 'A'+10;
y = 'Z'-20;
printf("%c=%d,%c=%d",x,x,y,y) ;
return 0;
}
/*
运行结果
K=75,F=70
*/
闰年:能被4整除而不能被100整除,或者能被400整除.
#include
int main()
{
int year;
scanf("%d",&year);
if((year%4==0&&year%100!=0)||year%400==0)
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
return 0;
}
/*
运行结果
1900
1900 is not a leap year.
2008
2008 is a leap year.
*/
#include
#include
int main()
{
double a, b, c, s, f, L;
scanf("%lf%lf%lf",&a,&b,&c);
s = (a+b+c)/2;
L = a + b + c;
f = sqrt(s*(s-a)*(s-b)*(s-c));
printf("周长为%.2lf\n面积为%.2lf\n",L, f);
return 0;
}
/*
运行结果
3 4 5
周长为12.00
面积为6.00
*/
#include
#include
#define PI acos(-1.0)
int main()
{
double r, n, single, temp, s1,s2;
scanf("%lf%lf",&r,&n);
single = 360.0/n;
temp = sin(PI*(single/180.0));
s1 = PI * r * r;
s2 =n*(1.0/2.0)*r*r*temp;
printf("圆的面积为%.2lf\n",s1);
printf("内接正n边形的面积为%.2lf\n",s2);
return 0;
}
/*
运行结果
1 2
圆的面积为3.14
内接正n边形的面积为2.00
*/
#include
#include
int gcd(int a, int b)
{
if(b==0) return a;
return gcd(b, a%b);
}
int main()
{
int a, b, ans,t;
scanf("%d %d",&a,&b);
if(a
#include
#include
/*
假设给定一个定义在区间[a, b]区间的递增函数f(x),
求f(x)=0的根.
*/
const double eps = 1e-5;//精度为10^(-5)
double f(double x)//假设f(x)=x*x=2即f(x)=x*x-2
{
return x*x-2;
}
double solve(double L, double R)
{
double left = L, right = R, mid;
while(right-left>eps)
{
mid = (left+right)/2;
if(f(mid)>0)
right = mid;
else
left = mid;
}
return mid;
}
int main()
{
printf("%.5f\n",solve(1,2));
return 0;
}
/*
运行结果
1.41421
*/
#include
#include
/*
x公鸡 5元/只
y母鸡 3元/只
z小鸡 1/3元/只
*/
int main()
{
int x, y, z, sum = 100;
double msum =100;
for(x=0; x<=sum/5; x++)
for(y=0; y<=sum/3; y++)
for(z=0; z<=sum*3; z++)
{
if(x+y+z==100&&5.0*double(x)+3.0*double(y)+1.0*double(z)/3==100.0)
printf("%d %d %d\n",x, y,z);
}
return 0;
}
/*
运行结果
0 25 75
4 18 78
8 11 81
12 4 84
*/
#include
#include
int js(int n)
{
int sum=0;
while(n)
{
sum+=pow(n%10,3);
n/=10;
}
return sum;
}
int main()
{
for(int i=100; i<=999; i++)
{
if(js(i)==1099)
printf("%d ",i);
}
return 0;
}
/*
运行结果
379 397 739 793 937 973
*/
#include
#include
#define eps 1e-5
double f(double x)
{
return (log(1+sqrt(2+x*x))-sqrt(1+2*x*x)-3-log(x));
}
double f1(double x)// f1(x) = lnx;
{
return log(x);
}
double f2(double x)//f2
{
return (log(1+sqrt(2+x*x))-sqrt(1+2*x*x)-3);
}
int main()
{
double x0, c, x1;
scanf("%lf",&x0);
c = f2(x0);
x1 = exp(c);
while(fabs(x1-x0)>=eps)
{
x0 = x1;
c = f2(x0);
x1 = exp(c);
}
printf("%.5f\n",x1);
}
/*
2
0.04414
*/
#include
#include
#define eps 1e-6//10^(-6)
double jc(double n)
{
double ans = 1;
for(int i=1; i<=n; i++)
ans*=i;
return ans;
}
int main()
{
double sum = 1, t, x;
scanf("%lf",&x);
for(int i=1; ; i++)
{
t = pow(x,i)/jc(i);
if(t
#include
#include
#include
#include
using namespace std;
bool cmp(int a, int b)
{
return a>b;
}
int main()
{
int a[3];
scanf("%d%d%d",&a[0],&a[1],&a[2]);
sort(a,a+3,cmp);
printf("%d %d %d\n",a[0], a[1], a[2]);
return 0;
}
/*
1
2.718282
*/
#include
#include
#include
#include
using namespace std;
int main()
{
double a, b, c, dt,x1, x2;
scanf("%lf%lf%lf",&a,&b,&c);
dt = b*b-4*a*c;
if(dt<0)
printf("有两个共轭复根!\n");
else if(dt==0)
{
x1 = (-1.0*b-sqrt(dt))/2.0*a;
x2 = x1;
printf("有两个相等的实根:%.2f\n",x1);
}
else
{
x1 = (-1.0*b+sqrt(dt))/2.0*a;
x2 = (-1.0*b-sqrt(dt))/2.0*a;
printf("有两个不等的实根:%.2f %.2f\n",x1,x2);
}
return 0;
}
/*
1 4 4
有两个相等的实根:-2.00
1 -2 -3
有两个不等的实根:3.00 -1.00
*/
#include
#include
#include
#include
using namespace std;
int main()
{
int sum=0;
for(int i=1; 2*i-1<=101; i++)
{
if(i%2==1)
sum+=(2*i-1);
else
sum+=(2*i-1)*(-1);
}
printf("%d\n",sum);
return 0;
}
/*
51
*/
#include
#include
#include
#include
using namespace std;
int main()
{
double sum=0;
sum = (pow(3,20)-1)/2.0;
printf("%.0f\n",sum);
return 0;
}
/*
1743392200
*/
#include
#include
#include
#include
using namespace std;
int main()
{
int n;
double sum=0;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
sum+=(1.0/double(i))*double(i+1);
}
printf("%.5f\n",sum);
return 0;
}
/*
20
23.59774
*/
#include
#include
#include
#include
using namespace std;
int main()
{
for(int i=1; i<=100; i++)
{
if(i%5==0||i%3==0)
printf("%d ",i);
}
return 0;
}
/*
3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60
63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
*/
a1x + b1y = c1;
a2x + b2y = c2;
#include
#include
#include
#include
using namespace std;
int main()
{
double a1, a2, b1, b2, c1, c2;
double dt, x, y;
scanf("%lf%lf%lf%lf%lf%lf",&a1,&b1,&c1,&a2,&b2,&c2);
dt = a1*b2 - a2*b1;
if(dt==0.0)
printf("无解\n");
else
{
x = (c1*b2-c2*b1)/dt;
y = (a1*c2-a2*c1)/dt;
printf("x = %f\ny = %f\n",x,y);
}
return 0;
}
/*
3 8 29 -3 8 7
x = 3.666667
y = 2.250000
*/
#include
#include
#include
#include
using namespace std;
#define eps 1e-7
int main()
{
double sum =0,t;
for(int i=1; ; i++)
{
t = 1.0/(2*double(i)-1);
if(t
#include
#include
#include
#include
using namespace std;
#define eps 1e-7
int main()
{
int x1,x2,x3,x4;
for(int i=1;i<100; i++)
{
x1 = i-4;
x2 = i+4;
x3 = i/4;
x4 = i*4;
if(x1+x2+x3+x4==100)
{
printf("%d %d %d %d\n",x1, x2, x3,x4);
break;
}
}
return 0;
}
/*
12 20 4 64
*/
#include
#include
#include
#include
using namespace std;
#define eps 1e-7
int main()
{
int x, y,z,ans=0;
for(int x=0; x<=20; x++)//5分
for(int y=0; y<=(100-x*5)/2; y++)//2分
{
ans++;
}
printf("%d\n",ans);
return 0;
}
/*
541
*/
#include
int gcd(int a, int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
int lcm(int a, int b)
{
return a/gcd(a,b)*b;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
int temp;//保证m>n
if(m
#include
#include
int main()
{
char a[100];
while(gets(a)!=NULL)
{
int ans1=0,ans2=0,ans3=0,ans4=0;
for(int i=0; i='a'&&a[i]<='b'||a[i]>='A'&&a[i]<='Z')
ans1++;
else if(a[i]==' ')
ans2++;
else if(a[i]>='0'&&a[i]<='9')
ans3++;
else
ans4++;
}
printf("%d %d %d %d\n",ans1, ans2, ans3, ans4);
}
return 0;
}
/*
123sdsnfn andsandjkwKKJ
5 4 3 14
*/
#include
#include
int main()
{
char a[100];
gets(a);
for(int i=0;i='a'&&a[i]<='z')
printf("%c",(a[i]-'a'+8)%26+'a');
else
printf("%c",(a[i]-'A'+8)%26+'A');
}
return 0;
}
/*
ABCDEFGHIJKLMNOOPQRSTUVWSYZabcdefghijklmnopqrstuvwsyz
IJKLMNOPQRSTUVWWXYZABCDEAGHijklmnopqrstuvwxyzabcdeagh
*/
#include
#include
typedef long long int LL;
int main()
{
LL k=1,ans=0;
for(int i=1;i<=20; i++)
{
k=k*i;
ans+=k;
}
printf("%lld\n", ans);
return 0;
}
/*
2561327494111820313
*/
#include
#include
int ans[100],k=0;
int p[10010] = {false};
void isPrime()//筛法求素数
{
for(int i=2; i<10010; i++)
{
if(p[i]==false)
{ ans[k++] = i;
for(int j=i+i; j<10010; j+=i)
{
p[j] =true ;
}
}
}
}
int main()
{
isPrime();//千万别忘
int n;
scanf("%d",&n);
//printf("k = %d\n",k);
for(int i=0; ans[i]
#include
#include
int main()
{
int a,b,x,flag = true;
while(flag)
{
scanf("%d%d",&a,&b);
for(x=1; x<=1000; x++ )
{
if(a*a==x+a&&b*b==x+b)
{
printf("%d\n",x);
flag = false;
}
}
printf("请重新输入a,b\n");
}
return 0;
}
/*
*/
思路:倒退。
#include
#include
int main()
{
int n;
scanf("%d",&n);
int sum = 1;
n-=1;
while(n--)
{
sum = (sum+1)*2;
}
printf("%d\n",sum);
return 0;
}
/*
6
94
*/
#include
#include
#include
int check(int n)
{
int sum=0,temp = n;
while(n)
{
sum+=pow(n%10,3);
n/=10;
}
if(temp==sum)
return true;
return false;
}
int main()
{
for(int i=100; i<=999; i++)
{
if(check(i)==true)
printf("%d ",i);
}
return 0;
}
/*
370 371 407
*/
#include
#include
#include
int main()
{
long long int a[100][100],n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i
#include
#include
#include
int main()
{
double a, x0,x1;
scanf("%lf",&a);
x0=a/2;
x1=(x0+a/x0)/2;
while(fabs(x1-x0)>=(1e-6))
{
x0 = x1;
x1 = (x0+a/x0)/2;
}
printf("%f\n",x1);
}
/*
10
3.162278
*/
#include
#define eps 1e-6
double f(double x)
{
return 3*x*x*x-5*x*x+7*x-8;
}
double solve(double L, double R)
{
double left = L, right = R, mid;
while(right - left > eps)
{
mid = (left + right) / 2;
if(f(mid) >= 0)
right = mid;
else
left = mid;
}
return mid;
}
int main()
{
printf("%lf\n", solve(-10.0, 10.0));
return 0;
}
/*
1.377848
*/
#include
int main()
{
for(int i=1; i<=10; i++)
{
for(int j=1; j<=i; j++)
printf("*");
printf("\n");
}
}
/*
*
**
***
****
*****
******
*******
********
*********
**********
*/
P124 17. 一位老师问数学李老师:“你今年多少岁了?,你太太多少岁了?", 李老师想了一下,笑着说:“我年龄的平方加上我太太的年龄恰好等于1053,而我太太年龄的平方和加上我的年龄等于873”。你来算算吧!
#include
int main()
{
for(int i=10; i<=100; i++)//数学李老师
{
for(int j=10; j<=100; j++)//太太
{
if(i*i+j==1053&&j*j+i==873)
{
printf("老师:%d 太太:%d\n",i,j);
break;
}
}
}
return 0;
}
/*
老师:32 太太:29
*/
#include
int main()
{
double sum = 100, temp = 100.0;
int n = 10;
while(n--)
{
temp = temp / 2.0;
printf("%f\n", temp);
sum = sum + temp * 2;
}
printf("sum = %f 第10次反跳的高度%f\n", sum, temp);
return 0;
}
/*
1
1落地 50*2
2落地 25*2
3落地 12.5*2
4落地
50.000000
25.000000
12.500000
6.250000
3.125000
1.562500
0.781250
0.390625
0.195313
0.097656
sum = 299.804688 第10次反跳的高度0.097656
*/
#include
int main()
{
int i, ncow[20+1];
ncow[1] = 2;
ncow[2] = 3;
ncow[3] = 4;
for(int i=4; i<=20; i++)
ncow[i] = ncow[i-1] + ncow[i-3];
for(int i=1; i<=20; i++)
{
printf("%12d", ncow[i]);
if(i%5==0)
printf("\n");
}
return 0;
}
/*
2 3 4 6 9
13 19 28 41 60
88 129 189 277 406
595 872 1278 1873 2745
*/
元旦 1月1日,且2002年时平年.
#include
int month[13]={
0,
31,//1
28,//2
31,//3
30,//4
31,//5
30,//6
31,//7
31,//8
30,//9
31,//10
30,//11
31,//12
};
int main()
{
int m, d, i, week = 2, m0=1,d0=1, sum=1;
scanf("%d%d",&m, &d);
while(m0
假设A 4 x 3 B 3 x 5
#include
int main()
{
int a[4][3], b[3][5], ans[4][5];
for(int i=0; i<4; i++)
for(int j=0; j<3; j++)
scanf("%d",&a[i][j]);
printf("\n");
for(int i=0; i<3; i++)
for(int j=0; j<5; j++)
scanf("%d",&b[i][j]);
printf("\n");
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
ans[i][j] = 0;
for(int k=0; k<3; k++)
{
ans[i][j]+=a[i][k] * b[k][j];
}
printf("%d ",ans[i][j]);
}
printf("\n");
}
return 0;
}
/*
1 1 0
3 2 -1
-2 3 1
-1 -2 1
2 3 -1 0 0
-1 -2 1 0 1
3 -1 -2 3 1
1 1 0 0 1
1 6 1 -3 1
-4 -13 3 3 4
3 0 -3 3 -1
*/
#include
int main()
{
int n;
char name[13][10] =
{
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
} ;
scanf("%d",&n);
printf("%s\n",name[n]);
return 0;
}
/*
7
July
Process returned 0 (0x0) execution time : 1.308 s
Press any key to continue.
*/
P146 6-11编制一个程序,计算函数 ,并输出其曲线图形。
#include
#include
#define DELTA 0.0625
#define PI 3.14159
#define LINE 30
#define WIDTH 80
#define RATE 40
#define BASE 30
int main()
{
int i, j, n;
double x, y;
char array[WIDTH];
y=0.0;
for(int j=0; j<=WIDTH-1; j++)
array[j] = ' ';
for(i=1; i<=LINE; i++)
{
array[BASE] = '|';
x = exp(-y)*sin(2*PI*y);
n = (int)(BASE*x)+BASE;
array[n] = '*';
for(j=0; j<=WIDTH-1; j++)
printf("%c",array[j]);
printf("\n");
array[n]=' ';
y+=DELTA;
}
return 0;
}
/*
*
| *
| *
| *
| *
| *
| *
| *
*
* |
* |
* |
* |
* |
* |
* |
*
| *
| *
| *
| *
| *
| *
| *
*
* |
* |
* |
* |
* |
Process returned 0 (0x0) execution time : 0.554 s
Press any key to continue.
*/
冒泡排序
#include
#include
//冒泡排序,相邻的两两交换,n n-i,j, j+1记住
int main()
{
int a[100], n,t;
scanf("%d",&n);
for(int i=0; ia[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
for(int i=0; i
#include
#include
int ans[1000], k=0;
int p[1001] = {false};//标记
void IsPrime()
{
for(int i=2; i<1001; i++)
{
if(p[i] == false)
{
ans[k++] = i;
for(int j=i+i; j<1001; j+=i)
p[j] = true;
}
}
}
int main()
{
IsPrime();
int cnt=0;
for(int i=0; i
就是 ( A 为奇数)个自然数按方阵形状排列,要求他的每行上各个数的和,每列上各个数的和以及沿对角线方向的各个数的和都等于一个常数的方阵。这个常数等于:
.
方阵中各个数的位置按如下方法确定。
#include
#include
int main()
{
int i, j, ans[15][15], n;
while(scanf("%d",&n)!=EOF)
{
if(n%2==1)//n必须为奇数
{
i = 0;
j = n/2;//第一行的中间
for(int k=1; k<=n*n; k++)
{
ans[i][j] = k;
if(k % n == 0)
i++;
else
{
i--;
j++;
}
if(i<0) i+=n;
if(j>(n-1)) j-=n;
}
}
for(int i=0; i
#include
#include
int p[10];
int vis[20] = {false};
int n=9;
int sum=0;
int check(int n)//判断一个数是不是完全平方数
{
return n == (int)sqrt(n)*(int)sqrt(n);
}
void dfs(int index)
{
if(index==n)
{
int n1 = p[0]*100+p[1]*10+p[2];
int n2 = p[3]*100+p[4]*10+p[5];
int n3 = p[6]*100+p[7]*10+p[8];
if(check(n1)&&check(n2)&&check(n3))
{
printf("%d %d %d\n", n1, n2, n3);
}
}
for(int i=1; i<=n; i++)
{
if(vis[i]==false)
{
p[index] = i;
vis[i] = true;
dfs(index+1);
vis[i] = false;
}
}
}
int main()
{
dfs(0);
printf("%d\n",sum);
return 0;
}
/*
361 529 784
361 784 529
529 361 784
529 784 361
784 361 529
784 529 361
0
Process returned 0 (0x0) execution time : 0.303 s
Press any key to continue.
*/
#include
#include
#define PI acos(-1.0)
int main()
{
int n;
scanf("%d",&n);
//计算三角函数时,要转换为pi*(x)/180 pi = 180,x对应多少π
for(int i=0; i<=n; i++)
printf("%f\n",sin(PI*(double(i)/180)));
return 0;
}
/*
30
0.000000
0.017452
0.034899
0.052336
0.069756
0.087156
0.104528
0.121869
0.139173
0.156434
0.173648
0.190809
0.207912
0.224951
0.241922
0.258819
0.275637
0.292372
0.309017
0.325568
0.342020
0.358368
0.374607
0.390731
0.406737
0.422618
0.438371
0.453990
0.469472
0.484810
0.500000
*/
#include
#include
#define PI acos(-1.0)
int main()
{
int r, c;
while(scanf("%d%d",&r,&c)!=EOF)
{
int a[100][100];
for(int i=0; i= rmaxn && k1!=j)
break;
}
for(k2=0; k2
在一块平板上立有三根立柱,从左到右分别标记为A,B,C。最初在A柱上放有6个大小不等的圆盘,并且大盘在下面,小盘在上面。要求将这些盘从A移到C(可以借助B柱),条件是:每次只能移动一盘,并且不允许把大盘放在小盘的上面。
参考:https://blog.csdn.net/xb2355404/article/details/79144451
#include
#include
void mov(int n,char a, char c)
{
printf("move %d from %c to %c\n",n,a, c);
}
void hanoi(int n, int a, int b, int c)
{
if(n==1) mov(n,a, c);
else
{
hanoi(n-1, a, c, b);
mov(n,a, c);
hanoi(n-1, b, a, c);
}
}
int main()
{
hanoi(3,'a','b','c');
return 0;
}
/*
move 1 from a to c
move 2 from a to b
move 1 from c to b
move 3 from a to c
move 1 from b to a
move 2 from b to c
move 1 from a to c
*/
在学校门口摆放由红花和黄花组成的25 x 25的正方形花阵,一层红花, 一层黄花。问:需要多少盆红花?需要多少盆黄花?
#include
#include
int main()
{
//假设1x1的为黄花
int yellow = 1, red=0;
for(int i=2; i<=25; i++)
{
if(i%2==0)
red+=(i*i);
else
yellow+=(i*i);
}
printf("红花为%d\n黄花为%d\n",red, yellow);
return 0;
}
/*
红花为2600
黄花为2925
Process returned 0 (0x0) execution time : 0.026 s
Press any key to continue.
*/
P160 7-2.计算并输出三个电阻的串联值和并联值。分别由函数series()和parallel()实现。
#include
void series(double a1, double a2, double a3)//串联
{
double ans;
ans = a1 + a2 + a3;
printf("串联值为 %f\n", ans);
return;
}
void parallel(double a1, double a2, double a3)
{
double temp, ans;
temp = 1/a1 + 1/a2 + 1/a3;
ans = 1/temp;
printf("并联值为 %f\n", ans);
return;
}
int main()
{
double a1, a2, a3;
scanf("%lf%lf%lf",&a1,&a2,&a3);
series(a1, a2, a3);
parallel(a1, a2, a3);
return 0;
}
/*
1 2 3
串联值为 6.000000
并联值为 0.545455
*/
#include
//方法一
int main()
{
int n;
scanf("%d",&n);
double sum;
for(int i=1; i<=n; i++)
{
sum += 1.0/double(i);
}
printf("%f\n",sum);
return 0;
}
/*
10
2.928968
Process returned 0 (0x0) execution time : 3.110 s
Press any key to continue.
*/
#include
int x,y;
int gcd(int a, int b)
{
if(b==0) return a;
return gcd(b, a%b);
}
void add(int a1, int b1,int a2, int b2)
{
x = a1*b2+a2*b1;
y = b1*b2;
int temp;
if(x>y)
temp = gcd(x,y);
else
temp = gcd(y,x);
//printf("%d/%d\ %dn", x,y, );
x = x/temp;
y = y/temp;
}
//方法二结果为分数
int main()
{
int n;
scanf("%d",&n);
x=1,y=1;
for(int i=2; i<=n; i++)
{
add(x,y,1,i);
}
printf("%d/%d\n",x,y);
return 0;
}
/*
10
7381/2520
Process returned 0 (0x0) execution time : 1.452 s
Press any key to continue.
*/
要求在8 x 8的国际象棋上放8个皇后,使任意两个皇后不能处于同一行,同一列,同一条斜线上。
#include
#include
#include
int vis[10] = {false};
int p[10], sum=0;
int n;
void dfs(int index)
{
if(index==n+1)
{
sum++;
return;
}
for(int i=1; i<=n; i++)
{
if(vis[i]==false)
{
int flag = true;
//当前坐标(index,i) 前一个坐标(k,p[k])
//注意注意:这里为k
#include
#include
#include
char a, b, c;
void mov(int n, char a, char b)
{
printf("move %d from %c to %c\n",n, a, b);
}
void hanoi(int n, char a, char b, char c)
{
if(n == 1) mov(n, a, c);
else
{
hanoi(n-1, a, c, b);
mov(n, a, c);
hanoi(n-1,b, a, c );
}
}
int main()
{
hanoi(3, 'a', 'b', 'c');
return 0;
}
/*
move 1 from a to c
move 2 from a to b
move 1 from c to b
move 3 from a to c
move 1 from b to a
move 2 from b to c
move 1 from a to c
Process returned 0 (0x0) execution time : 0.332 s
Press any key to continue.
*/
#include
#include
#include
int prime(int n)
{
int sqr = sqrt(n);
if(n==1)
return false;
for(int i=2; i<=sqr; i++)
{
if(n%i==0)
return false;
}
return true;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=n; i<=m; i++)
{
if(prime(i)==true)
printf("%d ", i);
}
printf("\n");
}
return 0;
}
/*
1 10
2 3 5 7
1 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
1 1000
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 1
07 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 2
23 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 3
37 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 4
57 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 5
93 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 7
19 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 8
57 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 9
97
*/
#include
#include
#include
int main()
{
int n,r;
while(scanf("%d%d",&n,&r)!=EOF)
{
int ans = 1;
for(int i=n-r+1; i<=n; i++)
{
ans*=i;
}
printf("%d\n",ans);
}
return 0;
}
/*
10 5
30240
10 9
3628800
10 1
10
*/
#include
#include
#include
typedef long long int LL;
LL dg(LL m, LL n)
{
if(m==n || n==0)
return 1;
else
return dg(m-1,n) + dg(m-1, n-1);
}
int main()
{
LL n,m;
scanf("%lld%lld",&m, &n);
printf("%lld\n",dg(m,n));
return 0;
}
/*
4 2
6
Process returned 0 (0x0) execution time : 2.734 s
Press any key to continue.
*/
#include
#include
int main()
{
double d;
char str[] = "123.45e-5";
d = atof(str);
printf("%f\n",d);//%e 使用科学计数法输出
return 0;
}
/*
0.001234
*/
#include
#include
int main()
{
double a, b, c,d;
for(int x=1; x<=44; x++)
{
a = (3*(double)x-44)*(3*(double)x-44)/(double)x;
b = 3*(double)x-44;
c = (double)x;
d = 44 - (double)x;
if(a>0&&b>0&&c>0&&d>0&&(a-(int(a))==0.0)&&(b-(int(b))==0.0)&&(c-(int(c))==0.0)&&(d-(int(d))==0.0))
{
printf("%.0f %.0f %.0f %.0f\n", a, b, c, d);
break;
}
}
return 0;
}
/*
1 4 16 28
*/
#include
#include
int strlen(char *a)
{
int n=0;
while(*a++!='\0')
n++;
return n;
}
void reverse(char *s)
{
int c,k;
char *p;
k = strlen(s);
for(p = s+k-1; s
#include
#include
double f(double x)
{
return (1+x*x);
}
int main()
{
double a, b;
scanf("%lf%lf",&a, &b);
int n=100; //将区间划分为50份
double s = 0, i=a, h = (b-a)/double(n);
//printf("%f %f %f\n",a, b, h);
while(i
有n个小孩围成一圈,顺序排号。从第1个人开始报数(从1报到5), 凡报到5的人退出圈子,问最后留下的小孩原来是第几号。
#include
int main()
{
int n, m;
while(scanf("%d%d",&n, &m)!=EOF)
{
int a[n+1];
for(int i=0; i
#include
#define MAXLINE 1000
int getline(char *s, int lim) //把取到的行送入s中,并返回其长度
{
int c, i ;
for(i=0; i0)
{
if(len > maxn)
{
maxn = len;
copy(line, save);
}
}
if(maxn > 0)//找到一行是最长的
printf("%s", save);
return 0;
}
/*
hello world!
hello me!
^Z
hello world!
*/
#include
void print_1(int *p)//输出整个数组元素
{
int i;
for(i=0; i<4; i++)
printf("%d ",*(p+i));
printf("\n");
}
void print_2(int (*pa)[4], int n)//输出第n行的元素
{
int j;
for(j=0; j<4; j++)
printf("%d ", *(*(pa+n)+j));
}
int main()
{
int multi[3][4] = {{1,3,5,7},{2,4,6,8},{10,20,30,40}};
int (*ptr)[4], count;
ptr = multi;
for(count=0; count<3; count++)
print_1(*ptr++);
getchar();//输入回车
print_2(multi, 1);
printf("\n");
return 0;
}
/*
1 3 5 7
2 4 6 8
10 20 30 40
2 4 6 8
*/
P215 8-7.写一