📄 sum.cpp
字号:
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
int mystrcmp(const char *ch1,const char *ch2){
int l=strlen(ch1)>strlen(ch2)?1:2;
string str = "";
if(l==1){
//在第二个参数前面补零
int len = strlen(ch1);
for(int k = 0;k < strlen(ch1)-strlen(ch2);k++){
str=str+"0";
}
str=str+ch2;
return strcmp(ch1,str.c_str());
}else{
int len = strlen(ch2);
for(int k = 0;k < strlen(ch2)-strlen(ch1);k++){
str=str+"0";
}
str=str+ch1;
return strcmp(str.c_str(),ch2);
}
}
string strdiv(string str,long int i){
string tmp = "";
char buf[2];
int t = 0;
int s = 0;
char ch;
const char * p = str.c_str();
for(int k = 0;k < str.size();k++){
ch = *p++;
t = atoi(&ch);
tmp = tmp+itoa((t+s*10)/i,buf,10);
s = (t+s*10)%i;
}
//把左端的零去掉
p = tmp.c_str();
for(int j =0;j < tmp.size();j++){
ch = *p++;
if(ch!='0'){
return &tmp[j];
}
}
return "0";
}
int strmod(string str,long int i){
int tmp = 0;
int t = 0;
char ch;
const char * p = str.c_str();
for(int k = 0;k < str.size();k++){
ch = *p++;
t = atoi(&ch);
tmp = (t+tmp*10)%i;
}
return tmp;
}
void main(){
ifstream in("input.txt");
ofstream out("output.txt");
string str; //接收读入的字符串
while(getline(in,str)){
int count=1; //统计满足条件的组合个数,开始时为一
long int n = 1; //用于后面的循环判断
//如果数较小,就用long int表示
if(str.size()<10){
const char* ch = str.c_str();
long int linput = atol(ch);
long int tmp = sqrt(linput/2);
while(n<=tmp){
if(linput%(2*n)==n){
if(linput/(2*n)>n) count++;
else break;
}
if(linput%(2*n+1)==0){
if(linput/(2*n+1)>n) count++;
else break;
}
n++;
}
}
//否则用string去处理输入的数
else{
string tmp = strdiv(str,2);
char buffer[10];
while(mystrcmp(ltoa(n,buffer,10),tmp.c_str())<=0){
if(strmod(str,2*n)==n){
if(mystrcmp(strdiv(str,2*n).c_str(),ltoa(n,buffer,10))>0) count++;
else break;
}
if(strmod(str,2*n+1)==0){
if(mystrcmp(strdiv(str,2*n+1).c_str(),ltoa(n,buffer,10))>0) count++;
else break;
}
n++;
}
}
out<<count<<"\n";
cout<<count<<endl;
}
in.close();
out.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -