2015百度实习机试题(两道在线编程)

来源:互联网 发布:淘宝返现软件哪个好 编辑:程序博客网 时间:2024/05/29 13:51


#include<iostream>using namespace std;int main(void) {     int group,hillNUM;     int arr[10][1000],R[10];      cin>>group;      for(int i=0;i<group;i++)     {              cin>>hillNUM;               R[i]=0;              for(int j=0;j< hillNUM;j++)              {                       cin>>arr[i][j];              }              R[i]+=arr[i][0]*2;              for(j=1;j<hillNUM;j++)                       R[i]+=(arr[i][j]-arr[i][j-1])*2;              R[i]+=arr[i][--j]*2;               cout<<R[i]<<endl;         }return 0;}


 

sample input

5

bBb

bbbb

abcd

AB

CC

 Sample output

32

32

10

12

48

 //参考答案

#include<iostream>#include<string>using namespace std;//自定义的接收一行字符串string get_a_line(istream& ins){         char c;         char cStr[50]={" "};          string str;         int i=0;         ins.get(c);         while( c != '\n')         {                   cStr[i++]=c;                   ins.get(c);         };         str=cStr;//C字符串可以直接赋值给string对象         return str;}void new_line(){         char next;         do         {                   cin.get(next);         }while(next != '\n');} intcharToint(char temp){         int tt;         if(temp>='a')                   tt=temp-'a'+1;         else                   tt=temp-'A'+1;         return tt;} boolisCapital(char ch){         return( ch>='A' &&ch<='Z');}  intmain(void) {                  string str[1000];         int group;         int R[1000];          cin>>group;         new_line();          for(int i=0;i<group;i++)         {                   str[i]=get_a_line(cin);                   R[i]=0;                               for(int j=0;j<str[i].length();j++)                   {                            char temp=str[i][j];                            int k=1;                             while(charToint(str[i][j+1])==charToint(temp))                            {                                     k++;                                     if(str[i][j+1]< 96)                                               k++;                                     j++;                            }                            if(isCapital(temp))                            {                                     k++;                                     R[i]+=charToint(temp)*k*k;                            }                            else                                     R[i]+=charToint(temp)*k*k;                   }                }         for(i=0;i<group;i++) cout<<R[i]<<endl;   return 0;         } 



 

 

 

 

 


0 0