如果数字等于数组中的数字,如何输出字母 [英] How to output letters if the number is equal to a number in the array
本文介绍了如果数字等于数组中的数字,如何输出字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for(int i=0; i<5;i++)
cin>>a[i];
for(int w=0;w<5;w++){
for(int y=0;y<5;y++){
if(a[w]*(y+1)==a[y]){
cout<<"A"<<" ";
else
cout<<a[w]*(y+1)<<" ";
}
}
cout<<endl;
}
sample input: 1 2 3 4 5
target output my output
A A A A A A A A A A
A A 6 8 10 2 4 6 8 10
A 6 9 12 15 3 6 9 12 15
A 8 12 16 20 4 8 12 16 20
A 10 15 20 25 5 10 15 20 25
我该怎么办?
What should i do?
推荐答案
我想从你的问题陈述中,你需要搜索数组。我没有在你的代码中看到你搜索了数组。我认为你已编入索引。
I think from your problem statement, you need to search the array. I do not see in your code that you searched the array. I think you indexed into the array.
#include <stdio.h>
#include <iostream>
using namespace std;
void original(void)
{
int a[] = { 1, 2, 3, 4, 5 };
for(int w=0;w<5;w++)
{
printf("Row %i, w = %i: ", w+1, w);
for(int y=0;y<5;y++)
{
printf("\n Col %i: {a[w] = %2i, a[w]*(y+1) = %2i, a[y] = %2i} ", y, a[w], a[w]*(y+1), a[y]);
if(a[w]*(y+1)==a[y])
cout<<"A"<<" ";
else
cout<<a[w]*(y+1)<<" ";
}
cout<<endl;
}
}
void new_version(void)
{
/*
Sounds from your problem statement like you want to SEARCH
the array.
*/
const int end_of_array = -1;
int a[] = { 1, 2, 3, 4, 5, end_of_array},
i = 0, // an index
y = 0;
for(int w=0;w<5;w++) // as in original
{
for(y=0; y<5; y++) // as in original
{
for(i=0; a[i] != end_of_array; i++) // SEARCH the array
{
if(a[w]*(y+1)==a[i]) // test for found
/* found */ break;
}
if(a[i] == end_of_array) // if index after end of array, then not found. Otherwise, found.
cout<<a[w]*(y+1)<<" "; // not in array
else
cout<<"A"<<" "; // in array
}
cout<<endl;
}
}
int main(int argc, char *argv[])
{
original();
cout<<endl<<"==========================="<<endl;
new_version();
getchar();
return 0;
}
</iostream></stdio.h>
这篇关于如果数字等于数组中的数字,如何输出字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文