一道加法题
来源:互联网 发布:sql server 编程 编辑:程序博客网 时间:2024/05/28 20:19
一道加法题
题目描述:
给你两个正整数d和n,计算[d/1]+[d/2]+...+[d/n]的结果,
其中d,n<=2e9,[x]表示x的值需要四舍五入到整数
如9/5就是2,20/7就是3
输入:
多组测试数据,每组仅一行为d和n
当d=n=0时结束程序
输出:
计算并输出结果
样例输入:
1 1
1 25
15 27
0 0
样例输出:
1
2
63
程序实现代码如下:
#include<iostream>
#include<fstream>
using namespace std;
int fnsum(const int d,const int n)
{
int sum=d,integer,remainder;
for(int i=2;i<=n;i++)
{
if(2*d<i)
break;
else
{
integer=d/i;
remainder=d%i;
if(2*remainder<i)
sum+=integer;
else
sum+=integer+1;
}
}
return sum;
}
int main()
{
int D,N;
ifstream filein("input.txt");
while(1)
{
filein>>D>>N;
if(D==0&&N==0)
{
break;
}
else
{
int result=fnsum(D,N);
cout<<result<<endl;
}
}
return EXIT_SUCCESS;
}
- 一道加法题
- 一道java入门题——多项式加法给我的启发
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一道题
- 一些英语鼓励句子
- SecureCRT在locale是中文情况下的courier new显示不正常
- 如何让团队增加利润
- 去掉电脑开机时跳出的“用户名 密码”框
- spring security 添加SwitchUser配置
- 一道加法题
- JSP彩色验证码
- t00ls提供Mysql sha1彩虹表(灰常强大)
- 转:ADS开发嵌入式的一些问题
- 读《Simulating Physics with Computers》
- PE型感染病毒 —— 增加节点修改PE入口 (3)
- Ubuntu 8.04增加Windows中文字体
- 访问chrome扩展中心
- GSM通信系统概述