for (int j = 1; j <= strlen(num); j++)
时间: 2024-06-08 17:08:03 浏览: 6
应该是 `j++`,这样才能使变量 `j` 在每次循环后自增1。所以完整的代码应该是:
```c
for ( j = 1; j <= strlen(num); j++) {
// 循环体代码
}
```
这个循环会在 `j` 从1开始,每次循环自增1,直到 `j` 的值大于 `num` 字符串的长度为止。
相关问题
#include<iostream> #include<cstring> using namespace std; void mnbs(int n,char a[][7]){ //mnbs-->max number bubble sort int s; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ s=0; for(int p=0;p<7;p++){ if(a[i][p]<a[j][p]||(a[i][p]=='/'&&int(a[j][p])>0)){ s=1; break; } } if(s){ swap(a[i],a[j]); } } } } int main(){ int n; cin>>n; char a[n][7],na[n][7]; int la[n]; for(int i=0;i<n;i++) for(int j=0;j<7;j++)na[i][j]='/'; for(int i=0;i<n;i++){ cin>>a[i]; la[i]=strlen(a[i]); } for(int i=0;i<n;i++) for(int j=0;j<la[i];j++)na[i][j]=a[i][j]; mnbs(n,na); for(int i=0;i<n;i++){ for(int j=0;j<la[i];j++) if(na[i][j]!='/')cout<<na[i][j]; } return 0; } 运行结果不正确(我输入3个数,123、456、9,之后给我输出了个94561)
非常抱歉,我之前给出的示例代码确实存在问题。感谢您的指正。以下是修正后的示例代码:
```cpp
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
bool custom_compare(const string& x, const string& y) {
return x + y > y + x;
}
string largest_number(int n, string a[]) {
sort(a, a + n, custom_compare);
string result = "";
for(int i = 0; i < n; i++) {
result += a[i];
}
return result;
}
int main() {
int n;
cin >> n;
string a[n];
for(int i = 0; i < n; i++) {
cin >> a[i];
}
string largest_num = largest_number(n, a);
cout << largest_num << endl;
return 0;
}
```
这段修正后的代码会正确地输出拼接后的最大数。对于输入的示例,它将输出:9456123。
再次感谢您的指正,并对之前的错误给您带来的困扰表示歉意。
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> int main() { char a[100]; gets(a); int t; int len = strlen(a); int count[27] = { 0 }; char s[27] = { 0 }; int num[27] = { 0 }; for (int i = 1; i <= len; i++) { if (a[i] > 'a' && a[i] < 'z') { count[a[i] - 'a' + 1]++; } if (a[i] > 'A' && a[i] < 'Z') { count[a[i] - 'A' + 1]++; } } for (int j = 1, i = 1; i <= len; j++, i++) { num[j] = count[i]; } for (int j = 1; j < 26; j++) { for (int i = 1; i < 26 - i; i++) { if (count[i] < count[i + 1]) { t = count[i]; count[i] = count[i + 1]; count[i + 1] = t; } } } int max = count[1]; char c; int flag = 0; while (max >= 0) { for (int i = 1; i <= 26; i++) { if (num[i] == max) { c = i + 'A'; if (flag == 0) { printf("%c-%d", c, max); flag = 1; } else printf(" %c-%d", c, max); } } max--; } return 0; }这个代码怎么修改
这个代码有几个问题需要修改:
1. 使用 `gets()` 函数会存在安全隐患,应该改用 `fgets()` 函数。
2. 在 `for` 循环中,数组下标应该从0开始,而不是从1开始。
3. 最后的输出格式不够清晰,应该加上换行符。
下面是修改后的代码:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main() {
char a[100];
fgets(a, 100, stdin);
int len = strlen(a);
int count[26] = { 0 };
int num[26] = { 0 };
for (int i = 0; i < len; i++) {
if (a[i] >= 'a' && a[i] <= 'z') {
count[a[i] - 'a']++;
}
if (a[i] >= 'A' && a[i] <= 'Z') {
count[a[i] - 'A']++;
}
}
for (int j = 0, i = 0; i < 26; j++, i++) {
num[j] = count[i];
}
for (int j = 0; j < 26; j++) {
for (int i = 0; i < 26 - i - 1; i++) {
if (count[i] < count[i + 1]) {
int t = count[i];
count[i] = count[i + 1];
count[i + 1] = t;
}
}
}
int max = count[0];
int flag = 0;
while (max >= 0) {
for (int i = 0; i < 26; i++) {
if (num[i] == max) {
char c = i + 'A';
if (flag == 0) {
printf("%c-%d", c, max);
flag = 1;
}
else {
printf(" %c-%d", c, max);
}
}
}
max--;
}
printf("\n");
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)