代码练习:通过键盘键入数字判断重复个数

注意问题:重复统计;数组个数输入确定;计数方法;计算量少

#include

using namespace std;
int i_zhaungtai=0;//状态量,判断
int number,x;//number记录输入范围,x记录输入的值
int a[100];//输入数组个数记录
int main()
{
cout << "请键入所输值的范围:" << '\n';
cin >>number;
//count c = i_x;
int *i_s =new int[number];//将键入的number给数组大小
for (int i = 0; i < number;++i)
i_s[i]=0;//数组初始化
int i_tem;//记录输入值
while (cin >> i_tem)
{
i_s[i_tem] = i_s[i_tem] + 1;//计数式
if (i_s[i_tem] == 1)//判断出现次数超过一就不写入,保证a中都是唯一值
{
a[x] = i_tem;
x = x + 1;
}
}
/*for (int i = 0; i < x; ++i)
{
cout << a[i] << '\n';
}*/
/*cin >> i_tem;
cout << i_tem << '\n';
int a[5] = { 1, 2, 3, 0, 6 };
for (int i = 0; i < 5; ++i)
i_s[a[i]] = i_s[a[i]] + 1;*/
for (int i = 0; i {
if (i_s[a[i]]>1)//只判断输入个数的值,不必全部范围内检测
{
cout << a[i] << "重复出现" << i_s[a[i]] << "次" << '\n';
i_zhaungtai = 1;
}
}
if (i_zhaungtai==0)//判断状态量
cout << "无重复数字" << '\n';
/*cout << '\n';
cout << x << ' '<for (int i = 0; i < number; ++i)
{
cout << i_s[i] << '\n';
}*/
system("pause");

}


import numpy as np
a=(raw_input()).split()
array1=np.array(a)
#print(array1.dtype)
#print(array1)
uniqs, counts = np.unique(array1, return_counts=True)
#print("Unique items : ", uniqs)
#print("Counts : ", counts)
for i in uniqs:
    if counts[np.where(uniqs==i)]>1:
        print (i,'has appeared',int(counts[np.where(uniqs==i)]),'times')

#encoding =utf-8
import numpy as np
#####date is producted by keybord
#a=(raw_input()).split()
#array1=np.array(a)

#####date is producted by random
rn = np.random.RandomState(100)
array1=rn.randint(0,10,size=[1,20])
print(array1)
#print(array1.dtype)
#print(array1)
uniqs, counts = np.unique(array1, return_counts=True)
print("Unique items : ", uniqs)
print("Counts : ", counts)

#####using for make the classification
#for i in range(0,len(uniqs)):
 #   if counts[i]>1:
  #      print (uniqs[i],'has appeared',int(counts[i]),'times')

#####using np.where make the classification
index1=np.where(counts>2)
print(uniqs.take(index1),"has appeared more than 2 times")

如果对你有帮助,谢谢你^_^

红包还不收?

代码练习:通过键盘键入数字判断重复个数_第1张图片

代码练习:通过键盘键入数字判断重复个数_第2张图片

你可能感兴趣的:(代码练习:通过键盘键入数字判断重复个数)