## 算法入门经典第二版（紫书） 第二章 2-1 -> 2-6

2018年04月29日 ⁄ 综合 ⁄ 共 1634字 ⁄ 字号 评论关闭

这章的题应该都算是水题了，都是一些最基本入门的模拟，不涉及任何算法，LRJ老师为的就是让大家熟悉熟悉竞赛的入门和最简单的竞赛.只要认真的模拟和思考，然后结合书本前面讲的那些知识的灵活运用，分分秒秒不在话下、、、、

2-1

```# include<cstdio>
# include<iostream>

using namespace std;

int a,b,c;

int f( int x )
{
a = x/100;
b = x/10%10;
c = x%10;
if ( a*a*a+b*b*b+c*c*c == x )
return 1;
else
return 0;

}

int main(void)
{
//int a,b,c;
for ( int i = 100;i < 200;i++ )
{
if(f(i))
cout<<i<<endl;
else
continuel

}

return 0;
}
```

2-2

```# include<cstdio>
# include<iostream>

using namespace std;

int main(void)
{
int a,b,c;
cin>>a>>b>>c;
int cnt = 0;
int flag = 1;
for ( int i = 10;i <= 100;i++ )
{
if ( i%3==a&&i%5==b&&i%7==c )
{
cnt++;
flag = 0;
printf("Case %d %d",cnt,i);
}

}
if ( flag)

return 0;
}
```

2-3

```# include<cstdio>
# include<iostream>

using namespace std;

int main(void)
{
int n;cin>>n;
int k = 0;
for ( int i = 0;i < n;i++ )
{
k = i;
for ( int j = 0;j < k;j++ )
printf(" ");
for ( int l = 0;l < 2*n-2*i-1;l++ )
printf("#");
printf("\n");
}

return 0;
}
```

2-4

```# include<cstdio>
# include<iostream>

using namespace std;

int main(void)
{
int n,m;
int cnt = 0;
double o;
while ( (scanf("%d%d",&n,&m)!=EOF)&&n!=0&&m!=0 )
{
double sum = 0;
for ( int i = n;i <= m;i++ )
{
o = i;
sum += 1.0/(o*o);
//cout<<sum<<endl;
}
printf("Case %d: %.5lf",++cnt,sum);

}

return 0;
}
```

2-5

```# include<cstdio>
# include<iostream>

using namespace std;

int main(void)
{
int a,b,c;
int y;//余数
int s;//商
while ( scanf("%d%d%d",&a,&b,&c)!=EOF&&a!=0&&b!=0&&c!=0 )
{
s = a/b;y = a%b;
printf("%d.",s);
for ( int i = 1;i <= c;i++ )
{
if ( (i<c)&&(c!=1) )
{
printf("%d",y*10/b);
}
else
{
if ( (y*10/b)<5 )
printf("%d",(y*10)/b);
else
printf("%d",(y*10)/b+1);

}
y=y*10%b;
}
}
return 0;
}
```

2-6

```# include<cstdio>

int main(void)
{
for ( int i = 123;i < 333;i++ )
{
if ((i%10)!=(i/10%10)&&(i%10)!=(i/100)&&(i/10%10)!=(i/100)
&&((i%10)*(i/10%10)*(i/100)*(2*i%10)*(2*i/10%10)
*(2*i/100)*(3*i%10)*(3*i/10%10)*(3*i/100)==362880))
printf("%d %d %d\n",i,i*2,i*3);
}

return 0;
}
```