我是一名编程初学者,有人可以帮助我解决这个问题吗? 该代码适用于多头和多头游戏,我需要做的是成名的猜测。名人堂也需要在前面输入演奏的名字。
String s = "This is a test.";
String enc = Utils.encrypt(s);
System.out.println(enc);
// fQHfYjbD+xAuN5XzH2ojk/EWNeKXUrKRSfx8LU+5dpuKkM/pueCMBjKCZw==
String dec = Utils.decrypt(enc);
System.out.println(dec);
// This is a test.
答案 0 :(得分:3)
对“带有附加名称的数字”进行排序的一种简单方法是将数字和名称放入struct
中,并具有一个数组struct
。然后,只需使用标准的qsort
函数对数组进行排序即可。
可能看起来像这样:
#include <stdio.h>
#include <stdlib.h>
// Define a type to hold the score together with the name of the player
typedef struct
{
char name[42];
int score;
} PlayerStats;
// Make a compare function to be used by qsort
int cmpfunc (const void * a, const void * b){
const PlayerStats* pA = a;
const PlayerStats* pB = b;
if (pA->score > pB->score) return -1;
if (pA->score < pB->score) return 1;
return 0;
}
// A function for printing the players and their score
void print_stats(PlayerStats *ps, size_t n)
{
for(size_t i=0; i<n; ++i) printf("%s : score=%d\n", ps[i].name, ps[i].score);
}
int main(){
// Make an array of players with scores.
PlayerStats player_stats[3] = {{"p1", 17}, {"p2", 9}, {"p3", 42}};
size_t numElements = sizeof player_stats / sizeof player_stats[0];
size_t sizeElement = sizeof player_stats[0];
printf("Unsorted:\n");
print_stats(player_stats, numElements);
// Use the standard qsort for sorting the array
qsort(player_stats, numElements, sizeElement, cmpfunc);
printf("Sorted:\n");
print_stats(player_stats, numElements);
return 0;
}
输出:
Unsorted:
p1 : score=17
p2 : score=9
p3 : score=42
Sorted:
p3 : score=42
p1 : score=17
p2 : score=9
在这里尝试:https://ideone.com/HMgDbn
如果要升序排序,只需更改比较功能,如:
int cmpfunc (const void * a, const void * b){
const PlayerStats* pA = a;
const PlayerStats* pB = b;
if (pA->score > pB->score) return 1; // Change -1 to 1
if (pA->score < pB->score) return -1; // Change 1 to -1
return 0;
}