2016级计算机C++助教工作(12) 第二次上机解题报告

来源:互联网 发布:切尔西 知乎 编辑:程序博客网 时间:2024/05/22 10:45

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;}





0 0
原创粉丝点击