PKU Online Judge 1131 为什么总是WA,希望高手帮忙看看这个程序哪里有问题~
题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1131解决的话我会再酌情加分~谢谢~#include<stdio.h...
题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1131
解决的话我会再酌情加分~谢谢~
#include <stdio.h>
#include <string.h>
char * div(char ptr[]) //返回字符串除以8后的结果
{
char * p;
char ans[1000];
int i=0;
p=ptr;
int s=(*p)-'0';
p++;
while ((*p)!='\0')
{
int k=(*p)-'0'+s*10;
ans[i++]=k/8+'0';
s=k%8;
p++;
}
ans[i]='\0';
return ans;
}
int main()
{
char origin[1000];
char * ptr;
while (scanf("%s", origin)!=EOF && origin[0]!='\0')
{
int l=strlen(origin);
char begin[1000];
char middle[1000]="\0";
ptr=origin;
ptr+=l;
ptr--;
int flag=0;
while ((*ptr)!='.') //从右到左读取字符
{
begin[0]=*ptr;
begin[1]='\0';
strcat(begin, middle);
strcat(begin, "000"); //给字符串后添上足够多的零便于除以8
strcpy(middle, div(begin));
ptr--;
flag=0;
}
ptr=middle;
while ((*ptr)!='\0')
ptr++;
ptr--;
while ((*ptr)=='0')
ptr--;
ptr++;
*ptr='\0'; //去除末尾多余的零
if (strcmp(middle,"\0")==0)
{
printf("%s [8] = 0 [10]\n", origin);
continue;
}
printf("%s [8] = 0.%s [10]\n", origin, middle);
}
return 0;
} 展开
解决的话我会再酌情加分~谢谢~
#include <stdio.h>
#include <string.h>
char * div(char ptr[]) //返回字符串除以8后的结果
{
char * p;
char ans[1000];
int i=0;
p=ptr;
int s=(*p)-'0';
p++;
while ((*p)!='\0')
{
int k=(*p)-'0'+s*10;
ans[i++]=k/8+'0';
s=k%8;
p++;
}
ans[i]='\0';
return ans;
}
int main()
{
char origin[1000];
char * ptr;
while (scanf("%s", origin)!=EOF && origin[0]!='\0')
{
int l=strlen(origin);
char begin[1000];
char middle[1000]="\0";
ptr=origin;
ptr+=l;
ptr--;
int flag=0;
while ((*ptr)!='.') //从右到左读取字符
{
begin[0]=*ptr;
begin[1]='\0';
strcat(begin, middle);
strcat(begin, "000"); //给字符串后添上足够多的零便于除以8
strcpy(middle, div(begin));
ptr--;
flag=0;
}
ptr=middle;
while ((*ptr)!='\0')
ptr++;
ptr--;
while ((*ptr)=='0')
ptr--;
ptr++;
*ptr='\0'; //去除末尾多余的零
if (strcmp(middle,"\0")==0)
{
printf("%s [8] = 0 [10]\n", origin);
continue;
}
printf("%s [8] = 0.%s [10]\n", origin, middle);
}
return 0;
} 展开
2个回答
展开全部
我自己以前做的,你参考一下,试一下看看答案是否和你一样,以便你找出错误,我都忘了.
可以通过的.
#include <iostream>
using namespace std;
struct number {
int dig[10000];
int maxlen;
}A,B,C;
char input[10000];
void divide(number &a,int m) {
int i,p;
for(i=0;i<=a.maxlen;i++) {
p=a.dig[i]%m;
a.dig[i]=a.dig[i]/m;
a.dig[i+1]+=p*10000;
if(i==a.maxlen && p!=0)
a.maxlen++;
}
}
void mul_add(number &a,number b,int m) {
int i;
a.maxlen=b.maxlen;
for(i=a.maxlen;i>=1;i--) {
a.dig[i]+=b.dig[i]*m;
while(a.dig[i]>=10000) {
a.dig[i]-=10000;
a.dig[i-1]++;
}
if(a.dig[a.maxlen]==0)
a.maxlen--;
}
}
void print(number a) {
if(a.maxlen==0) {
printf("%d",a.dig[0]);
return ;
}
int i;
printf("%d.",a.dig[0]);
for(i=1;i<a.maxlen;i++)
printf("%04d",a.dig[i]);
i=a.dig[a.maxlen];
if(i%1000==0)
printf("%d",i/1000);
else if(i%100==0)
printf("%02d",i/100);
else if(i%10==0)
printf("%03d",i/10);
else
printf("%04d",i);
}
int main() {
int i;
while(scanf("%s",input)!=EOF) {
memset(&A,0,sizeof(A));
memset(&B,0,sizeof(B));
A.dig[0]=1;
A.maxlen=0;
B.dig[0]=input[0]-'0';
if(input[1]==0) {
printf("%c [8] = %c [10]\n",input[0],input[0]);
continue;
}
for(i=2;input[i]!=0;i++) {
divide(A,8);
mul_add(B,A,input[i]-'0');
}
printf("%s [8] = ",input);
print(B);
printf(" [10]\n");
}
return 0;
}
可以通过的.
#include <iostream>
using namespace std;
struct number {
int dig[10000];
int maxlen;
}A,B,C;
char input[10000];
void divide(number &a,int m) {
int i,p;
for(i=0;i<=a.maxlen;i++) {
p=a.dig[i]%m;
a.dig[i]=a.dig[i]/m;
a.dig[i+1]+=p*10000;
if(i==a.maxlen && p!=0)
a.maxlen++;
}
}
void mul_add(number &a,number b,int m) {
int i;
a.maxlen=b.maxlen;
for(i=a.maxlen;i>=1;i--) {
a.dig[i]+=b.dig[i]*m;
while(a.dig[i]>=10000) {
a.dig[i]-=10000;
a.dig[i-1]++;
}
if(a.dig[a.maxlen]==0)
a.maxlen--;
}
}
void print(number a) {
if(a.maxlen==0) {
printf("%d",a.dig[0]);
return ;
}
int i;
printf("%d.",a.dig[0]);
for(i=1;i<a.maxlen;i++)
printf("%04d",a.dig[i]);
i=a.dig[a.maxlen];
if(i%1000==0)
printf("%d",i/1000);
else if(i%100==0)
printf("%02d",i/100);
else if(i%10==0)
printf("%03d",i/10);
else
printf("%04d",i);
}
int main() {
int i;
while(scanf("%s",input)!=EOF) {
memset(&A,0,sizeof(A));
memset(&B,0,sizeof(B));
A.dig[0]=1;
A.maxlen=0;
B.dig[0]=input[0]-'0';
if(input[1]==0) {
printf("%c [8] = %c [10]\n",input[0],input[0]);
continue;
}
for(i=2;input[i]!=0;i++) {
divide(A,8);
mul_add(B,A,input[i]-'0');
}
printf("%s [8] = ",input);
print(B);
printf(" [10]\n");
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
易捷测试
2024-10-23 广告
2024-10-23 广告
load pull的简单原理是使用一个叫tuner的东西,可理论模拟任意阻抗的负载,(你可以把tuner想像成一个一段并联传输线和一段串联传输线的组合器件,而传输线的长度和阻抗都是可调的,那么通过调节即可遍历smith原图的任意位置)实际测...
点击进入详情页
本回答由易捷测试提供
2009-03-03
展开全部
用这个吧:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string str1, r;
int x, y, m;
vector <int> a;
while (cin >> str1){
str1 = str1.substr(2);
a.clear();
for (int i = str1.size() - 1; i >= 0; --i){
m = 0; x = str1[i] - 48;
for (int j = 0; j != a.size(); ++j){
y = x * 10 + a[j];
a[j] = y / 8;
x = y % 8;
}
while (x != 0){
y = x * 10;
a.push_back(y / 8);
x = y % 8;
}
}
cout < < "0." < < str1 < < " [8] = 0.";
for (int i = 0; i != a.size(); ++i) cout < < a[i];
cout < < " [10]" < < endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string str1, r;
int x, y, m;
vector <int> a;
while (cin >> str1){
str1 = str1.substr(2);
a.clear();
for (int i = str1.size() - 1; i >= 0; --i){
m = 0; x = str1[i] - 48;
for (int j = 0; j != a.size(); ++j){
y = x * 10 + a[j];
a[j] = y / 8;
x = y % 8;
}
while (x != 0){
y = x * 10;
a.push_back(y / 8);
x = y % 8;
}
}
cout < < "0." < < str1 < < " [8] = 0.";
for (int i = 0; i != a.size(); ++i) cout < < a[i];
cout < < " [10]" < < endl;
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询