2-1
#include <iostream>
#include <stdio.h>#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
for(int i = 100;i <= 999;i++){
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if(a*a*a + b*b*b +c*c*c == i)
cout<<i<<endl;
}
return 0;
}
2-2
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
int a,b,c;
int kase = 1;
while(cin>>a>>b>>c){
int ans = 0;
ans = a*70 + b*21 + c*15;
while(ans > 105)
ans -= 105;
if(ans > 100 || ans < 0 )
cout<<"Case "<<kase<<": "<<"No answer"<<endl;
else
cout<<"Case "<<kase<<": "<<ans<<endl;
kase++;
}
return 0;
}
2-3
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
int n;
while(cin>>n){
for(int i = n;i > 0;i--){
for(int j = 0;j < n-i;j++)
cout<<" ";
for(int k = 0;k < 2*i-1;k++){
cout<<"#";
}
cout<<endl;
}
}
return 0;
}
2-4
//本题陷阱在于n比较大时,n*n会溢出,所以 1/n^2 应该用 1/n/n 而不是 1/(n*n)。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
int n,m;
int kase = 1;
while(cin>>n>>m && (n||m)){
double ans = 0;
for(int i = n;i <= m;i++){
ans += (double)1/i/i;
}
printf("Case %d: %.5f\n",kase,ans);
kase++;
}
return 0;
}
2-5
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#define INF 1000000000
using namespace std;
int main()
{
int a,b,c;
int kase = 0;
while(cin>>a>>b>>c &&(a||b||c)){
//cout << fixed << setprecision(c)<<(double)a/b<<endl;//不用那么麻烦的做法
kase++;
printf("Case %d: %d.",kase,a/b);//打印整数部分及小数点
a%=b;
for(int i=1;i<c;i++){
printf("%d",a*10/b);
a=a*10%b;
}
//最后一位,四舍五入处理
if(a*10%b*10/b>=5)//c+1位数据判断,若大于等于5
printf("%d\n",a*10/b+1);//c位需 五入
else
printf("%d\n",a*10/b);
}
return 0;
}
2-6
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
int def,ghi;
int a,b,c,d,e,f,g,h,i;
for(int abc = 123;abc < 333;abc++){
bool arr[10] = {false};
a = abc/100;
b = abc/10%10;
c = abc%10;
if(a == 0 || b == 0 || c == 0 || a == b || b == c || a == c) continue;
arr[a] = arr[b] = arr[c] = true;
def = 2*abc;
d = def/100;
e = def/10%10;
f = def%10;
if(d == e || e == f || d == f) continue;
if(arr[d] == false && arr[e] == false && arr[f] == false)
arr[d] = arr[e] = arr[f] = true;
else continue;
ghi = 3*abc;
g = ghi/100;
h = ghi/10%10;
i = ghi%10;
if(arr[g] == false && arr[h] == false && arr[i] == false)
printf("%d %d %d\n",abc,def,ghi);
else continue;
}
return 0;
}
思考题2
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
using namespace std;
int main()
{
double i;
for(i = 0;i != 10;i += 0.1)
printf("%.1f\n",i);
return 0;
}
//无限循环