码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-13 11:32:45      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

欢迎交流讨论!

@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[n], 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; }

  

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

标签:

原文地址:http://www.cnblogs.com/wbstackpop/p/4289869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!