6.表達式求值c++
1+2*3=7 1*(2+3)=5 1*2*3=6 (1+2)*3=9
一行三個數a,b,c (1 <= a, b, c <= 10)
可以得到的最大值
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,(1<n<=3000)1
第二行爲n個整數a
,a2
,...,an
,其中(2<=ai
<=10^5)
輸出僅包含一行,輸出f(m)的最大值
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」
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.
輸入爲一個整數n
輸出一個整數,即第n項的值
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.