牛客網-2019年校招真題-經過率降序(二)

6.表達式求值c++

今天上課,老師教了小易怎麼計算加法和乘法,乘法的優先級大於加法,可是若是一個運算加了括號,那麼它的優先級是最高的。例如:
1+2*3=7
1*(2+3)=5
1*2*3=6
(1+2)*3=9
如今小易但願你幫他計算給定3個數a,b,c,在它們中間添加"+", "*", "(", ")"符號,可以得到的最大值。

輸入描述:

一行三個數a,b,c (1 <= a, b, c <= 10)

輸出描述:

可以得到的最大值
示例1

輸入

1 2 3

輸出

9
 1 using System;
 2 using System.Text;
 3 using System.Text.RegularExpressions;
 4 namespace AAAAA
 5 {
 6     class AA
 7     {
 8         public static void Main(string[] args)
 9         {
10             string str = Console.ReadLine();
11             string[] arr = Regex.Split(str, " ");
12             int[] shuZu=new int[3];
13             int n = 0;
14             int sum = 0;
15             foreach (string str2 in arr)
16             {
17                 shuZu[n]=Convert.ToInt32(str2);
18                 n++;
19             }
20             int[] result=new int[6];
21             result[0]=shuZu[0]+shuZu[1]+shuZu[2];
22             result[1]=shuZu[0]*shuZu[1]*shuZu[2];
23             result[2]=shuZu[0]*shuZu[1]+shuZu[2];
24             result[3]=shuZu[0]+shuZu[1]*shuZu[2];
25             result[4]=(shuZu[0]+shuZu[1])*shuZu[2];
26             result[5]=shuZu[0]*(shuZu[1]+shuZu[2]);
27             Console.WriteLine(Math.Max(result[0],Math.Max(result[1],Math.Max(result[2],Math.Max(result[3],Math.Max(result[4],result[5]))))));
28         }
29     }
30 }

7.模數求和spa

現給定n個整數,並定義一個非負整數m,且令f(m) = (m%a 1)+(m%a 2)+...+(m%a n)。
此處的X % Y的結果爲X除以Y的餘數。
現請你找出一個m,求出f(m)的最大值。

輸入描述:

輸入包含兩行,第一行爲一正整數n,(1<n<=3000)
第二行爲n個整數a
1
,a
2
,...,a
n
,其中(2<=a
i
<=10^5)

輸出描述:

輸出僅包含一行,輸出f(m)的最大值
示例1

輸入

3
3 4 6

輸出

10

說明

就樣例而言,當m取11時可取得最大值。
 1 using System;
 2 using System.Text;
 3 using System.Text.RegularExpressions;
 4 
 5 namespace WorkAttendance
 6 {
 7     class Attendance
 8     {
 9         public static void Main(string[] args)
10         {
11            int m = System.Convert.ToInt32(Console.ReadLine());
12             string str = Console.ReadLine();
13             string[] arr = Regex.Split(str, " ");
14             int n = 0;
15             int sum = 0;
16             foreach (string str2 in arr)
17             {
18                 sum += Convert.ToInt32(str2) - 1;
19                 n++;
20             }
21             Console.WriteLine(sum); 
22         }
23     }
24 }

 8.二進制中有多少個一code

把一個32-bit整型轉成二進制,其中包含多少個1,好比5的二進制表達是101,其中包含2個1blog

輸入描述:

輸入爲整型(十進制),只需兼容32-bit便可,如五、32

輸出描述:

輸出爲字符串,如「2」、「1」
示例1

輸入

5

輸出

2

說明

5的二進制是101,其中包含2個1
 1 using System;
 2 using System.Text;
 3 namespace AAAAA
 4 {
 5     class AA
 6     {
 7         public static void Main(string[] args)
 8         {
 9             int a = Convert.ToInt32(Console.ReadLine());
10             string str = Convert.ToString(a, 2);
11             int c = 0;
12             foreach (char ch in str)
13             {
14                 if (ch == '1')
15                 {
16                     c++;
17                 }
18             }
19             Console.WriteLine(c);
20             Console.ReadKey();
21         }
22     }
23 }

 


9.
有一個無限長的數字序列1,2,2,3,3,3,4,4,4,4,5,5,5,5,5。。。(數字序列從1開始遞增,且數字k在該序列中正好出現k次),求第n項是多少

輸入描述:

輸入爲一個整數n

輸出描述:

輸出一個整數,即第n項的值
示例1

輸入

4

輸出

3

 1  public static void Main(string[] args)
 2         {
 3              int n = Convert.ToInt32(Console.ReadLine());
 4              int i=0;
 5              for(;;i++)
 6              {
 7                   if(n<=i*(i+1)/2&&n>(i-1)*i/2)
 8                   {
 9                      Console.WriteLine(i);
10                       break;
11                    }
12              }
13         }

 

10.
相關文章
相關標籤/搜索