zoukankan      html  css  js  c++  java
  • 牛客网:机试在线训练(1)

    字符串分隔

    连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;   
    长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。 

    输入:

    abc
    123456789

    输出:

    abc00000
    12345678
    90000000

     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 int main(){
     6     string str;
     7     while(cin >> str)
     8     {
     9         string temp(8, '0');
    10         if(str.size()<= 8 )
    11         {
    12             temp.replace(0, str.size(), str);
    13             cout << temp << endl;
    14         }
    15         else{
    16             int loop = str.size()/8;
    17             int remain = str.size()%8;
    18             for(int i = 0; i < loop; i++)
    19             {
    20                 temp.replace(0, 8, str, i*8, 8 );
    21                 cout << temp << endl;
    22             }
    23             if(remain){
    24                 temp = "00000000";
    25                 temp.replace(0, remain, str, loop*8, remain);
    26                 cout << temp << endl;
    27             }
    28         }
    29     }
    30     return 0;
    31 }

    进制转换

    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

    输入一个十六进制的数值字符串。

    输出该数值的十进制字符串。

    输入:

    0xA

    输出:

    10

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int a;
     7     while(cin >> hex >>a){
     8     cout << a << endl;
     9     }
    10 }

    质数因子

    功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     long a;
     7     while(cin >> a)
     8     {
     9         while(a != 1)
    10         {
    11             for(long j = 2; j<= a; j++ )
    12             {
    13                 if(a%j == 0)
    14                 {
    15                     a /= j;
    16                     cout << j << ' ';
    17                     break;
    18                 }
    19             }
    20         }
    21     }
    22     return 0;
    23 }

    坐标移动

    开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

    输入:

    合法坐标为A(或者D或者W或者S) + 数字(两位以内)

    坐标之间以;分隔。

    非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。

    下面是一个简单的例子 如:

    A10;S20;W10;D30;X;A1A;B10A11;;A10;

    处理过程:

    起点(0,0)

    +   A10   =  (-10,0)

    +   S20   =  (-10,-20)

    +   W10  =  (-10,-10)

    +   D30  =  (20,-10)

    +   x    =  无效

    +   A1A   =  无效

    +   B10A11   =  无效

    +  一个空 不影响

    +   A10  =  (10,-10)

    结果 (10, -10)

    输入:

    一行字符串

    输出:

    最终坐标,以,分隔

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        string input;
        char separator = ';';
        string valid = "0123456789";
        int value = 0;
        
        while(getline(cin, input))
        {
            vector<int> points(2, 0);
            size_t new_start = 0;
            size_t posSeparator = input.find(separator);
            while(posSeparator != string::npos)
            {
                string current_command = input.substr(new_start, posSeparator - new_start);
                new_start = posSeparator + 1;
                posSeparator  = input.find(separator, new_start);
                
                if(current_command.size() <=1 || current_command.size() >3)
                    continue;
                if(current_command[0] == 'A' ||current_command[0] == 'S'||current_command[0] == 'D'||current_command[0] == 'W' )
                {
                    string small = current_command.substr(1);
                    if(small.find_first_not_of(valid) == string::npos )
                    {
                        value = stoi(small, nullptr, 10);
                        if(current_command[0] == 'A')
                            points[0] -= value;
                        else if(current_command[0] == 'D')
                            points[0] += value;
                        else if(current_command[0] == 'W')
                            points[1] += value;
                        else if(current_command[0] == 'S')
                            points[1] -= value;
                         
                    }
                }
            }
            cout << points[0] << ',' << points[1] << endl;
        }
        return 0;
    }

    汽水瓶换饮料

    有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝? 

    对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。

    输入:

    3
    10
    81
    0

    输出:

    1

    5

    40

    #include<iostream>
    using namespace std;
    int main()
    {
        int input;
        while(cin >> input)
        {
            int drink = 0;
            if(input == 0) break;
            if(input == 1) cout << drink << endl;
            
            int empty = input;
            int full = empty;
            int remain = 0;
            while(full){
                full = empty / 3;
                drink += full;
                remain = empty % 3;
                empty = full + remain;
            }
            if(remain == 2)
                drink++;
            
            cout << drink << endl;
        }
        return 0;
    }

    最小公倍数

    输入两个正整数A和B

    输出A和B的最小公倍数

    最小公倍数 = A x B / 最大公约数

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int gcd(int a, int b);
    int main(){
        int a, b;
        int minMax;
        while(cin >> a >> b){
           minMax = a*b/gcd(a,b);
            cout << minMax << endl;
        }
    }
    
    int gcd(int a, int b)
    {
        if(b == 0)
            return a;
        return gcd(b, a%b);
    }

    求解立方根

    输入:double 待求解参数

    返回值:double  输入参数的立方根,保留一位小数

    牛顿迭代法

    #include<iostream>
    #include<iomanip>
    #include<algorithm>
    using namespace std;
    int main()
    {
        double a=0.0, t = 0.0;
        double e = 0.0001; 
        while(cin >> a){
            t = a;
            while(abs(t*t*t - a) > e){
                t = t - ( t*t*t - a )* 1.0 / (3 * t*t);
                  }
            cout << setiosflags(ios::fixed)<< setprecision(1) << t << endl;
        }
        return 0;
    }
  • 相关阅读:
    笔记:Oracle查询重复数据并删除,只保留一条记录
    64位系统安装ODBC驱动的方法
    批量Excel数据导入Oracle数据库
    Oracle自我补充之Decode()函数使用介绍
    解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
    Linux命令区
    Linux下安装PHP+Nginx+Msql
    Thinkphp时间转换与统计的问题
    phpStydy配置memcache扩展
    Thinkphp+Nginx(PHPstudy)下报的404错误,403错误解决
  • 原文地址:https://www.cnblogs.com/heifengli/p/9673785.html
Copyright © 2011-2022 走看看