#include <iostream> #include <iomanip> using namespace std;
int main(){ long long v[2] ={12345ll,678987654321ll}; long long quotient[2] = {v[0]/7,(v0 % 7 * 1000000000000ll+v[1])/7}; long long remain =v[0] %7 + v[1] %7; cout<<"quotient"<<quotient[0]<<setw(12)<<setfill('0')<<quotient[1]<<endl; cout<<"remain="<<remain<<endl; return 0; }
#include <iostream>
using namespace std;
int main()
{
int a=123456789;
int b=123456789;
for(int i=1;i<=7;i++)
{
if(b%7!=0)
{
b=b-i;
}
else
goto f;
}
f:
int d=b/7;
int n=100000000*(a-b)+87654321;
int m=n;
for(int y=1;y<=7;y++)
{
if(n%7!=0)
n=n-y;
else
goto ff;
}
ff:
int e=n/7;
int g=m-n;
cout<<"12345678987654321除以7的商为:"<<d<<e<<endl;
double h=g%7;
cout<<"12345678987654321除以7的余数为:"<<h<<endl;
return 0;
}
/符号的赋值有点乱,这个你自己搞掂了,这个简单易懂,就是麻烦点。/
#9
回复第一位回答我的。首先谢谢哈。问下您 是用什么开发工具呢 我有的是vc++6.0 .在vc 中编译头问题 有错误啊。long long定义是错误的额。C:\Users\Administrator\Desktop\编程文件\dasfjl.cpp(6) : error C2632: 'long' followed by 'long' is illegal
怎么回事?
#10
首先谢谢哈。问下您 是用什么开发工具呢 我有的是vc++6.0 .在vc 中编译头问题 有错误啊。long long定义是错误的额。C:\Users\Administrator\Desktop\编程文件\dasfjl.cpp(6) : error C2632: 'long' followed by 'long' is illegal
怎么回事?
#11
我也是的。你大几呢
#12
++
没注意
long long可以表示 12345678987654321
这个数不大。
#13
#include <iostream> #include <string> using namespace std; inline int compare(string str1,string str2) {//相等返回0,大于返回1,小于返回-1 if (str1.size()>str2.size()) return 1; //长度长的整数大于长度小的整数 else if (str1.size()<str2.size()) return -1; else return str1.compare(str2); //若长度相等,则头到尾按位比较 } string SUB_INT(string str1,string str2); string ADD_INT(string str1,string str2) {//高精度加法 int sign=1; //sign 为符号位 string str; if (str1[0]=='-') { if (str2[0]=='-') { sign=-1; str=ADD_INT(str1.erase(0,1),str2.erase(0,1)); } else { str=SUB_INT(str2,str1.erase(0,1)); } } else { if (str2[0]=='-') { str=SUB_INT(str1,str2.erase(0,1)); } else { //把两个整数对齐,短整数前面加0补齐 string::size_type L1,L2; int i; L1=str1.size(); L2=str2.size(); if (L1<L2) { for (i=1;i<=L2-L1;i++) str1="0"+str1; } else { for (i=1;i<=L1-L2;i++) str2="0"+str2; } int int1=0,int2=0; //int2 记录进位 for (i=str1.size()-1;i>=0;i--) { int1=(int(str1[i])-'0'+int(str2[i])-'0'+int2)%10; int2=(int(str1[i])-'0'+int(str2[i])-'0'+int2)/10; str=char(int1+'0')+str; } if (int2!=0) str=char(int2+'0')+str; } } //运算后处理符号位 if ((sign==-1)&&(str[0]!='0')) str="-"+str; return str; } string SUB_INT(string str1,string str2) {//高精度减法 int sign=1; //sign 为符号位 string str; int i,j; if (str2[0]=='-') { str=ADD_INT(str1,str2.erase(0,1)); } else { int res=compare(str1,str2); if (res==0) return "0"; if (res<0) { sign=-1; string temp =str1; str1=str2; str2=temp; } string::size_type tempint; tempint=str1.size()-str2.size(); for (i=str2.size()-1;i>=0;i--) { if (str1[i+tempint]<str2[i]) { j=1; while (1) {//zhao4zhong1添加 if (str1[i+tempint-j]=='0') { str1[i+tempint-j]='9'; j++; } else { str1[i+tempint-j]=char(int(str1[i+tempint-j])-1); break; } } str=char(str1[i+tempint]-str2[i]+':')+str; } else { str=char(str1[i+tempint]-str2[i]+'0')+str; } } for (i=tempint-1;i>=0;i--) str=str1[i]+str; } //去除结果中多余的前导0 str.erase(0,str.find_first_not_of('0')); if (str.empty()) str="0"; if ((sign==-1) && (str[0]!='0')) str ="-"+str; return str; } string MUL_INT(string str1,string str2) {//高精度乘法 int sign=1; //sign 为符号位 string str; if (str1[0]=='-') { sign*=-1; str1 =str1.erase(0,1); } if (str2[0]=='-') { sign*=-1; str2 =str2.erase(0,1); } int i,j; string::size_type L1,L2; L1=str1.size(); L2=str2.size(); for (i=L2-1;i>=0;i--) { //模拟手工乘法竖式 string tempstr; int int1=0,int2=0,int3=int(str2[i])-'0'; if (int3!=0) { for (j=1;j<=(int)(L2-1-i);j++) tempstr="0"+tempstr; for (j=L1-1;j>=0;j--) { int1=(int3*(int(str1[j])-'0')+int2)%10; int2=(int3*(int(str1[j])-'0')+int2)/10; tempstr=char(int1+'0')+tempstr; } if (int2!=0) tempstr=char(int2+'0')+tempstr; } str=ADD_INT(str,tempstr); } //去除结果中的前导0 str.erase(0,str.find_first_not_of('0')); if (str.empty()) str="0"; if ((sign==-1) && (str[0]!='0')) str="-"+str; return str; } string DIVIDE_INT(string str1,string str2,int flag) {//高精度除法。flag==1时,返回商; flag==0时,返回余数 string quotient,residue; //定义商和余数 int sign1=1,sign2=1; if (str2 == "0") { //判断除数是否为0 quotient= "ERROR!"; residue = "ERROR!"; if (flag==1) return quotient; else return residue ; } if (str1=="0") { //判断被除数是否为0 quotient="0"; residue ="0"; } if (str1[0]=='-') { str1 = str1.erase(0,1); sign1 *= -1; sign2 = -1; } if (str2[0]=='-') { str2 = str2.erase(0,1); sign1 *= -1; } int res=compare(str1,str2); if (res<0) { quotient="0"; residue =str1; } else if (res == 0) { quotient="1"; residue ="0"; } else { string::size_type L1,L2; L1=str1.size(); L2=str2.size(); string tempstr; tempstr.append(str1,0,L2-1); for (int i=L2-1;i<L1;i++) { //模拟手工除法竖式 tempstr=tempstr+str1[i]; tempstr.erase(0,tempstr.find_first_not_of('0'));//zhao4zhong1添加 if (tempstr.empty()) tempstr="0";//zhao4zhong1添加 for (char ch='9';ch>='0';ch--) { //试商 string str; str=str+ch; if (compare(MUL_INT(str2,str),tempstr)<=0) { quotient=quotient+ch; tempstr =SUB_INT(tempstr,MUL_INT(str2,str)); break; } } } residue=tempstr; } //去除结果中的前导0 quotient.erase(0,quotient.find_first_not_of('0')); if (quotient.empty()) quotient="0"; if ((sign1==-1)&&(quotient[0]!='0')) quotient="-"+quotient; if ((sign2==-1)&&(residue [0]!='0')) residue ="-"+residue ; if (flag==1) return quotient; else return residue ; } string DIV_INT(string str1,string str2) {//高精度除法,返回商 return DIVIDE_INT(str1,str2,1); } string MOD_INT(string str1,string str2) {//高精度除法,返回余数 return DIVIDE_INT(str1,str2,0); } int main() { char ch; string s1,s2,res;
while (cin>>s1>>ch>>s2) { switch (ch) { case '+':res=ADD_INT(s1,s2);break; case '-':res=SUB_INT(s1,s2);break; case '*':res=MUL_INT(s1,s2);break; case '/':res=DIV_INT(s1,s2);break; case '%':res=MOD_INT(s1,s2);break; default : break; } cout<<res<<endl; } return(0); }
#14
VS2008 == VC9 ,VS2010 = VC10
VC9,VC10 都可以
VC6 long long 用 __int64 替换,代码请看7楼;
PS:
12345678987654321这个数不大,可以直接用 __int64 表示,输出格式是%I64d;
是我误以为,这个数超过64 Bits 了。
常量也有类型。
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.
To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.
To specify a decimal constant, begin the specification with a nonzero digit. For example:
int i = 157; // Decimal constant
int j = 0198; // Not a decimal number; erroneous octal constant
int k = 0365; // Leading zero specifies octal constant, not decimal
To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:
int i = 0377; // Octal constant
int j = 0397; // Error: 9 is not an octal digit
To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:
int i = 0x3fff; // Hexadecimal constant
int j = 0X3FFF; // Equal to i
To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:
unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value
#include <iostream> #include <iomanip> using namespace std;
int main(){ long long v[2] ={12345ll,678987654321ll}; long long quotient[2] = {v[0]/7,(v0 % 7 * 1000000000000ll+v[1])/7}; long long remain =v[0] %7 + v[1] %7; cout<<"quotient"<<quotient[0]<<setw(12)<<setfill('0')<<quotient[1]<<endl; cout<<"remain="<<remain<<endl; return 0; }
#include <iostream> #include <iomanip> using namespace std;
int main(){ long long v[2] ={12345ll,678987654321ll}; long long quotient[2] = {v[0]/7,(v0 % 7 * 1000000000000ll+v[1])/7}; long long remain =v[0] %7 + v[1] %7; cout<<"quotient"<<quotient[0]<<setw(12)<<setfill('0')<<quotient[1]<<endl; cout<<"remain="<<remain<<endl; return 0; }
首先谢谢哈。问下您 是用什么开发工具呢 我有的是vc++6.0 .在vc 中编译头问题 有错误啊。long long定义是错误的额。C:\Users\Administrator\Desktop\编程文件\dasfjl.cpp(6) : error C2632: 'long' followed by 'long' is illegal
怎么回事?
#include <iostream> #include <string> using namespace std; inline int compare(string str1,string str2) {//相等返回0,大于返回1,小于返回-1 if (str1.size()>str2.size()) return 1; //长度长的整数大于长度小的整数 else if (str1.size()<str2.size()) return -1; else return str1.compare(str2); //若长度相等,则头到尾按位比较 } string SUB_INT(string str1,string str2); string ADD_INT(string str1,string str2) {//高精度加法 int sign=1; //sign 为符号位 string str; if (str1[0]=='-') { if (str2[0]=='-') { sign=-1; str=ADD_INT(str1.erase(0,1),str2.erase(0,1)); } else { str=SUB_INT(str2,str1.erase(0,1)); } } else { if (str2[0]=='-') { str=SUB_INT(str1,str2.erase(0,1)); } else { //把两个整数对齐,短整数前面加0补齐 string::size_type L1,L2; int i; L1=str1.size(); L2=str2.size(); if (L1<L2) { for (i=1;i<=L2-L1;i++) str1="0"+str1; } else { for (i=1;i<=L1-L2;i++) str2="0"+str2; } int int1=0,int2=0; //int2 记录进位 for (i=str1.size()-1;i>=0;i--) { int1=(int(str1[i])-'0'+int(str2[i])-'0'+int2)%10; int2=(int(str1[i])-'0'+int(str2[i])-'0'+int2)/10; str=char(int1+'0')+str; } if (int2!=0) str=char(int2+'0')+str; } } //运算后处理符号位 if ((sign==-1)&&(str[0]!='0')) str="-"+str; return str; } string SUB_INT(string str1,string str2) {//高精度减法 int sign=1; //sign 为符号位 string str; int i,j; if (str2[0]=='-') { str=ADD_INT(str1,str2.erase(0,1)); } else { int res=compare(str1,str2); if (res==0) return "0"; if (res<0) { sign=-1; string temp =str1; str1=str2; str2=temp; } string::size_type tempint; tempint=str1.size()-str2.size(); for (i=str2.size()-1;i>=0;i--) { if (str1[i+tempint]<str2[i]) { j=1; while (1) {//zhao4zhong1添加 if (str1[i+tempint-j]=='0') { str1[i+tempint-j]='9'; j++; } else { str1[i+tempint-j]=char(int(str1[i+tempint-j])-1); break; } } str=char(str1[i+tempint]-str2[i]+':')+str; } else { str=char(str1[i+tempint]-str2[i]+'0')+str; } } for (i=tempint-1;i>=0;i--) str=str1[i]+str; } //去除结果中多余的前导0 str.erase(0,str.find_first_not_of('0')); if (str.empty()) str="0"; if ((sign==-1) && (str[0]!='0')) str ="-"+str; return str; } string MUL_INT(string str1,string str2) {//高精度乘法 int sign=1; //sign 为符号位 string str; if (str1[0]=='-') { sign*=-1; str1 =str1.erase(0,1); } if (str2[0]=='-') { sign*=-1; str2 =str2.erase(0,1); } int i,j; string::size_type L1,L2; L1=str1.size(); L2=str2.size(); for (i=L2-1;i>=0;i--) { //模拟手工乘法竖式 string tempstr; int int1=0,int2=0,int3=int(str2[i])-'0'; if (int3!=0) { for (j=1;j<=(int)(L2-1-i);j++) tempstr="0"+tempstr; for (j=L1-1;j>=0;j--) { int1=(int3*(int(str1[j])-'0')+int2)%10; int2=(int3*(int(str1[j])-'0')+int2)/10; tempstr=char(int1+'0')+tempstr; } if (int2!=0) tempstr=char(int2+'0')+tempstr; } str=ADD_INT(str,tempstr); } //去除结果中的前导0 str.erase(0,str.find_first_not_of('0')); if (str.empty()) str="0"; if ((sign==-1) && (str[0]!='0')) str="-"+str; return str; } string DIVIDE_INT(string str1,string str2,int flag) {//高精度除法。flag==1时,返回商; flag==0时,返回余数 string quotient,residue; //定义商和余数 int sign1=1,sign2=1; if (str2 == "0") { //判断除数是否为0 quotient= "ERROR!"; residue = "ERROR!"; if (flag==1) return quotient; else return residue ; } if (str1=="0") { //判断被除数是否为0 quotient="0"; residue ="0"; } if (str1[0]=='-') { str1 = str1.erase(0,1); sign1 *= -1; sign2 = -1; } if (str2[0]=='-') { str2 = str2.erase(0,1); sign1 *= -1; } int res=compare(str1,str2); if (res<0) { quotient="0"; residue =str1; } else if (res == 0) { quotient="1"; residue ="0"; } else { string::size_type L1,L2; L1=str1.size(); L2=str2.size(); string tempstr; tempstr.append(str1,0,L2-1); for (int i=L2-1;i<L1;i++) { //模拟手工除法竖式 tempstr=tempstr+str1[i]; tempstr.erase(0,tempstr.find_first_not_of('0'));//zhao4zhong1添加 if (tempstr.empty()) tempstr="0";//zhao4zhong1添加 for (char ch='9';ch>='0';ch--) { //试商 string str; str=str+ch; if (compare(MUL_INT(str2,str),tempstr)<=0) { quotient=quotient+ch; tempstr =SUB_INT(tempstr,MUL_INT(str2,str)); break; } } } residue=tempstr; } //去除结果中的前导0 quotient.erase(0,quotient.find_first_not_of('0')); if (quotient.empty()) quotient="0"; if ((sign1==-1)&&(quotient[0]!='0')) quotient="-"+quotient; if ((sign2==-1)&&(residue [0]!='0')) residue ="-"+residue ; if (flag==1) return quotient; else return residue ; } string DIV_INT(string str1,string str2) {//高精度除法,返回商 return DIVIDE_INT(str1,str2,1); } string MOD_INT(string str1,string str2) {//高精度除法,返回余数 return DIVIDE_INT(str1,str2,0); } int main() { char ch; string s1,s2,res;
while (cin>>s1>>ch>>s2) { switch (ch) { case '+':res=ADD_INT(s1,s2);break; case '-':res=SUB_INT(s1,s2);break; case '*':res=MUL_INT(s1,s2);break; case '/':res=DIV_INT(s1,s2);break; case '%':res=MOD_INT(s1,s2);break; default : break; } cout<<res<<endl; } return(0); }
#include <iostream> #include <iomanip> using namespace std;
int main(){ long long v[2] ={12345ll,678987654321ll}; long long quotient[2] = {v[0]/7,(v0 % 7 * 1000000000000ll+v[1])/7}; long long remain =v[0] %7 + v[1] %7; cout<<"quotient"<<quotient[0]<<setw(12)<<setfill('0')<<quotient[1]<<endl; cout<<"remain="<<remain<<endl; return 0; }
首先谢谢哈。问下您 是用什么开发工具呢 我有的是vc++6.0 .在vc 中编译头问题 有错误啊。long long定义是错误的额。C:\Users\Administrator\Desktop\编程文件\dasfjl.cpp(6) : error C2632: 'long' followed by 'long' is illegal
怎么回事?
VS2008 == VC9 ,VS2010 = VC10
VC9,VC10 都可以
VC6 long long 用 __int64 替换,代码请看7楼;
PS:
12345678987654321这个数不大,可以直接用 __int64 表示,输出格式是%I64d;
是我误以为,这个数超过64 Bits 了。
常量也有类型。
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.
To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.
To specify a decimal constant, begin the specification with a nonzero digit. For example:
int i = 157; // Decimal constant
int j = 0198; // Not a decimal number; erroneous octal constant
int k = 0365; // Leading zero specifies octal constant, not decimal
To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:
int i = 0377; // Octal constant
int j = 0397; // Error: 9 is not an octal digit
To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:
int i = 0x3fff; // Hexadecimal constant
int j = 0X3FFF; // Equal to i
To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:
unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value