2016级计算机C++助教工作(12) 第二次上机解题报告
2016-12-26 13:39
453 查看
A. Brainman
冒泡排序,平方的复杂度能过,循环N次,每次判断相邻两个数是否要交换,统计次数即可#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int num[1001]; int main(){ int t,n; cin>>t; for(int tt = 1; tt <= t; tt++){ cin>>n; for(int i = 0;i < n; i++){ cin>>num[i]; } int ans = 0; for(int i = 0;i < n; i++){ for(int j = 0;j < n -1 ; j++){ if(num[j] > num[j+1]){ swap(num[j],num[j+1]); ans++; } } } printf("Scenario #%d:\n",tt); cout<<ans<<endl<<endl; } return 0; }
B. Eazzzzzy
根据三种情况,分别输出图形接口,每一行的输出格式都是能算出的#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int main(){ int t,n,c; while(cin>>t){ if(t == -1) return 0; if(t == 1){ cin>>n; for(int i = 0;i < n; i++){ for(int j = 0;j < n - i - 1; j++) cout<<" "; for(int j = 0;j < i*2+1; j++) cout<<"*"; cout<<endl; } } if(t == 2){ cin>>n>>c; for(int i = 0;i < c; i++){ for(int j = 0;j < c - i - 1;j++) cout<<" "; for(int j = 0;j < n; j++) cout<<"*"; cout<<endl; } } if(t == 3){ cin>>n>>c; for(int i = 0;i < c; i++){ for(int j = 0;j < n; j++) cout<<"*"; cout<<endl; } } cout<<endl; } }
C. Primary Arithmetic
按位加法,统计进位数即可#include <iostream> using namespace std; int main() { int a,b,c,d; while(cin>>a>>b){ if(a==0&&b==0) break; c=d=0; while(a!=0 || b!=0 ){ c = a%10 + b%10 + d; a = a/10; b = b/10; if(c>9){ d++; } } if(d==0) cout<<"No carry operation."<<endl; else if(d==1) cout<<"1 carry operation."<<endl; else cout<<d<<" carry operations."<<endl; } }
D. Binary Numbers
输出二进制数中1的位置#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int main(){ int t,n; cin>>t; while(t--){ cin>>n; int f = 0,p = 0; while(n){ if(n % 2 == 1){ if(f)cout<<" "; cout<<p; f = 1; } p++; n /= 2; } cout<<endl; } }
E. Digital Roots
实际把所有数字加起来对9取模即可,特判结果是0的情况,#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; char x[1000]; int main(){ int n; while(cin>>x){ if(x[0] == '0')break; int len = strlen(x); int t = 0; for(int i = 0;i < len; i++) t += x[i] - '0'; t = t % 9; if(t == 0) t = 9; cout<<t<<endl; } return 0; }
相关文章推荐
- 2016级计算机C++助教工作(11) 第三次上机解题报告
- 2016级计算机C++助教工作(10)第三次第四次上机题注意事项
- 2016级计算机C++助教工作(8)第二次上课内容
- 2016级计算机C++助教工作(7)第一次上机题
- 2016级计算机C++助教工作(3) 提问方式 和 发送邮件的方式
- 2016级计算机C++助教工作(6)OJ上各种返回结果以及代表意思和可能涉及的原因
- 2016级计算机C++助教工作(2)基本语法与资料查找方法
- 2016级计算机C++助教工作(4)数组定义 与 优美的写代码的姿势
- 2016级计算机C++助教工作(5) 第一次讲课内容
- 2016级计算机C++助教工作(1) codeblocks的汉化与相关操作
- 2016级计算机C++助教工作(9)第三次上课内容
- 16级C++第二次上机解题报告
- 北京航空航天大学2014第二次上机解题报告
- 16级C++第三次上机解题报告
- C++第二次上机实验报告-1
- 【作业解答】第二次上机作业解题报告
- C++第二次上机报告
- 2016级数据结构第一次上机解题报告
- C++第二次实验上机报告(计算标准体重)
- C++ 第二次上机实验报告