冒泡排序 - 输出整数的字符串

时间:2017-09-16 16:32:31

标签: c string algorithm sorting bubble-sort

我无法将第二个冒泡排序打印出字符串,它只输出0和1。

请原谅我缺乏编程技巧和丑陋的代码 - 但即使我尝试使用 strcmp 在其他线程中推荐的解决方案,我也无法使其正常运行并在char声明之前放置

算法的目标是首先按一个顺序输出几个整数,然后反转它。角色也是如此,这就是我被困的地方。

字符的冒泡排序:

  // Print (unsorted) CHAR in-values
  printf ("\n\nin:  ");
  for (cc = 0; cc <= 10; cc++) {
      printf ("%d", datastring[cc]);
  }

  // Sort CHAR out-values
  printf ("\n out: ");

  lengthcc = sizeof(datastring) / sizeof(char);

  for (cc = 0; cc < lengthcc; ++cc) {

    for (dd = 1; dd < lengthcc; ++dd) {

            if (datastring[dd] < datastring[dd - 1]) {

                tmpcc = datastring[dd];
                datastring[dd] = datastring[dd - 1];
                datastring[dd - 1] = tmpcc;

            }

    }

  }

  // Print (sorted) CHAR out-values
  for (cc = 0; cc < lengthcc; ++cc) {

    printf ("%d", datastring[cc]);

  }

代码声明的开头和整理的排序

int main (int argc, char ** argv)
{
  int ii, jj, cc, length, lengthcc, dd;

  int data[] = { 7, 4, 3 };
  char datastring[10] = { "o", "i", "u", "f", "O", "I", "w", "U", "W", "B", "F" };
  int tmp;
  char tmpcc;

  // Print first arguments
  printf ("%s %s %s \n \n", argv[0], argv[1], argv[2]);

  // Print (unsorted) INT in-values
  printf ("in:  ");
  for (ii = 0; ii <= 2; ii++) {
      printf ("%d", data[ii]);
  }

  // Sort INT out-values
  printf ("\n out: ");

  length = sizeof(data) / sizeof(int);

  for (ii = 0; ii < length; ++ii) {

    for (jj = 1; jj < length; ++jj) {

            if (data[jj] < data[jj - 1]) {

                tmp = data[jj];
                data[jj] = data[jj - 1];
                data[jj - 1] = tmp;

            }

    }

  }

  // Print (sorted) INT out-values
  for (ii = 0; ii < length; ++ii) {

    printf ("%d", data[ii]);

  }

很抱歉凌乱的代码和糟糕的演示文稿。

1 个答案:

答案 0 :(得分:0)

这是代码:

SELECT MONTHNAME(purchase_date) AS Month,
    SUM(CASE WHEN weight IS NULL THEN
                 CASE WHEN special IS NULL THEN price*amount
                          ELSE amount / CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(special, '/', 1), '/', -1),DECIMAL(10,2)) * CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(special, '/', 2), '/', -1),DECIMAL(10,2)) END
             WHEN amount IS NULL THEN price*weight
    END) AS Profit
FROM sales
GROUP BY MONTHNAME(purchase_date)

你必须写“%c”来显示字符。 “%d”用于显示整数,“%f”用于显示整数或浮点数等。 因为字母是由ascii表排列的,所以使用大写字母不能很好地工作。 如果要初始化字符串中的单个字符,则必须使用单个qoute。弦的长度也太短了。 请改变你的代码风格。为什么使用“dd”而不是“d”这样的变量?

相关问题