检查数字是否在一个范围内 [英] Checking whether the number in a range

查看:74
本文介绍了检查数字是否在一个范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将输入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;>  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 ++)> 



,这只是最明显的。

在代码中添加注释以解释它应该做什么。


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.


这篇关于检查数字是否在一个范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆