本文主要是介绍UVa10494不知错哪,求赐教,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错解
#include<iostream>
#include<cstring>
using namespace std;
char b[1000010],d[1000010];
int main()
{
freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);
int a,i,j;
char sign;
while(cin>>b>>sign>>a)
{
int k=strlen(b);
//int kk=0;
if(sign=='/')
{
int c=0;
long long temp=0;
for(i=0;i<k;i++)
{
c=(b[i]-'0')+c*10;
if(c>=a)
{
temp=c/a+temp*10;
d[i]=c/a+'0';
c=c%a;
}
else
d[i]='0';
}
//cout<<temp<<endl;
i=0;
while(d[i]=='0' && i<k-1)i++;
for(;i<k;i++)
cout<<d[i];
cout<<endl;
}
else
{
long long temp=0;
int c=0;
for(i=0;i<k;i++)
{
//c=b[i]-'0';
// temp=(c+temp*10)%a;
temp = b[i] - '0' + c*10;
c = temp%;
}
cout<<c<<endl;
}
}
return 0;
}
正解
#include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 10000
int x[MAX];
string Div(string str, int y)
{
int i;
long long rem;
stringstream s;
string ans;
memset(x, 0, sizeof(x));
for(i = 0; i < str.size(); i++)
{
x[i] = str[i]-'0';
}
rem = 0;
for(i = 0; i < str.size(); i++){
long long temp;
temp = rem*10 + x[i];
rem = temp%y;
x[i] = temp/y;
}
for(i = 0; i < str.size(); i++)
{
if(x[i] != 0)
break;
}
for(int j = i; j < str.size(); j++)
{
s << x[j];
}
s >> ans;
if(ans.empty())
ans = "0";
return ans;
}
string Mod(string str, int y){
long long rem;
stringstream s;
string ans;
rem = 0;
for(int i = 0; i < str.size(); i++){
long long temp;
temp = str[i] - '0' + rem*10;
rem = temp%y;
}
s << rem;
s >> ans;
return ans;
}
int main()
{
freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);
string x, oper;
int y;
while(cin >> x >> oper >> y)
{
if(oper == "/")
cout << Div(x, y) << endl;
else
cout << Mod(x, y) << endl;
}
return 0;
}
这篇关于UVa10494不知错哪,求赐教的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!