我正在嘗試用 C 創建一個程式,其中用戶將隨機字串插入到矩陣中。然后程式使用這個公式來計算每個字串,其余的代碼顯示具有最高值的字串。每個后續字母的值都比前一個更大。
如果有人能指出我正確的方向,我會很高興,目前該程式顯示字串中的一些隨機字母。
公式: Sum=(t*26^(n-1))
t = 字母數,n = 剩余字母數
例子: abc -> 1*26^2 2*26^1 3*26^0
其余代碼:
#include <stdio.h>
#include <string.h>
void insert(int, char[10][10], int num[]);
int computing(char[10]);
void sort(int, int num[], char[10][10]);
int main(){
int x;
char arr[10][10];
int num[x];
printf("How many words do you wish to enter: \n");
scanf(" %d", &x);
insert(x, arr, num);
sort(x, num, arr);
return 0;
}
void insert(int x, char arr[10][10], int num[]){
int i, r;
for(i=0; i<x; i ){
printf("Insert %d. word: ", i 1);
scanf("%s", &arr[i][0]);
num[i] = computing(arr[i]);
}
}
int computing(char arr[10]){
int n, i, t=1, m=0, k;
n = strlen(arr);
k = n;
for(i=0; i<n; i ){
m = (t*26^(k-1));
t ;
k = k - 1;
}
return m;
}
void sort(int x, int num[], char arr[10][10]){
int i, temp;
char ch;
for(i = 0; i < x - 1; i ){
if(num[i] > num[i 1]){
temp = num[i];
num[i] = num[i 1];
num[i 1] = temp;
ch = arr[i][0];
arr[i][0] = arr[i 1][0];
arr[i 1][0] = ch;
}
}
printf("Word with the biggest sum is: %s\n", &arr[x-1][0]);
}
uj5u.com熱心網友回復:
問題:
^是不是電源功能int num[x];高于scanf(" %d",&x);所以x未初始化。移動int num[x];下面的scanf- 排序壞了。即使進行了上述修復,它也會破壞我提供的測驗資料的條目。
- 排序需要對資料執行 N 次傳遞。目前,它只做一個。
arr是硬連線的char arr[10][10];。它應該是動態的,如num:char arr[x][10];
定義一個struct同時包含單詞 string (in arr)和它的numscore的方法要容易得多。然后,排序不必只交換兩個陣列一個。
因此,我重構了代碼以使用此類struct并修復了排序和其他問題。注釋為:
#include <stdio.h>
#include <string.h>
typedef struct {
int num; // ranking
char str[10]; // word string
} word_t;
void insert(int, word_t num[]);
int computing(word_t *);
void sort(int, word_t num[]);
int
main(void)
{
int x;
// NOTE/BUG: x is _not_ yet initialized
#if 0
char arr[10][10];
int num[x];
#endif
printf("How many words do you wish to enter: \n");
scanf(" %d", &x);
// NOTE/FIX: x is now initialized
#if 1
word_t words[x];
#endif
insert(x, words);
sort(x, words);
return 0;
}
void
insert(int x, word_t *words)
{
int i;
for (i = 0; i < x; i ) {
word_t *word = &words[i];
printf("Insert %d. word: ", i 1);
scanf("%s", word->str);
word->num = computing(word);
printf("DEBUG: num=%d '%s'\n",word->num,word->str);
}
}
int
computing(word_t *word)
{
int n, i,
t = 1,
m = 0,
k;
n = strlen(word->str);
k = n;
// NOTE/FIX: compute pow(26,k - 1);
#if 1
int p26 = 1;
for (i = 1; i <= (k - 1); i)
p26 *= 26;
#endif
for (i = 0; i < n; i ) {
#if 0
m = (t * 26 ^ (k - 1));
#else
m = (t * p26);
#endif
t ;
k = k - 1;
// NOTE/FIX: recalc pow(26,k - 1) now that we've decremented k
#if 1
p26 /= 26;
#endif
}
return m;
}
void
sort(int x, word_t *words)
{
int i;
word_t temp;
int more = 1;
while (more) {
more = 0;
for (i = 0; i < x - 1; i ) {
word_t *lhs = &words[i];
word_t *rhs = &words[i 1];
if (lhs->num > rhs->num) {
temp = *lhs;
*lhs = *rhs;
*rhs = temp;
more = 1;
}
}
}
printf("Word with the biggest sum is: %s\n", words[x - 1].str);
}
這是我使用的輸入資料:
7
abc
defg
hijkl
mnopqr
hello
world
gbye
請注意,在原始代碼中,程式列印出來mbye作為答案
這是程式輸出:
How many words do you wish to enter:
Insert 1. word: DEBUG: num=731 'abc'
Insert 2. word: DEBUG: num=19010 'defg'
Insert 3. word: DEBUG: num=494265 'hijkl'
Insert 4. word: DEBUG: num=12850896 'mnopqr'
Insert 5. word: DEBUG: num=494265 'hello'
Insert 6. word: DEBUG: num=494265 'world'
Insert 7. word: DEBUG: num=19010 'gbye'
Word with the biggest sum is: mnopqr
未來可能出現的問題/修復。需要注意的是,num價值是相同的兩個hello和world。如果我們不厭其煩地將數字求冪,這些數字應該不同嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373432.html
