61 条题解

  • 51
    @ 2022-5-21 12:54:17

    输出是精髓,建议不要用数组和循环,会比较烦。上代码!

        #include <bits/stdc++.h>
        using namespace std;
        int main()
        {
            int n;
            cin >> n;
            if (n%2==1)
            {
                cout << n/2+1;//因为是从1开始,所以可以这样
            }
            else
            {
                cout << n+1;//中间值是n/2,他的后一项值为n/2+1,和为n+1
            }
            return 0;
        }
    

    日常第十四题,嗨害嗨~~~

    • 14
      @ 2023-8-12 22:00:35

      找规律基础题

      这道题与前面的题不一样,是一道找规律题,不难,只要小学数学及格就做得出来。

      • 从题目中也能看出来,这道题目的奇数和偶数应该分别讨论,这里先讨论偶数。

      步骤:

      1. 找到奇数规律。
      2. 找到偶数规律。
      3. 整合进代码。

      1.奇数规律

      • 知道题目既然是小学找规律难度,那我们就可以用小学的方法做。
      • 小学的方法一般是1. 列出3个及以上的例子2. 通过例子总结规律
      • 这道题目也一样,我们可以把几个连续的例子列出来总结规律。
      名称 col1 col2 col3 col4 col5 col6
      3 5 7 9 11 13
      中间值 2 3 4 5 6 7
      • 能够发现,在输入是奇数的情况下,中间值是 (输入+1)/2。
      • 这样,我们第一个任务就完成了。

      2.偶数规律

      • 同样使用上面的方法总结规律。
      名称 col1 col2 col3 col4 col5 col6
      2 4 6 8 10 12
      中间值之和 1+2=3 2+3=5 3+4=7 4+5=9 5+6=11 6+7=13
      • 我们同样可以据此总结出规律,在输入是偶数的情况下,中间值之和是(输入+1)。

      3. 整合进代码

      • 这一步很简单,用一个变量储存输入,把所得公式套上去即可。

        #include <bits/stdc++.h>
        using namespace std;
        int main()
        {
            int n;
            cin>>n;//输入n。
            if (n%2==1)
            {//如果是奇数。
                cout<<(n+1)/2;
                //奇数中间值公式。
            }
            else
            {//如果是偶数。
                cout<<n+1;
                //偶数中间值和公式。
            }
            return 0;
        }
        

      (求赞)

      • 12
        @ 2023-8-7 20:17:50
        #include <iostream>
        using namespace std;
        int main()
        {
            int n, sum = 0;
            cin >> n;
            if(n % 2 == 1)
            {
                sum += n / 2 + 1;
            }
            else
            {
                sum += n / 2 + n / 2 + 1;
            }
            cout << sum;
            return 0;
        }
        
        • 11
          @ 2023-8-16 21:43:27

          不会吧,这么简单 应该都会吧......(先 后看!养成习惯!)

          #include <bits/stdc++.h>
          using namespace std;
          int main()
          {
              int n;
              cin>>n;//输入n
              if (n%2==1)
              {
                  cout<<n/2+1;//奇数情况
              }
              else
              {
                  cout<<n+1;//偶数情况
              }
              return 0;
          }
          
          

          制作不易,给个赞吧,球球了...... 有什么问题,联系我,邮箱是ASheepBoy_Bed@163.com

          • 5
            @ 2022-8-3 21:15:16

            这道题的思维需要一点转弯~ 先来看奇数情况:(n + 1)/ 2 再看偶数复杂情况:n / 2 + (n + 2) / 2 来吧,思路捋清楚了就看代码↓

            #include <bits/stdc++.h>
            using namespace std;
            int main()
            {
                int n;
                cin >> n;
                if (n % 2 == 1) cout << (n + 1) / 2;
                else cout << n / 2 + (n + 2) / 2;
                return 0;
            }
            
          • 3
            @ 2023-8-5 20:10:25

            题解:

            #include <iostream>
            using namespace std;
            int main()#初始化
            {
                int a;
                cin>>a;#输入a
                if (a%2==0)
                {
                    cout<<a+1;#因a/2*2=a,所以省去了
                }
                else
                {
                    cout<<a/2+1;#输出单数的中间数(大家应该都能看懂,所以不写了)
                }
                #不写return 0;应该也可以,我就没写
            }
            
            • 1
              @ 2023-8-14 15:42:15
              #include <iostream>
              using namespace std;
              int main()
              {
                  int n;
                  cin>>n;
                  if(n%2==0)//判断奇偶性,观察规律
                  {
                      cout<<1+n;
                  }
                  else
                  {
                      cout<<(n+1)/2;
                  }
                  return 0;
              }
              
              • 0
                @ 2024-5-12 13:04:15
                #include <bits/stdc++.h>//绝对AC不然你吃我
                using namespace std;
                int main()
                {
                    int n;
                    cin>>n;
                    if (n%2==0)
                    {
                        cout<<2*(n/2)+1;
                    }
                    else
                    {
                        cout<<n/2+n%2;
                    }
                    return 0;
                }
                
                • 0
                  @ 2023-11-25 19:36:54
                  #include <iostream>
                  using namespace std;
                  int main()
                  {
                  	int n, num = 0;
                  	cin >> n;
                      if (n % 2 == 1)
                  	{
                  		n = n / 2;
                  		num = n + 1; 
                  	}
                  	else
                  	{
                  		num = n + 1;
                  	}
                  	cout << num;
                  	return 0;
                  }
                  
                  • 0
                    @ 2023-10-21 21:20:52

                    单数就是(n+1)/2 (比如说5的中间数就是(5+1)/2=3) 偶数就是n+1 (比如说6的中间数就是中间3+4=7,也就是6+1) 上代码

                    #include <bits/stdc++.h> 
                    using namespace std;
                    int main()
                    {
                        int n;
                        cin>>n;
                        if (n%2==1)
                        {
                            cout<<(n+1)/2;
                        }
                        else
                        {
                            cout<<n+1;
                        }
                        return 0;
                    }
                    
                    • 0
                      @ 2023-8-30 17:14:28
                      #include<iostream>
                      using namespace std;
                      int main()
                      {
                          int n;
                          cin>>n;
                          if(n%2==1)
                          {
                              cout<<n/2+1;
                          }
                          else
                          {
                              cout<<n/2+n/2+1;
                          }
                          return 0;
                      }```这个算简单的了
                      
                      • 0
                        @ 2023-8-30 9:28:33
                        #include <iostream>
                        using namespace std;
                        int main()
                        {
                            int q;
                            cin>>q;
                            if (q%2==0)
                            {
                                q+=1;//可从多次计算得出
                            }
                            else
                            {
                                q=q/2+1;
                            }
                            cout<<q;
                            return 0;
                        }
                        
                        • 0
                          @ 2023-8-30 9:03:09
                          #include <bits/stdc++.h>
                          using namespace std;
                          int main() 
                          {
                                   int n;
                                  cin >> n;
                                  if (n%2==1)
                                  {
                                      cout << n/2+1;
                                  }
                                  else
                                  {
                                      cout << n+1;
                                  }
                              return 0;
                          }
                          
                          • 0
                            @ 2023-8-28 10:56:50
                            #include <bits/stdc++.h>
                            using namespace std;
                            int main()
                            {
                                int n;
                                cin >> n;
                                cout << ((n % 2 == 0) ? (n / 2) + (n / 2 + 1) : n / 2 + 1);
                                return 0;
                            }
                            

                            全网首发,全网最短

                            • 0
                              @ 2023-8-28 7:01:21
                              # 一个简单的判断和二分,不需要数组,上代码!
                              #include <bits/stdc++.h>//万能开头
                              using namespace std;
                              int n;//定义变量
                              int main()
                              {
                                  cin>>n;//输入变量
                                  if(n%2==1)//判断为奇数
                                  {
                                      cout<<(1+n)/2;//二分公式
                                  }
                                  else
                                  {
                                      cout<<n+1;//n/2*2+1=n+1
                                  }
                                  return 0;
                              }
                              //一手交赞,一手交货(代码)
                              
                              
                              
                              
                              
                              
                              //核桃hetao1609095编程
                              //水印
                              
                              • 0
                                @ 2023-8-18 19:53:24

                                6

                                • 0
                                  @ 2023-8-14 18:05:40
                                  #使用Python简单一些
                                  n=int(input())#输入一个数n
                                  if n%2==1:#判断n是不是奇数
                                      print((n+1)//2)#输出中间数
                                  else:#否则(n是偶数)
                                      print(n+1)#输出中间数之和
                                  
                                  • 0
                                    @ 2023-8-11 19:53:23

                                    int n; cin >> n; if (n % 2 == 1)//如果为奇数 { cout << n / 2 + 1;//奇数的中间值是奇数除以2取整在加1 } else//如果是偶数 { cout << n / 2 + n / 2 + 1;//懂得都懂 }

                                    • 0
                                      @ 2023-8-10 20:16:59
                                      #include <iostream>
                                          using namespace std;
                                          int main()
                                          {
                                              int a;
                                              cin >> a;
                                              if(a%2==0)
                                              {
                                                  cout <<a+1;
                                              }
                                              else
                                              {
                                                  cout <<a/2+1;
                                              }
                                              return 0;
                                          }
                                      
                                      • 0
                                        @ 2023-8-7 18:29:41
                                        #include<bits/stdtr1c++.h>
                                          	using namespace std;
                                        int main()	
                                        {
                                            int i;
                                            cin>>i;
                                            if(i%2==0)
                                                cout<<i/2*2+1;
                                            else
                                                cout<<i/2+1;
                                        	return 0;		
                                        }
                                        

                                        信息

                                        ID
                                        555
                                        时间
                                        1000ms
                                        内存
                                        128MiB
                                        难度
                                        4
                                        标签
                                        递交数
                                        8804
                                        已通过
                                        4089
                                        上传者