2 条题解

  • 0
    @ 2025-10-13 19:14:59

    C(新答案):

    #include<stdio.h>
    #include<stdlib.h> //stdlib.h头文件提供绝对值函数abs()
    int main() {
    	int n, x, y, ans, t;
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++) {
    		scanf("%d%d", &x, &y);
    		ans = 0;
    		if(x > y) {
    			t = x;
    			x = y;
    			y = t;
    		}
    		for(int j = x + 1; j < y; j++) {
    			if(abs(j % 2) == 1) ans += j;//负奇数 % 2 等于 -1,正奇数 % 2 等于 1,在此运用abs()函数可把两种情况归类为一种。只要能将两种情况都判断到,无论什么方法都正确。
    		}
    		printf("%d\n", ans);
    	}
    	return 0;
    } 
    
    • 0
      @ 2024-12-18 14:07:26

      C :

      #include<stdio.h>
      
      int main()
      {
      	int n,x,y,i,j,t,s;
      	scanf("%d",&n);
      	for(i=1;i<=n;s=0,i++)
      	{
      		
      		scanf("%d %d",&x,&y);
      		if(x>y)
      		{
      			t=y;
      			y=x;
      			x=t;
      		}
      		for(j=x+1;j<y;j++)
      		{
      			
      			if(j%2!=0)
      			{
      				s+=j;
      			}
      		}
      		printf("%d\n",s);
      	}
      	
      	
      	return 0;
      }
      

      C++ :

      #include<stdio.h>
      int main()
      {
       int n,a,b,i,c,t,y;
       scanf("%d",&n);
       for(i=1;i<=n;i++)
       {
            y=0;
            scanf("%d %d",&a,&b);
            if(a>=b)
            t=a,a=b,b=t;
            if(a%2==0)
            {
             for(c=a+1;c<b;c+=2)
            y=y+c;
             printf("%d\n",y);
            }
            else
            {
             for(c=a+2;c<b;c+=2)
            y=y+c;
             printf("%d\n",y);
            }
           
       }
       return 0;
      }
      
      • 1

      信息

      ID
      621
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      提交数
      144
      已通过
      30
      上传者