检查数字是否在一个范围内

编程入门 行业动态 更新时间:2024-10-11 19:17:39
本文介绍了检查数字是否在一个范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是将输入A2D3作为字符串并且想要打印AADD.A的数量等于2且D等于3倍......但是为什么我的代码不起作用请帮助... 我尝试过:

#include< iostream>使用namespace std ; int main() { int n; while(cin>> n) { for(int k = 0; k< n;> { string s; cin>> s; int len = s.size(); for(int i = 0; i< len;> { char c = s [i]; if (c =='1'&& c =='9') { int pos = i; while(s [i]!='A '&& s [i]!='Z') { i ++; } int p = i; int len2 = p-pos-1; int number = 0; for(int j = 0; j< len2; j ++) { int digit = s [pos] - '0'; number * = 10; number + = digit; } for(int j = 0; j< number; j ++)> { cout<< s [pos-1]; } } } cout<< endl; } } 返回0; }

解决方案

我猜不工作意味着它不能编译。 有这么多错误,理解代码变得复杂。 有些错误

for ( int k = 0 ; k< n;> = 0 ; i< len;>

if (c == ' 1'&& c == ' 9')

while (s [i]!= ' A'&& s [i]!= ' Z')

int pos = i; while (s [i]!= ' A'&& s [i]!= ' Z') { i ++; }

for ( int j = 0 ; j< number; j ++)>

,这只是最明显的。 在代码中添加注释以解释它应该做什么。

i just taking input A2D3 as string and wanted to print AADD.the number of A equal to 2 and D equal to 3 times..but why is my code not working help please... What I have tried:

#include <iostream> using namespace std; int main() { int n; while(cin>>n) { for(int k=0; k<n;> { string s; cin>>s; int len=s.size(); for(int i=0; i<len;> { char c=s[i]; if(c=='1'&&c=='9') { int pos=i; while(s[i]!='A'&&s[i]!='Z') { i++; } int p=i; int len2=p-pos-1; int number=0; for (int j = 0; j < len2; j++) { int digit=s[pos] - '0'; number *= 10; number += digit; } for(int j=0;j<number;j++)> { cout<<s[pos-1]; } } }cout<<endl; } } return 0; }

解决方案

I guess "not working" means that it don't compile. There so many bugs, it get complicated to understand the code. Some bugs

for(int k=0; k<n;>

for(int i=0; i<len;>

if(c=='1'&&c=='9')

while(s[i]!='A'&&s[i]!='Z')

int pos=i; while(s[i]!='A'&&s[i]!='Z') { i++; }

for(int j=0;j<number;j++)>

and it is just the most obvious ones. Add comments in code to explain what it is supposed to do.

更多推荐

检查数字是否在一个范围内

本文发布于:2023-11-07 19:24:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:范围内   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!