我已经花了不少时间编写我的代码(它不起作用)。这是一个欧拉计划问题,其中给定一个非常大的和来查找,然后要求打印该和的前十位数字。 (问题可以在这里找到:https://projecteuler.net/problem=13)
我已经运行了几次“测试”,其中添加了打印命令以查看代码中不同点的不同值。当我运行代码时,我得到了从符号到应该是个位数的十位数字的任何内容。
无论如何。我的问题是:这是类型转换问题还是我的方法缺少其他一些明显的问题?我一直在研究类型转换试图找到修复方法,但无济于事。
感谢您的帮助!
代码如下:
// this is a program to find a very large sum of many very large numbers
#include <stdio.h>
#include <math.h>
int main()
{
//declare all ints needed
int i;
int j;
int d; // digit, need to add 48
int placesum; // sum of addition in _'s place (1's, 10's, 10000's)
int place; // final place value
int c = 0, tens = 1, otherc; // counters for start finder
int a = 0; // another counter
//declare all arrays
char numarray[101][51]; //array of strings containing all 100 numbers
char sum[100];
printf("please save data to largesumdata.txt\n\n press enter when ready");
getchar();
// THE PROBLEM- I don't know how to get my data into my program // FIXED
// using fscanf()
FILE *pf; // declare a pointer to the file
pf = fopen("largesumdata.txt", "r"); // trys to open file // "r" means read only
if(pf == NULL)
printf("Unable to open file, sorry Jar\n");
else
{
for(j = 0; j < 100; j++)
fscanf(pf, "%s\n", &numarray[j]); // fscanf(pointer, data type, location)
}
//TESTING
//printf("You have reached point A\n");//POINT A WAS REACHED
//TESTING
//TESTING
//printf("Check1, %c\n", numarray[45][23]);
//TESTING
//TESTING
//printf("%c\n", numarray[90][22]);//Can successfully call characters from array
//TESTING
// (Brute force attempt) //I NEVER MESS WITH numarray WHY IS IT CHANGING
for(i = 49; i >= 0; i--)
{
//printf("%d\n", d);
for(j = 0; j < 100; j++)
{
d = (int)numarray[j][i] - 'o';
//printf("%d\n", d);
//holdup// d -= 48; // ASCII conversion // could also write "d = d-48"
//printf("%d\n", d);
placesum += d; // could also write "placesum = placesum + d"
//printf("%d\n", placesum);
}
place = placesum % 10;
placesum = placesum / 10; // takes "10's place" digit for next column
// now need to put 'int place' into 'char sum'
sum[i+5] = (char)place+'0'; // ASCII conversion // "+5" for extra space //HERE not properly stored in sum
}
//TESTING
//printf("Check2, %c\n", numarray[45][23]);
//TESTING
//TESTING
//printf("You have reached point B\n");//POINT B WAS REACHED
//TESTING
// find out where sum starts
for(c=0; c<10; c++)
if(sum[c] != '0')
break;
//TESTING
//printf("You have reached point C\n"); //POINT C WAS REACHED
//TESTING
otherc = 4-c;
printf("The first 10 digits of the sum of all those f***ing numbers is....\n");
printf("%d-%d-%d-%d-%d-%d-%d-%d-%d-%d", sum[otherc, otherc+1, otherc+2, otherc+3, otherc+4, otherc+5, otherc+6, otherc+7, otherc+8, otherc+9]);
//%c-%c-%c-%c-%c-%c-%c-%c-%c-%c //copy and paste purposes
//%d-%d-%d-%d-%d-%d-%d-%d-%d-%d // ^^^^^
getchar();
return 0;
}
附注如果我的过多笔记令人困惑,我深表歉意
最佳答案
您使用错误的形式在 C 中打印数组。
sum[其他c、其他c+1、其他c+2、其他c+3、其他c+4、其他c+5、其他c+6、其他c+7、其他c+8、其他c+9]
-> 这实际上衰减为 sum[otherc+9]
因为 C 将 ,
视为运算符。
要打印每个数组索引处的值,您应该像这样使用它:sum[otherc], sum[otherc+1], sum[otherc+2],..
要了解有关 C 的 ,
(逗号)运算符的更多信息,您可以 begin here
在您的 printf
中,正如我上面所解释的,第一个格式说明符 %d
得到 sum[otherc + 9]
,因为 sum[otherc,...,otherc+9]
实际上是一个数字,即数组 sum
的第 otherc + 9
索引。您没有为其他格式说明符提供任何要打印的内容,因此您会得到垃圾。
关于c - 大数、求和和数据转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320280/