您的位置:首页 > 其它

刘汝佳算法竞赛入门经典 第二单元习题答案自编

2015-02-13 10:53 597 查看
欢迎交流讨论!

@2-1
#include <fstream>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(){
int n;
while(fin>>n){
int count = 0;
//计算位数
while(n){
count++;
n /= 10;
}
fout << count << endl;
}
return 0;
}

@2-2
#include <fstream>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(){
int n;
for(int i = 100; i < 1000; i++){
int A = i/100, B = i%100%10, C = i % 10;
if(A*A*A+B*B*B+C*C*C == i)
fout << i << "^_______________________________________________________^"<< endl;
else fout << "it is not~" << endl;
}
return 0;
}

@2-3
#include <fstream>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(){
int a, b, c, i;
while(fin >>a>>b>>c){
for(i = 10; i <= 100; i++){
if(i%3 == a && i%5==b && i%7==c){
fout << i << endl;
break;
}
}
if(i == 101) fout << "No answer" << endl;
}
return 0;
}

@2-4
#include <fstream>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(){
int n;
for(int i = 1; i <=5; i++){
//输出空格0-4个
for(int j = 1; j <= i-1; j++) fout << " ";
//输出# 11-2i
for(int k = 1; k <= 11-2*i; k++) fout << "#";
fout << endl;
}
return 0;
}

@2-5
#include <fstream>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");

int main(){
int n;
while(fin >> n){
int a
, m, i = 0, count = 0;
while(i < n) fin >> a[i++];
fin >> m;
for(i = 0; i < n; i++){
if(a[i] < m) count++;
}
fout << count << endl;
}
return 0;
}

@2-6
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");

int main(){
int n;
while(fin >> n){
double count = 0;
int i = 1;
while(i <= n) count += 1.0/i;
fout << fixed<< setprecision(3) << count << endl;
}
return 0;
}

@2-7
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");

int main(){
double count = 0;
int i = 1, flag = -1;

while(1.0/i >= 1.0e-6){
flag *= -1;//flag取反,使每一项变符号
double temp = 1.0/i;
count += flag * temp;
i += 2;
}
fout << count << endl;
return 0;
}

@2-8
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(void){
long long  n, m;
while(fin >> n >> m){
long long loop = n;
double sum = 0.0;
while(loop <= m){
double add = 1.0/(loop*loop);
sum += add;
loop++;
}
fout << fixed << setprecision(5)<< sum << endl;
}
return 0;
}

@2-9
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(void){
int a, b, c;
while(fin >> a >> b >> c){
fout << fixed << setprecision(c) << a*1.0/b << endl;
}
return 0;
}

@2-10
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
ifstream fin("aplusb.in");
ofstream fout("aplusb.out");
int main(void){
int abc, def, ghi, hash[10];
for( abc = 123; abc <= 329; abc++){
memset(hash, 0, sizeof(hash));
def = 2*abc, ghi = 3*abc;
hash[def/100] = 1, hash[def%100/10] = 1, hash[def%10] = 1;
hash[abc/100] = 1, hash[abc%100/10] = 1, hash[abc%10] = 1;
hash[ghi/100] = 1, hash[ghi%100/10] = 1, hash[ghi%10] = 1;

int countOne = 0;
for(int i = 1; i < 10; i++){
if(hash[i] == 1)
countOne++;
}
if(countOne == 9)
fout << "abc:def:ghi is:\n" << abc <<":"<< def << ":"<< ghi <<endl;
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: