前三章作业总结

来源:互联网 发布:阿里云dns 编辑:程序博客网 时间:2024/06/05 10:51
  1. 对n个人分班,每班k(k>0)人,最后不足k人也编一个班,问要分几个班?键盘输入n, k的值,输出分班数。 (尝试使用if语句)

    #include <iostream>using namespace std;int main(){    int n,k;    cout<<"请给出总人数n和每班人数k:";    cin>>n>>k;    if(n%k = =0)            cout<<"分班数:"<<n/k<<endl;  //简化输出    else cout<<"分班数:"<<n/k+1<<endl;    system("pause");    return 0;}

    简单计算可直接在输出时进行,理清思路可减少代码量。

    灵活运用int i/int j

  2. 将用户输入的一个(0~99)十进制数转换为十六进制数输出。例如,用户输入89,输出其十六进制表示的数为:0x59。

    #include <iostream>using namespace std;int main(){     int a,b,c;     cout<<"iput a decimal integer(0-99):";     cin>>a;     b=a/16;     c=a%16;     cout<<"The result is : 0x";     if (b == 0)        cout << char(c > 9 ? 'A' + c - 10 : c) << endl;     else        cout<< b << char(c > 9 ? 'A' + c - 10 : c) << endl;     system("pause");     return 0;}

    灵活使用三目运算符

    另解

    #include <iostream>using std::cout;using std::cin;using std::endl;int main() {    int n, a, b;    cout << "input a decimal integer(0-99)";    cin >> n;    cout << "Its hexadecimal number integer is:" << "0x";    if (n >= 0 && n <= 9)        cout << n << endl;    else if (n < 16)        cout << char('A' + n - 10) << endl;    else    {        a = n % 16;        b = n / 16;        cout << b << (a < 10 ? a : char('A' + a - 10)) << endl;    }    system("pause");    return 0;}
  3. 输出100~1000之间的10个随机数

    #include<iostream>#include<ctime>using namespace std;int main(){    srand(unsigned(time(0)));    int a;    for(int i=0;i!=10;i++){        a=rand()%901+100; //运用%运算符和加减运算来控制随机数范围        cout<<a<<' ';    }    cout<<endl;    system("pause");    return 0;}

    rand()控制随机数产生

  4. 有一个分支函数

    编写一程序,输入x,输出y的值,要求分别用if语句和条件表达式求解y。

    int main(){int  i=1;while(i==1){    cout<<"输入x的值:";    double x,y;    cin>>x;    y=x<1? x:(x<10? 2*x-1:3*x-11);  //三目运算极其简单!    cout<<"y="<<y<<endl;    cout<<"还要继续练习吗(1--yes,0--no)?";    cin>>i;}return 0;}

    嵌套三目运算符的应用


  5. i=120n!

    #include<iostream>using namespace std;int main() {long long int sum = 0;int prec = 0;for (int j = 1; j != 21; j++) {       //每次循环重新确定过程量值,嵌套循环常用    long long int  a = 1;             for (int i = 1; i != j + 1; i++) {        a *= i;    }    sum += a;}cout << "该式结果为" << sum << endl;system("pause");return 0;

    long long 类型有助于拓展数据

    嵌套循环中重新确定过程量

  6. 将输入的整数各位上为偶数的数取出,按原来从高位到低位的顺序组成一个新数输出

    #include<iostream>#include<math.h>using namespace std;int main() {long long int i;cout << "请输入一个整数:";cin >> i;long long j = 0, a = 0, n = 0;while (i) {    a = i % 10;    i = i / 10;    if (a % 2 == 0) {        j += a * pow(10.0,double(n)) ;        n++;    }}cout << j << endl;system("pause");return 0;}

    从高位到低位输出时用pow(double,double)来控制,注意格式转化

    也可用循环乘自己来代替pow

    #include <iostream>using std::cout;using std::cin;using std::endl;int main(){long long n,nn=0,d=1;cout << "请输入一个数: ";cin >> n;for (;n!=0;n=n/10){    int a = n % 10;    if (a % 2 == 0) {        nn += a*d;        d *= 10;    }}cout << "新数为" << nn << endl;system("pause");return 0;}
  7. 程序设计,求出1~1000之间能被7或11整除,但不能同时被7和11整除的所有整数 ,一行输出8个数并对齐。

    #include<iostream>#include<iomanip>using namespace std;int main() {int j = 0;bool c;    //直接进行多个条件比较时,bool运算符控制起来比较简单for (int i = 1; i != 1000; i++) {    if ((i % 7 == 0 || i % 11 == 0)&&!(c=i%7==0&&i%11==0)) {        cout << setw(4) << i;        j++;        if (j % 8 == 0) cout << endl;    }}system("pause");return 0;}

    灵活运用布尔运算符控制条件

    使用set(w)等小伎俩进行对齐操作

  8. 程序功能,将用户输入的一行字符串中的数字字符进行转换,转换规则:将‘0’转换成‘9’,‘1’转换成‘8’,‘2’转换成‘7’,…,‘9’转换成‘0’;若是其他字符则保持不变,将转换后的字符串输出

    #include<iostream>using namespace std;int main() {cout << "请输入字符:";int n = 1;                        //为了控制输出“结果为”while (1) {    char c = getchar();    if (n == 1) cout << "结果为  ";//仅在第一次循环中输出“结果为”    if (c < 48 || c > 57)  cout << c;    else cout << char(105 - c);    n++;}system("pause");return 0;}

    getchar()的运用

    用计数器n或flagbool灵活控制

0 0
原创粉丝点击