首页 > 其他分享 >(UVA) The ? 1 ? 2 ? ... ? n = k problem

(UVA) The ? 1 ? 2 ? ... ? n = k problem

时间:2023-04-20 21:32:21浏览次数:34  
标签:... int sum long ii printf UVA problem


The ? 1 ? 2 ? ... ? n = k problem 
 



The problem
Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k
For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12
with n = 7

The Input
The first line is the number of test cases, followed by a blank line.
Each test case of the input contains integer k (0<=|k|<=1000000000).
Each test case will be separated by a single line.
The Output
For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.
Print a blank line between the outputs for two consecutive test cases.
Sample Input

2

12

-3646397

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    int T;
    scanf("%d",&T);
    long long int n;
    while(T--)
    {
        long long int sum = 0;
        scanf("%lld",&n);
        if(n<0)
        {
            n = -n;
        }
        int ii  = 0;
        for(int i=1;;i++)
        {
            sum += i;
            if(sum >= n)
            {
                ii = i;
                break;
            }
        }
        if((sum - n)%2 == 0)
        {
            if(T>0)
            {
                printf("%d\n\n",ii);
            }
            else
            {
                printf("%d\n",ii);
            }
            continue;
        }
        for(int j=ii+1;;j++)
        {
            sum += j;
            if((sum-n)%2 == 0)
            {
                if(T>0)
                {
                    printf("%d\n\n",j);
                }
                else
                {
                    printf("%d\n",j);
                }
                break;
            }
        }
    }
    return 0;
}

标签:...,int,sum,long,ii,printf,UVA,problem
From: https://blog.51cto.com/u_14834528/6210681

相关文章

  • 【调试】Valgrind内存泄漏内存越界|运行时间|调用|cache命中率|多线程竞态|堆栈分析..
    目录即看即用详细简介Valgrind工具详解安装使用检测内存泄漏其他内存问题memcheck工具的常用选型其他选项附录其他类似工具实例分析:03.使用未初始化的内存04.使用野指针05.动态内存越界访问06.分配空间后没有释放07.不匹配使用delete或者free08.两次......
  • org.pentaho.di.core.exception.KettleXMLException: Unexpected problem reading sh
    【kettle】【报错】UnexpectedproblemreadingsharedobjectsfromXMLfile当读共享文件时发生错误UnexpectedproblemreadingsharedobjectsfromXMLfile:null当读共享文件时发生错误kettle新建转换时,“读取共享对象时发生一个严重错误”“Unexpectedproblemre......
  • A*B Problem 485 (数学题+九余数定理)
    A*BProblem1000ms |          内存限制:655352设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数相加,直到结果是个一位数k。例如:6*8=48;4+8=12;1+2=3;输出3即可。第一行输入一个数N(0<N<=1000000),表示N组测试数据。......
  • JS中三个点(...)是什么?
    我们在看js代码时经常会出现(...)三个点的东西,它究竟是什么意思?又有何用处?下面我就给大家分享一下三个点的那些事什么意思?三个点(...)真名叫扩展运算符,是在ES6中新增加的内容,它可以在函数调用/数组构造时,将数组表达式或者string在语法层面展开;还可以在构造字面量对象时将对象表达式......
  • How Many O's? UVA - 11038
    写下区间[a,b]的所有数 ,问一共有多少个0 #include<iostream>#include<cstring>#include<vector>usingnamespacestd;#defineintlonglongintn,f[40][40][2][2];vector<int>a;intdfs(intx,intcnt0,intflg,intlead){ if(x<0){ i......
  • Investigating Div-Sum Property UVA - 11361
     定问在[A,B]中,有多少个整数本身能被m整除,各个数位上数字之和也能被m整除?  #include<iostream>#include<cstring>#include<vector>usingnamespacestd;vector<int>a;intm,f[40][105][105][2];intdfs(intx,intv1,intv2,intflg){ if(x<0) retur......
  • hdoj The sum problem 2058 (数学等差公式&技巧转换)
    ThesumproblemTimeLimit:5000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):21416    AcceptedSubmission(s):6287ProblemDescriptionGivenasequence1,2,3,......N,yourjob......
  • 19c环境,运行DBCA创建CDB时,报错ORA-01519: error while processing file:?/rdbms/admin
    1、同事新搭建的一套19CRAC,补丁为19.10,运行DBCA安装CDB数据库时报错,错误日志如下所示:ORA-01519:errorwhileprocessingfile:?/rdbms/admin/dcore.bsq.....ORA-00604:erroroccurredatrecursiveSQLlevel1ORA-01119:errorincreatingdatabasefile'+DATA01/CDB1/pdb......
  • UVA11806 Cheerleaders
    你有一个n×m的网格图,现在你要将K个人放在网格中,满足一下条件:网格图的四个边都至少有一个人。每个格子上不能有两个人。每个人必须都有位置。注意:四个角的人可以同时算作在两个边上  容斥原理   J=0时就是allAnswer#include<iostream>#include<cstri......
  • 喝点鸡汤...
    Now,Iwanttotakethisopportunitytogiveyousomeadvice.Overthecourseofyourlife,youwillfindthatthingsarenotalwaysfair.Youwillfindthatthingshappentoyouthatyoudonotdeserveandthatarenotalwayswarranted.Butyouhavet......