for(int i=startIndex;i<=endIndex;i++) { if (a[i]!='*') {a[curIndex++]=a[i]; } }
时间: 2024-04-28 19:21:46 浏览: 10
这段代码是将 * 之间的非 * 字符移到字符串的开头。具体来说,startIndex 和 endIndex 分别表示字符串开头和结尾的 * 的位置,curIndex 表示当前可以放置非 * 字符的位置。对于字符串中的每一个字符,如果它不是 *,就将它放到 curIndex 所指示的位置,并将 curIndex 加 1。这样,最终 curIndex 的位置就是处理后的字符串中第一个 * 的位置。因为 startIndex 和 endIndex 分别指向字符串开头和结尾的 * 的位置,所以在循环过程中,只有在 a[i] 不是 * 的情况下才需要将其移到开头,否则直接跳过。
相关问题
#include<iostream> #include<cstring> using namespace std; void fun(char *a) { int len=strlen(a); int startIndex = 0; int endIndex=len-1; while(a[startIndex]=='*'&&startIndex<len) {startIndex++;} while (a[endIndex]=='*'&&endIndex>=0) {endIndex--;} int curIndex = startIndex. for(int i=startIndex;i<=endIndex;i++) { if (a[i]!='*') {a[curIndex++]=a[i]; } } for (int i=curIndex;i<=endIndex;i++) { a[i]='*'; } a[endIndex+1]='\0';} int main() { char a[100]; cout <<"请输入一个不包含空格的带*字符串:"<< endl; cin>>a; fun(a); cout <<"处理后的字符串为:"<< endl; cout << a << endl; return 0; }
这段代码是用来处理一个带有 * 的字符串,将 * 移动到字符串的末尾。具体来说,该函数会先找到字符串开头和结尾的 *,然后将 * 之间的非 * 字符移到字符串的开头,最后将剩余的 * 移动到字符串的末尾。这个函数的时间复杂度为 O(n),其中 n 是字符串的长度。在主函数中,会先读入一个不包含空格的带 * 字符串,然后调用该函数进行处理,并输出处理后的字符串。
@Component public class temperature { @Autowired private RestTemplate restTemplate; public static ArrayList date = new ArrayList(); public static ArrayList temperature = new ArrayList(); public static ArrayList windSpeed = new ArrayList(); @PostConstruct public void init() { String url = "https://mock.mengxuegu.com/mock/643e52da56377f1ed96c16c6/dataFetcher"; JSONObject forObject = restTemplate.getForObject(url, JSONObject.class); date = forObject.get("date", ArrayList.class); temperature = forObject.get("temperature", ArrayList.class); windSpeed = forObject.get("windspeed", ArrayList.class); test(); //windspeed.test(windSpeed); } public void test() { // 将数组转换为List,便于后续处理 // 将List分成14组 List<Integer> temperature = new ArrayList<>(); // 假设已存在的温度数据已经存储在temperature中 int groupSize = 24; int groupCount = temperature.size() / groupSize; for (int i = 0; i < groupCount; i++) { int startIndex = i * groupSize; int endIndex = startIndex + groupSize; List<Integer> group = temperature.subList(startIndex, endIndex); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int value : group) { if (value < min) { min = value; } if (value > max) { max = value; } } System.out.println("Group " + (i + 1) + ": min = " + min + ", max = " + max); } int firstHalfMax = Integer.MIN_VALUE; int secondHalfMax = Integer.MIN_VALUE; for (int i = 0; i < temperature.size(); i++) { int value = temperature.get(i); if (i < temperature.size() / 2 && value > firstHalfMax) { firstHalfMax = value; } if (i >= temperature.size() / 2 && value > secondHalfMax) { secondHalfMax = value; } } System.out.println("First half max = " + firstHalfMax); System.out.println("Second half max = " + secondHalfMax); }
这段代码主要是获取一个模拟的天气数据并进行处理。首先使用RestTemplate发送一个HTTP请求获取JSON格式的天气数据,然后将其分别存储在三个ArrayList中,分别为日期(date)、温度(temperature)和风速(windSpeed)。然后对温度进行一些处理,将其分成14组,每组包含24个数据,在每组中求出最小值和最大值,并输出到控制台。然后再找出前半部分和后半部分中的最大值,并同样输出到控制台。整个过程是在类初始化时进行的,使用了@PostConstruct注解,表示在初始化后立即执行。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)